patch: mqueue improvements

move callback handler to 'struct mqueue'
This commit is contained in:
Alfred E. Heggestad 2013-03-09 11:00:38 +00:00
parent ba87f31c41
commit e1125a5334
2 changed files with 14 additions and 11 deletions

View file

@ -6,7 +6,7 @@
struct mqueue; struct mqueue;
typedef void (mqueue_h)(int id, void *data); typedef void (mqueue_h)(int id, void *data, void *arg);
int mqueue_alloc(struct mqueue **mqp); int mqueue_alloc(struct mqueue **mqp, mqueue_h *h, void *arg);
int mqueue_push(struct mqueue *mq, mqueue_h *h, int id, void *data); int mqueue_push(struct mqueue *mq, int id, void *data);

View file

@ -23,10 +23,11 @@
*/ */
struct mqueue { struct mqueue {
int pfd[2]; int pfd[2];
mqueue_h *h;
void *arg;
}; };
struct msg { struct msg {
mqueue_h *h;
int id; int id;
void *data; void *data;
uint32_t magic; uint32_t magic;
@ -71,8 +72,7 @@ static void event_handler(int flags, void *arg)
return; return;
} }
if (msg.h) mq->h(msg.id, msg.data, mq->arg);
msg.h(msg.id, msg.data);
} }
@ -80,21 +80,26 @@ static void event_handler(int flags, void *arg)
* Allocate a new Message Queue * Allocate a new Message Queue
* *
* @param mqp Pointer to allocated Message Queue * @param mqp Pointer to allocated Message Queue
* @param h Message handler
* @param arg Handler argument
* *
* @return 0 if success, otherwise errorcode * @return 0 if success, otherwise errorcode
*/ */
int mqueue_alloc(struct mqueue **mqp) int mqueue_alloc(struct mqueue **mqp, mqueue_h *h, void *arg)
{ {
struct mqueue *mq; struct mqueue *mq;
int err = 0; int err = 0;
if (!mqp) if (!mqp || !h)
return EINVAL; return EINVAL;
mq = mem_zalloc(sizeof(*mq), destructor); mq = mem_zalloc(sizeof(*mq), destructor);
if (!mq) if (!mq)
return ENOMEM; return ENOMEM;
mq->h = h;
mq->arg = arg;
mq->pfd[0] = mq->pfd[1] = -1; mq->pfd[0] = mq->pfd[1] = -1;
if (pipe(mq->pfd) < 0) { if (pipe(mq->pfd) < 0) {
err = errno; err = errno;
@ -119,13 +124,12 @@ int mqueue_alloc(struct mqueue **mqp)
* Push a new message onto the Message Queue * Push a new message onto the Message Queue
* *
* @param mq Message Queue * @param mq Message Queue
* @param h Message handler
* @param id General purpose Identifier * @param id General purpose Identifier
* @param data Application data * @param data Application data
* *
* @return 0 if success, otherwise errorcode * @return 0 if success, otherwise errorcode
*/ */
int mqueue_push(struct mqueue *mq, mqueue_h *h, int id, void *data) int mqueue_push(struct mqueue *mq, int id, void *data)
{ {
struct msg msg; struct msg msg;
ssize_t n; ssize_t n;
@ -133,7 +137,6 @@ int mqueue_push(struct mqueue *mq, mqueue_h *h, int id, void *data)
if (!mq) if (!mq)
return EINVAL; return EINVAL;
msg.h = h;
msg.id = id; msg.id = id;
msg.data = data; msg.data = data;
msg.magic = MAGIC; msg.magic = MAGIC;