-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathmqueue.c
170 lines (136 loc) · 2.82 KB
/
mqueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* @file mqueue.c Thread Safe Message Queue
*
* Copyright (C) 2010 Creytiv.com
*/
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <re_types.h>
#include <re_fmt.h>
#include <re_mem.h>
#include <re_net.h>
#include <re_main.h>
#include <re_mqueue.h>
#include "mqueue.h"
#define MAGIC 0x14553399
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define close closesocket
#endif
/**
* Defines a Thread-safe Message Queue
*
* The Message Queue can be used to communicate between two threads. The
* receiving thread must run the re_main() loop which will be woken up on
* incoming messages from other threads. The sender thread can be any thread.
*/
struct mqueue {
re_sock_t pfd[2];
struct re_fhs *fhs;
mqueue_h *h;
void *arg;
};
struct msg {
void *data;
uint32_t magic;
int id;
};
static void destructor(void *arg)
{
struct mqueue *q = arg;
if (q->pfd[0] != RE_BAD_SOCK) {
q->fhs = fd_close(q->fhs);
(void)close(q->pfd[0]);
}
if (q->pfd[1] != RE_BAD_SOCK)
(void)close(q->pfd[1]);
}
static void event_handler(int flags, void *arg)
{
struct mqueue *mq = arg;
struct msg msg;
ssize_t n;
if (!(flags & FD_READ))
return;
n = pipe_read(mq->pfd[0], &msg, sizeof(msg));
if (n < 0)
return;
if (n != sizeof(msg)) {
(void)re_fprintf(stderr, "mqueue: short read of %d bytes\n",
n);
return;
}
if (msg.magic != MAGIC) {
(void)re_fprintf(stderr, "mqueue: bad magic on read (%08x)\n",
msg.magic);
return;
}
mq->h(msg.id, msg.data, mq->arg);
}
/**
* Allocate a new Message Queue
*
* @param mqp Pointer to allocated Message Queue
* @param h Message handler
* @param arg Handler argument
*
* @return 0 if success, otherwise errorcode
*/
int mqueue_alloc(struct mqueue **mqp, mqueue_h *h, void *arg)
{
struct mqueue *mq;
int err = 0;
if (!mqp || !h)
return EINVAL;
mq = mem_zalloc(sizeof(*mq), destructor);
if (!mq)
return ENOMEM;
mq->fhs = NULL;
mq->h = h;
mq->arg = arg;
mq->pfd[0] = mq->pfd[1] = RE_BAD_SOCK;
if (pipe(mq->pfd) < 0) {
err = RE_ERRNO_SOCK;
goto out;
}
err = net_sockopt_blocking_set(mq->pfd[0], false);
if (err)
goto out;
err = net_sockopt_blocking_set(mq->pfd[1], false);
if (err)
goto out;
err = fd_listen(&mq->fhs, mq->pfd[0], FD_READ, event_handler, mq);
if (err)
goto out;
out:
if (err)
mem_deref(mq);
else
*mqp = mq;
return err;
}
/**
* Push a new message onto the Message Queue
*
* @param mq Message Queue
* @param id General purpose Identifier
* @param data Application data
*
* @return 0 if success, otherwise errorcode
*/
int mqueue_push(struct mqueue *mq, int id, void *data)
{
struct msg msg;
ssize_t n;
if (!mq)
return EINVAL;
msg.id = id;
msg.data = data;
msg.magic = MAGIC;
n = pipe_write(mq->pfd[1], &msg, sizeof(msg));
if (n < 0)
return errno;
return (n != sizeof(msg)) ? EPIPE : 0;
}