Browse Source
Since they're short, we use a simple array. This can be replaced later. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>ppa-0.6.1
Rusty Russell
8 years ago
3 changed files with 53 additions and 1 deletions
@ -0,0 +1,30 @@ |
|||||
|
#include <lightningd/msg_queue.h> |
||||
|
|
||||
|
void msg_queue_init(struct msg_queue *q, const tal_t *ctx) |
||||
|
{ |
||||
|
q->q = tal_arr(ctx, const u8 *, 0); |
||||
|
} |
||||
|
|
||||
|
void msg_enqueue(struct msg_queue *q, const u8 *add) |
||||
|
{ |
||||
|
size_t n = tal_count(q->q); |
||||
|
tal_resize(&q->q, n+1); |
||||
|
q->q[n] = add; |
||||
|
|
||||
|
/* In case someone is waiting */ |
||||
|
io_wake(q); |
||||
|
} |
||||
|
|
||||
|
const u8 *msg_dequeue(struct msg_queue *q) |
||||
|
{ |
||||
|
size_t n = tal_count(q->q); |
||||
|
const u8 *msg; |
||||
|
|
||||
|
if (!n) |
||||
|
return NULL; |
||||
|
|
||||
|
msg = q->q[0]; |
||||
|
memmove(q->q, q->q + 1, sizeof(*q->q) * (n-1)); |
||||
|
tal_resize(&q->q, n-1); |
||||
|
return msg; |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
/* Helper for simple message queues. */ |
||||
|
#ifndef LIGHTNING_LIGHTNINGD_MSG_QUEUE_H |
||||
|
#define LIGHTNING_LIGHTNINGD_MSG_QUEUE_H |
||||
|
#include "config.h" |
||||
|
#include <ccan/io/io.h> |
||||
|
#include <ccan/short_types/short_types.h> |
||||
|
|
||||
|
struct msg_queue { |
||||
|
const u8 **q; |
||||
|
}; |
||||
|
|
||||
|
void msg_queue_init(struct msg_queue *q, const tal_t *ctx); |
||||
|
|
||||
|
void msg_enqueue(struct msg_queue *q, const u8 *add); |
||||
|
|
||||
|
const u8 *msg_dequeue(struct msg_queue *q); |
||||
|
|
||||
|
#define msg_queue_wait(conn, q, next, arg) \ |
||||
|
io_out_wait((conn), (q), (next), (arg)) |
||||
|
|
||||
|
#endif /* LIGHTNING_LIGHTNINGD_MSG_QUEUE_H */ |
Loading…
Reference in new issue