Skip to content

Commit e1125a5

Browse files
committed
patch: mqueue improvements
move callback handler to 'struct mqueue'
1 parent ba87f31 commit e1125a5

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

include/re_mqueue.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
struct mqueue;
88

9-
typedef void (mqueue_h)(int id, void *data);
9+
typedef void (mqueue_h)(int id, void *data, void *arg);
1010

11-
int mqueue_alloc(struct mqueue **mqp);
12-
int mqueue_push(struct mqueue *mq, mqueue_h *h, int id, void *data);
11+
int mqueue_alloc(struct mqueue **mqp, mqueue_h *h, void *arg);
12+
int mqueue_push(struct mqueue *mq, int id, void *data);

src/mqueue/mqueue.c

+11-8
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
*/
2424
struct mqueue {
2525
int pfd[2];
26+
mqueue_h *h;
27+
void *arg;
2628
};
2729

2830
struct msg {
29-
mqueue_h *h;
3031
int id;
3132
void *data;
3233
uint32_t magic;
@@ -71,30 +72,34 @@ static void event_handler(int flags, void *arg)
7172
return;
7273
}
7374

74-
if (msg.h)
75-
msg.h(msg.id, msg.data);
75+
mq->h(msg.id, msg.data, mq->arg);
7676
}
7777

7878

7979
/**
8080
* Allocate a new Message Queue
8181
*
8282
* @param mqp Pointer to allocated Message Queue
83+
* @param h Message handler
84+
* @param arg Handler argument
8385
*
8486
* @return 0 if success, otherwise errorcode
8587
*/
86-
int mqueue_alloc(struct mqueue **mqp)
88+
int mqueue_alloc(struct mqueue **mqp, mqueue_h *h, void *arg)
8789
{
8890
struct mqueue *mq;
8991
int err = 0;
9092

91-
if (!mqp)
93+
if (!mqp || !h)
9294
return EINVAL;
9395

9496
mq = mem_zalloc(sizeof(*mq), destructor);
9597
if (!mq)
9698
return ENOMEM;
9799

100+
mq->h = h;
101+
mq->arg = arg;
102+
98103
mq->pfd[0] = mq->pfd[1] = -1;
99104
if (pipe(mq->pfd) < 0) {
100105
err = errno;
@@ -119,21 +124,19 @@ int mqueue_alloc(struct mqueue **mqp)
119124
* Push a new message onto the Message Queue
120125
*
121126
* @param mq Message Queue
122-
* @param h Message handler
123127
* @param id General purpose Identifier
124128
* @param data Application data
125129
*
126130
* @return 0 if success, otherwise errorcode
127131
*/
128-
int mqueue_push(struct mqueue *mq, mqueue_h *h, int id, void *data)
132+
int mqueue_push(struct mqueue *mq, int id, void *data)
129133
{
130134
struct msg msg;
131135
ssize_t n;
132136

133137
if (!mq)
134138
return EINVAL;
135139

136-
msg.h = h;
137140
msg.id = id;
138141
msg.data = data;
139142
msg.magic = MAGIC;

0 commit comments

Comments
 (0)