2 * Copyright (c) 2006 David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 #include <sys/syscall.h>
32 #include <sys/mqueue.h>
34 #include "namespace.h"
40 #include "sigev_thread.h"
41 #include "un-namespace.h"
42 #include "libc_private.h"
44 extern int __sys_kmq_notify(int, const struct sigevent *);
45 extern int __sys_kmq_open(const char *, int, mode_t,
46 const struct mq_attr *);
47 extern int __sys_kmq_setattr(int, const struct mq_attr *__restrict,
48 struct mq_attr *__restrict);
49 extern ssize_t __sys_kmq_timedreceive(int, char *__restrict, size_t,
50 unsigned *__restrict, const struct timespec *__restrict);
51 extern int __sys_kmq_timedsend(int, const char *, size_t, unsigned,
52 const struct timespec *);
53 extern int __sys_kmq_unlink(const char *);
54 extern int __sys_close(int fd);
58 struct sigev_node *node;
61 __weak_reference(__mq_open, mq_open);
62 __weak_reference(__mq_open, _mq_open);
63 __weak_reference(__mq_close, mq_close);
64 __weak_reference(__mq_close, _mq_close);
65 __weak_reference(__mq_notify, mq_notify);
66 __weak_reference(__mq_notify, _mq_notify);
67 __weak_reference(__mq_getattr, mq_getattr);
68 __weak_reference(__mq_getattr, _mq_getattr);
69 __weak_reference(__mq_setattr, mq_setattr);
70 __weak_reference(__mq_setattr, _mq_setattr);
71 __weak_reference(__mq_timedreceive_cancel, mq_timedreceive);
72 __weak_reference(__mq_timedreceive, _mq_timedreceive);
73 __weak_reference(__mq_timedsend_cancel, mq_timedsend);
74 __weak_reference(__mq_timedsend, _mq_timedsend);
75 __weak_reference(__mq_unlink, mq_unlink);
76 __weak_reference(__mq_unlink, _mq_unlink);
77 __weak_reference(__mq_send_cancel, mq_send);
78 __weak_reference(__mq_send, _mq_send);
79 __weak_reference(__mq_receive_cancel, mq_receive);
80 __weak_reference(__mq_receive, _mq_receive);
83 __mq_open(const char *name, int oflag, mode_t mode,
84 const struct mq_attr *attr)
89 mq = malloc(sizeof(struct __mq));
93 mq->oshandle = __sys_kmq_open(name, oflag, mode, attr);
94 if (mq->oshandle != -1) {
105 __mq_close(mqd_t mqd)
109 if (mqd->node != NULL) {
111 __sigev_delete_node(mqd->node);
112 __sigev_list_unlock();
116 return (__sys_close(h));
119 typedef void (*mq_func)(union sigval val);
122 mq_dispatch(struct sigev_node *sn)
124 mq_func f = sn->sn_func;
127 * Check generation before calling user function,
128 * this should avoid expired notification.
130 if (sn->sn_gen == sn->sn_info.si_value.sival_int)
135 __mq_notify(mqd_t mqd, const struct sigevent *evp)
138 struct sigev_node *sn;
141 if (evp == NULL || evp->sigev_notify != SIGEV_THREAD) {
142 if (mqd->node != NULL) {
144 __sigev_delete_node(mqd->node);
146 __sigev_list_unlock();
148 return __sys_kmq_notify(mqd->oshandle, evp);
151 if (__sigev_check_init()) {
153 * Thread library is not enabled.
159 sn = __sigev_alloc(SI_MESGQ, evp, mqd->node, 1);
165 sn->sn_id = mqd->oshandle;
166 sn->sn_dispatch = mq_dispatch;
167 __sigev_get_sigevent(sn, &ev, sn->sn_gen);
169 if (mqd->node != NULL)
170 __sigev_delete_node(mqd->node);
172 __sigev_register(sn);
173 ret = __sys_kmq_notify(mqd->oshandle, &ev);
174 __sigev_list_unlock();
179 __mq_getattr(mqd_t mqd, struct mq_attr *attr)
182 return __sys_kmq_setattr(mqd->oshandle, NULL, attr);
186 __mq_setattr(mqd_t mqd, const struct mq_attr *newattr, struct mq_attr *oldattr)
189 return __sys_kmq_setattr(mqd->oshandle, newattr, oldattr);
193 __mq_timedreceive(mqd_t mqd, char *buf, size_t len,
194 unsigned *prio, const struct timespec *timeout)
197 return __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, timeout);
201 __mq_timedreceive_cancel(mqd_t mqd, char *buf, size_t len,
202 unsigned *prio, const struct timespec *timeout)
206 _pthread_cancel_enter(1);
207 ret = __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, timeout);
208 _pthread_cancel_leave(ret == -1);
213 __mq_receive(mqd_t mqd, char *buf, size_t len, unsigned *prio)
216 return __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, NULL);
220 __mq_receive_cancel(mqd_t mqd, char *buf, size_t len, unsigned *prio)
224 _pthread_cancel_enter(1);
225 ret = __sys_kmq_timedreceive(mqd->oshandle, buf, len, prio, NULL);
226 _pthread_cancel_leave(ret == -1);
230 __mq_timedsend(mqd_t mqd, char *buf, size_t len,
231 unsigned prio, const struct timespec *timeout)
234 return __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, timeout);
238 __mq_timedsend_cancel(mqd_t mqd, char *buf, size_t len,
239 unsigned prio, const struct timespec *timeout)
243 _pthread_cancel_enter(1);
244 ret = __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, timeout);
245 _pthread_cancel_leave(ret == -1);
250 __mq_send(mqd_t mqd, char *buf, size_t len, unsigned prio)
253 return __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, NULL);
258 __mq_send_cancel(mqd_t mqd, char *buf, size_t len, unsigned prio)
262 _pthread_cancel_enter(1);
263 ret = __sys_kmq_timedsend(mqd->oshandle, buf, len, prio, NULL);
264 _pthread_cancel_leave(ret == -1);
269 __mq_unlink(const char *path)
272 return __sys_kmq_unlink(path);
276 __mq_oshandle(mqd_t mqd)
279 return (mqd->oshandle);