Files
palladum-lightning/common/msg_queue.c
Rusty Russell 6e5cb299dd global: remove unnecessary includes from C files.
Basically, `devtools/reduce-includes.sh */*.c`.

Build time from make clean (RUST=0) (includes building external libs):

Before:
	real    0m38.944000-40.416000(40.1131+/-0.4)s
	user    3m6.790000-17.159000(15.0571+/-2.8)s
	sys     0m35.304000-37.336000(36.8942+/-0.57)s
After:
	real    0m37.872000-39.974000(39.5466+/-0.59)s
	user    3m1.211000-14.968000(12.4556+/-3.9)s
	sys     0m35.008000-36.830000(36.4143+/-0.5)s

Build time after touch config.vars (RUST=0):

Before:
	real    0m19.831000-21.862000(21.5528+/-0.58)s
	user    2m15.361000-30.731000(28.4798+/-4.4)s
	sys     0m21.056000-22.339000(22.0346+/-0.35)s

After:
	real    0m18.384000-21.307000(20.8605+/-0.92)s
	user    2m5.585000-26.843000(23.6017+/-6.7)s
	sys     0m19.650000-22.003000(21.4943+/-0.69)s

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-10-23 06:44:04 +10:30

115 lines
2.2 KiB
C

#include "config.h"
#include <ccan/membuf/membuf.h>
#include <common/daemon.h>
#include <common/msg_queue.h>
#include <wire/wire.h>
static bool warned_once;
struct msg_queue {
bool fd_passing;
MEMBUF(const u8 *) mb;
};
static int extract_fd(const u8 *msg)
{
const u8 *p = msg + sizeof(u16);
size_t len = tal_count(msg) - sizeof(u16);
if (fromwire_peektype(msg) != MSG_PASS_FD)
return -1;
return fromwire_u32(&p, &len);
}
/* Close any fds left in queue! */
static void destroy_msg_queue(struct msg_queue *q)
{
const u8 **elems = membuf_elems(&q->mb);
for (size_t i = 0; i < membuf_num_elems(&q->mb); i++) {
int fd = extract_fd(elems[i]);
if (fd != -1)
close(fd);
}
}
/* Realloc helper for tal membufs */
static void *membuf_tal_realloc(struct membuf *mb, void *rawelems,
size_t newsize)
{
char *p = rawelems;
tal_resize(&p, newsize);
return p;
}
struct msg_queue *msg_queue_new(const tal_t *ctx, bool fd_passing)
{
struct msg_queue *q = tal(ctx, struct msg_queue);
q->fd_passing = fd_passing;
membuf_init(&q->mb, tal_arr(q, const u8 *, 0), 0, membuf_tal_realloc);
if (q->fd_passing)
tal_add_destructor(q, destroy_msg_queue);
return q;
}
static void do_enqueue(struct msg_queue *q, const u8 *add TAKES)
{
const u8 **msg = membuf_add(&q->mb, 1);
*msg = tal_dup_talarr(q, u8, add);
if (!warned_once && msg_queue_length(q) > 250000) {
/* Can cause re-entry, so set flag first! */
warned_once = true;
send_backtrace("excessive queue length");
}
/* In case someone is waiting */
io_wake(q);
}
size_t msg_queue_length(const struct msg_queue *q)
{
return membuf_num_elems(&q->mb);
}
void msg_enqueue(struct msg_queue *q, const u8 *add)
{
if (q->fd_passing)
assert(fromwire_peektype(add) != MSG_PASS_FD);
do_enqueue(q, add);
}
void msg_enqueue_fd(struct msg_queue *q, int fd)
{
u8 *fdmsg = tal_arr(q, u8, 0);
assert(q->fd_passing);
towire_u16(&fdmsg, MSG_PASS_FD);
towire_u32(&fdmsg, fd);
do_enqueue(q, take(fdmsg));
}
const u8 *msg_dequeue(struct msg_queue *q)
{
size_t n = msg_queue_length(q);
if (!n)
return NULL;
return membuf_consume(&q->mb, 1)[0];
}
int msg_extract_fd(const struct msg_queue *q, const u8 *msg)
{
assert(q->fd_passing);
return extract_fd(msg);
}
void msg_wake(const struct msg_queue *q)
{
io_wake(q);
}