do not release GVL for non-blocking operations
[ruby_posix_mq.git] / ext / posix_mq / posix_mq.c
blob8a016bf3cb6bef72e3148931a55a8fdcc7f26b6e
1 #define _XOPEN_SOURCE 600
2 #ifdef HAVE_SYS_SELECT_H
3 # include <sys/select.h>
4 #endif
5 #ifdef HAVE_SIGNAL_H
6 # include <signal.h>
7 #endif
8 #ifdef HAVE_PTHREAD_H
9 # include <pthread.h>
10 #endif
11 #include <ruby.h>
13 #include <time.h>
14 #include <mqueue.h>
15 #include <fcntl.h>
16 #include <sys/stat.h>
17 #include <errno.h>
18 #include <assert.h>
19 #include <unistd.h>
21 #if defined(__linux__)
22 # define MQD_TO_FD(mqd) (int)(mqd)
23 #elif defined(HAVE___MQ_OSHANDLE) /* FreeBSD */
24 # define MQD_TO_FD(mqd) __mq_oshandle(mqd)
25 #else
26 # warning mqd_t is not select()-able on your OS
27 # define MQ_IO_MARK(mq) ((void)(0))
28 # define MQ_IO_SET(mq,val) ((void)(0))
29 #endif
31 #ifdef MQD_TO_FD
32 # define MQ_IO_MARK(mq) rb_gc_mark((mq)->io)
33 # define MQ_IO_SET(mq,val) do { (mq)->io = (val); } while (0)
34 #endif
36 struct posix_mq {
37 mqd_t des;
38 struct mq_attr attr;
39 VALUE name;
40 VALUE thread;
41 #ifdef MQD_TO_FD
42 VALUE io;
43 #endif
46 static VALUE cPOSIX_MQ, cAttr;
47 static ID id_new, id_kill, id_fileno;
48 static ID sym_r, sym_w, sym_rw;
49 static const mqd_t MQD_INVALID = (mqd_t)-1;
51 /* Ruby 1.8.6+ macros (for compatibility with Ruby 1.9) */
52 #ifndef RSTRING_PTR
53 # define RSTRING_PTR(s) (RSTRING(s)->ptr)
54 #endif
55 #ifndef RSTRING_LEN
56 # define RSTRING_LEN(s) (RSTRING(s)->len)
57 #endif
58 #ifndef RSTRUCT_PTR
59 # define RSTRUCT_PTR(s) (RSTRUCT(s)->ptr)
60 #endif
61 #ifndef RSTRUCT_LEN
62 # define RSTRUCT_LEN(s) (RSTRUCT(s)->len)
63 #endif
65 #ifndef HAVE_RB_STR_SET_LEN
66 # ifdef RUBINIUS
67 # define rb_str_set_len(str,len) rb_str_resize(str,len)
68 # else /* 1.8.6 optimized version */
69 /* this is taken from Ruby 1.8.7, 1.8.6 may not have it */
70 static void rb_18_str_set_len(VALUE str, long len)
72 RSTRING(str)->len = len;
73 RSTRING(str)->ptr[len] = '\0';
75 # define rb_str_set_len(str,len) rb_18_str_set_len(str,len)
76 # endif /* ! RUBINIUS */
77 #endif /* !defined(HAVE_RB_STR_SET_LEN) */
79 #ifndef HAVE_RB_STRUCT_ALLOC_NOINIT
80 static VALUE rb_struct_alloc_noinit(VALUE class)
82 return rb_funcall(class, id_new, 0, 0);
84 #endif /* !defined(HAVE_RB_STRUCT_ALLOC_NOINIT) */
86 /* partial emulation of the 1.9 rb_thread_blocking_region under 1.8 */
87 #ifndef HAVE_RB_THREAD_BLOCKING_REGION
88 # include <rubysig.h>
89 # define RUBY_UBF_IO ((rb_unblock_function_t *)-1)
90 typedef void rb_unblock_function_t(void *);
91 typedef VALUE rb_blocking_function_t(void *);
92 static VALUE
93 rb_thread_blocking_region(
94 rb_blocking_function_t *func, void *data1,
95 rb_unblock_function_t *ubf, void *data2)
97 VALUE rv;
99 assert(RUBY_UBF_IO == ubf && "RUBY_UBF_IO required for emulation");
101 TRAP_BEG;
102 rv = func(data1);
103 TRAP_END;
105 return rv;
107 #endif /* ! HAVE_RB_THREAD_BLOCKING_REGION */
109 /* used to pass arguments to mq_open inside blocking region */
110 struct open_args {
111 int argc;
112 const char *name;
113 int oflags;
114 mode_t mode;
115 struct mq_attr attr;
118 /* used to pass arguments to mq_send/mq_receive inside blocking region */
119 struct rw_args {
120 mqd_t des;
121 char *msg_ptr;
122 size_t msg_len;
123 unsigned msg_prio;
124 struct timespec *timeout;
127 /* hope it's there..., TODO: a better version that works in rbx */
128 struct timeval rb_time_interval(VALUE);
130 static struct timespec *convert_timeout(struct timespec *dest, VALUE time)
132 struct timeval tv, now;
134 if (NIL_P(time))
135 return NULL;
137 tv = rb_time_interval(time); /* aggregate return :( */
138 gettimeofday(&now, NULL);
139 dest->tv_sec = now.tv_sec + tv.tv_sec;
140 dest->tv_nsec = (now.tv_usec + tv.tv_usec) * 1000;
142 if (dest->tv_nsec > 1000000000) {
143 dest->tv_nsec -= 1000000000;
144 dest->tv_sec++;
147 return dest;
150 /* runs without GVL */
151 static VALUE xopen(void *ptr)
153 struct open_args *x = ptr;
154 mqd_t rv;
156 switch (x->argc) {
157 case 2: rv = mq_open(x->name, x->oflags); break;
158 case 3: rv = mq_open(x->name, x->oflags, x->mode, NULL); break;
159 case 4: rv = mq_open(x->name, x->oflags, x->mode, &x->attr); break;
160 default: rv = MQD_INVALID;
163 return (VALUE)rv;
166 /* runs without GVL */
167 static VALUE xsend(void *ptr)
169 struct rw_args *x = ptr;
171 if (x->timeout)
172 return (VALUE)mq_timedsend(x->des, x->msg_ptr, x->msg_len,
173 x->msg_prio, x->timeout);
175 return (VALUE)mq_send(x->des, x->msg_ptr, x->msg_len, x->msg_prio);
178 /* runs without GVL */
179 static VALUE xrecv(void *ptr)
181 struct rw_args *x = ptr;
183 if (x->timeout)
184 return (VALUE)mq_timedreceive(x->des, x->msg_ptr, x->msg_len,
185 &x->msg_prio, x->timeout);
187 return (VALUE)mq_receive(x->des, x->msg_ptr, x->msg_len, &x->msg_prio);
190 /* runs without GVL, path resolution may be slow */
191 static VALUE xunlink(void *ptr)
193 VALUE name = (VALUE)ptr;
195 return (VALUE)mq_unlink(RSTRING_PTR(name));
198 /* called by GC */
199 static void mark(void *ptr)
201 struct posix_mq *mq = ptr;
203 rb_gc_mark(mq->name);
204 rb_gc_mark(mq->thread);
205 MQ_IO_MARK(mq);
208 /* called by GC */
209 static void _free(void *ptr)
211 struct posix_mq *mq = ptr;
213 if (mq->des != MQD_INVALID) {
214 /* we ignore errors when gc-ing */
215 int saved_errno = errno;
217 mq_close(mq->des);
218 errno = saved_errno;
220 xfree(ptr);
223 /* automatically called at creation (before initialize) */
224 static VALUE alloc(VALUE klass)
226 struct posix_mq *mq;
227 VALUE rv = Data_Make_Struct(klass, struct posix_mq, mark, _free, mq);
229 mq->des = MQD_INVALID;
230 mq->attr.mq_flags = 0;
231 mq->attr.mq_maxmsg = 0;
232 mq->attr.mq_msgsize = -1;
233 mq->attr.mq_curmsgs = 0;
234 mq->name = Qnil;
235 mq->thread = Qnil;
236 MQ_IO_SET(mq, Qnil);
238 return rv;
241 /* unwraps the posix_mq struct from self */
242 static struct posix_mq *get(VALUE self, int need_valid)
244 struct posix_mq *mq;
246 Data_Get_Struct(self, struct posix_mq, mq);
248 if (need_valid && mq->des == MQD_INVALID)
249 rb_raise(rb_eIOError, "closed queue descriptor");
251 return mq;
254 /* converts the POSIX_MQ::Attr astruct into a struct mq_attr attr */
255 static void attr_from_struct(struct mq_attr *attr, VALUE astruct, int all)
257 VALUE *ptr;
259 if (CLASS_OF(astruct) != cAttr)
260 rb_raise(rb_eArgError, "not a POSIX_MQ::Attr: %s",
261 RSTRING_PTR(rb_inspect(astruct)));
263 ptr = RSTRUCT_PTR(astruct);
265 attr->mq_flags = NUM2LONG(ptr[0]);
267 if (all || !NIL_P(ptr[1]))
268 attr->mq_maxmsg = NUM2LONG(ptr[1]);
269 if (all || !NIL_P(ptr[2]))
270 attr->mq_msgsize = NUM2LONG(ptr[2]);
271 if (!NIL_P(ptr[3]))
272 attr->mq_curmsgs = NUM2LONG(ptr[3]);
276 * call-seq:
277 * POSIX_MQ.new(name [, flags [, mode [, mq_attr]]) => mq
279 * Opens a POSIX message queue given by +name+. +name+ should start
280 * with a slash ("/") for portable applications.
282 * If a Symbol is given in place of integer +flags+, then:
284 * * +:r+ is equivalent to IO::RDONLY
285 * * +:w+ is equivalent to IO::CREAT|IO::WRONLY
286 * * +:rw+ is equivalent to IO::CREAT|IO::RDWR
288 * +mode+ is an integer and only used when IO::CREAT is used.
289 * +mq_attr+ is a POSIX_MQ::Attr and only used if IO::CREAT is used.
290 * If +mq_attr+ is not specified when creating a queue, then the
291 * system defaults will be used.
293 * See the manpage for mq_open(3) for more details on this function.
295 static VALUE init(int argc, VALUE *argv, VALUE self)
297 struct posix_mq *mq = get(self, 0);
298 struct open_args x;
299 VALUE name, oflags, mode, attr;
301 rb_scan_args(argc, argv, "13", &name, &oflags, &mode, &attr);
303 if (TYPE(name) != T_STRING)
304 rb_raise(rb_eArgError, "name must be a string");
306 switch (TYPE(oflags)) {
307 case T_NIL:
308 x.oflags = O_RDONLY;
309 break;
310 case T_SYMBOL:
311 if (oflags == sym_r)
312 x.oflags = O_RDONLY;
313 else if (oflags == sym_w)
314 x.oflags = O_CREAT|O_WRONLY;
315 else if (oflags == sym_rw)
316 x.oflags = O_CREAT|O_RDWR;
317 else
318 rb_raise(rb_eArgError,
319 "symbol must be :r, :w, or :rw: %s",
320 RSTRING_PTR(rb_inspect(oflags)));
321 break;
322 case T_BIGNUM:
323 case T_FIXNUM:
324 x.oflags = NUM2INT(oflags);
325 break;
326 default:
327 rb_raise(rb_eArgError, "flags must be an int, :r, :w, or :wr");
330 x.name = RSTRING_PTR(name);
331 x.argc = 2;
333 switch (TYPE(mode)) {
334 case T_FIXNUM:
335 x.argc = 3;
336 x.mode = NUM2UINT(mode);
337 break;
338 case T_NIL:
339 if (x.oflags & O_CREAT) {
340 x.argc = 3;
341 x.mode = 0666;
343 break;
344 default:
345 rb_raise(rb_eArgError, "mode not an integer");
348 switch (TYPE(attr)) {
349 case T_STRUCT:
350 x.argc = 4;
351 attr_from_struct(&x.attr, attr, 1);
353 /* principle of least surprise */
354 if (x.attr.mq_flags & O_NONBLOCK)
355 x.oflags |= O_NONBLOCK;
356 break;
357 case T_NIL:
358 break;
359 default:
360 rb_raise(rb_eArgError, "attr must be a POSIX_MQ::Attr: %s",
361 RSTRING_PTR(rb_inspect(attr)));
364 mq->des = (mqd_t)rb_thread_blocking_region(xopen, &x, RUBY_UBF_IO, 0);
365 if (mq->des == MQD_INVALID)
366 rb_sys_fail("mq_open");
368 mq->name = rb_str_dup(name);
369 if (x.oflags & O_NONBLOCK)
370 mq->attr.mq_flags = O_NONBLOCK;
372 return self;
376 * call-seq:
377 * POSIX_MQ.unlink(name) => 1
379 * Unlinks the message queue given by +name+. The queue will be destroyed
380 * when the last process with the queue open closes its queue descriptors.
382 static VALUE s_unlink(VALUE self, VALUE name)
384 mqd_t rv;
385 void *ptr = (void *)name;
387 if (TYPE(name) != T_STRING)
388 rb_raise(rb_eArgError, "argument must be a string");
390 rv = (mqd_t)rb_thread_blocking_region(xunlink, ptr, RUBY_UBF_IO, 0);
391 if (rv == MQD_INVALID)
392 rb_sys_fail("mq_unlink");
394 return INT2NUM(1);
398 * call-seq:
399 * mq.unlink => mq
401 * Unlinks the message queue to prevent other processes from accessing it.
402 * All existing queue descriptors to this queue including those opened by
403 * other processes are unaffected. The queue will only be destroyed
404 * when the last process with open descriptors to this queue closes
405 * the descriptors.
407 static VALUE _unlink(VALUE self)
409 struct posix_mq *mq = get(self, 0);
410 mqd_t rv;
411 void *ptr = (void *)mq->name;
413 assert(TYPE(mq->name) == T_STRING && "mq->name is not a string");
415 rv = (mqd_t)rb_thread_blocking_region(xunlink, ptr, RUBY_UBF_IO, 0);
416 if (rv == MQD_INVALID)
417 rb_sys_fail("mq_unlink");
419 return self;
422 static void setup_send_buffer(struct rw_args *x, VALUE buffer)
424 buffer = rb_obj_as_string(buffer);
425 x->msg_ptr = RSTRING_PTR(buffer);
426 x->msg_len = (size_t)RSTRING_LEN(buffer);
430 * call-seq:
431 * mq.send(string [,priority[, timeout]]) => nil
433 * Inserts the given +string+ into the message queue with an optional,
434 * unsigned integer +priority+. If the optional +timeout+ is specified,
435 * then Errno::ETIMEDOUT will be raised if the operation cannot complete
436 * before +timeout+ seconds has elapsed. Without +timeout+, this method
437 * may block until the queue is writable.
439 static VALUE _send(int argc, VALUE *argv, VALUE self)
441 struct posix_mq *mq = get(self, 1);
442 struct rw_args x;
443 VALUE buffer, prio, timeout;
444 mqd_t rv;
445 struct timespec expire;
447 rb_scan_args(argc, argv, "12", &buffer, &prio, &timeout);
449 setup_send_buffer(&x, buffer);
450 x.des = mq->des;
451 x.timeout = convert_timeout(&expire, timeout);
452 x.msg_prio = NIL_P(prio) ? 0 : NUM2UINT(prio);
454 if (mq->attr.mq_flags & O_NONBLOCK)
455 rv = (mqd_t)xsend(&x);
456 else
457 rv = (mqd_t)rb_thread_blocking_region(xsend, &x,
458 RUBY_UBF_IO, 0);
459 if (rv == MQD_INVALID)
460 rb_sys_fail("mq_send");
462 return Qnil;
466 * call-seq:
467 * mq << string => mq
469 * Inserts the given +string+ into the message queue with a
470 * default priority of 0 and no timeout.
472 static VALUE send0(VALUE self, VALUE buffer)
474 struct posix_mq *mq = get(self, 1);
475 struct rw_args x;
476 mqd_t rv;
478 setup_send_buffer(&x, buffer);
479 x.des = mq->des;
480 x.timeout = NULL;
481 x.msg_prio = 0;
483 rv = (mqd_t)rb_thread_blocking_region(xsend, &x, RUBY_UBF_IO, 0);
484 if (rv == MQD_INVALID)
485 rb_sys_fail("mq_send");
487 return self;
490 #ifdef MQD_TO_FD
492 * call-seq:
493 * mq.to_io => IO
495 * Returns an IO.select-able +IO+ object. This method is only available
496 * under Linux and is not intended to be portable.
498 static VALUE to_io(VALUE self)
500 struct posix_mq *mq = get(self, 1);
501 int fd = MQD_TO_FD(mq->des);
503 if (NIL_P(mq->io))
504 mq->io = rb_funcall(rb_cIO, id_new, 1, INT2NUM(fd));
506 return mq->io;
508 #endif
510 static VALUE _receive(int wantarray, int argc, VALUE *argv, VALUE self);
513 * call-seq:
514 * mq.receive([buffer, [timeout]]) => [ message, priority ]
516 * Takes the highest priority message off the queue and returns
517 * an array containing the message as a String and the Integer
518 * priority of the message.
520 * If the optional +buffer+ is present, then it must be a String
521 * which will receive the data.
523 * If the optional +timeout+ is present, then it may be a Float
524 * or Integer specifying the timeout in seconds. Errno::ETIMEDOUT
525 * will be raised if +timeout+ has elapsed and there are no messages
526 * in the queue.
528 static VALUE receive(int argc, VALUE *argv, VALUE self)
530 return _receive(1, argc, argv, self);
534 * call-seq:
535 * mq.shift([buffer, [timeout]]) => message
537 * Takes the highest priority message off the queue and returns
538 * the message as a String.
540 * If the optional +buffer+ is present, then it must be a String
541 * which will receive the data.
543 * If the optional +timeout+ is present, then it may be a Float
544 * or Integer specifying the timeout in seconds. Errno::ETIMEDOUT
545 * will be raised if +timeout+ has elapsed and there are no messages
546 * in the queue.
548 static VALUE shift(int argc, VALUE *argv, VALUE self)
550 return _receive(0, argc, argv, self);
553 static VALUE _receive(int wantarray, int argc, VALUE *argv, VALUE self)
555 struct posix_mq *mq = get(self, 1);
556 struct rw_args x;
557 VALUE buffer, timeout;
558 ssize_t r;
559 struct timespec expire;
561 if (mq->attr.mq_msgsize < 0) {
562 if (mq_getattr(mq->des, &mq->attr) < 0)
563 rb_sys_fail("mq_getattr");
566 rb_scan_args(argc, argv, "02", &buffer, &timeout);
567 x.timeout = convert_timeout(&expire, timeout);
569 if (NIL_P(buffer)) {
570 buffer = rb_str_new(0, mq->attr.mq_msgsize);
571 } else {
572 StringValue(buffer);
573 rb_str_modify(buffer);
574 rb_str_resize(buffer, mq->attr.mq_msgsize);
576 OBJ_TAINT(buffer);
577 x.msg_ptr = RSTRING_PTR(buffer);
578 x.msg_len = (size_t)mq->attr.mq_msgsize;
579 x.des = mq->des;
581 if (mq->attr.mq_flags & O_NONBLOCK) {
582 r = (ssize_t)xrecv(&x);
583 } else {
584 r = (ssize_t)rb_thread_blocking_region(xrecv, &x,
585 RUBY_UBF_IO, 0);
587 if (r < 0)
588 rb_sys_fail("mq_receive");
590 rb_str_set_len(buffer, r);
592 if (wantarray)
593 return rb_ary_new3(2, buffer, UINT2NUM(x.msg_prio));
594 return buffer;
598 * call-seq:
599 * mq.attr => mq_attr
601 * Returns a POSIX_MQ::Attr struct containing the attributes
602 * of the message queue. See the mq_getattr(3) manpage for
603 * more details.
605 static VALUE getattr(VALUE self)
607 struct posix_mq *mq = get(self, 1);
608 VALUE astruct;
609 VALUE *ptr;
611 if (mq_getattr(mq->des, &mq->attr) < 0)
612 rb_sys_fail("mq_getattr");
614 astruct = rb_struct_alloc_noinit(cAttr);
615 ptr = RSTRUCT_PTR(astruct);
616 ptr[0] = LONG2NUM(mq->attr.mq_flags);
617 ptr[1] = LONG2NUM(mq->attr.mq_maxmsg);
618 ptr[2] = LONG2NUM(mq->attr.mq_msgsize);
619 ptr[3] = LONG2NUM(mq->attr.mq_curmsgs);
621 return astruct;
625 * call-seq:
626 * mq.attr = POSIX_MQ::Attr(IO::NONBLOCK) => mq_attr
628 * Only the IO::NONBLOCK flag may be set or unset (zero) in this manner.
629 * See the mq_setattr(3) manpage for more details.
631 * Consider using the POSIX_MQ#nonblock= method as it is easier and
632 * more natural to use.
634 static VALUE setattr(VALUE self, VALUE astruct)
636 struct posix_mq *mq = get(self, 1);
637 struct mq_attr newattr;
639 attr_from_struct(&newattr, astruct, 0);
641 if (mq_setattr(mq->des, &newattr, NULL) < 0)
642 rb_sys_fail("mq_setattr");
644 return astruct;
648 * call-seq:
649 * mq.close => nil
651 * Closes the underlying message queue descriptor.
652 * If this descriptor had a registered notification request, the request
653 * will be removed so another descriptor or process may register a
654 * notification request. Message queue descriptors are automatically
655 * closed by garbage collection.
657 static VALUE _close(VALUE self)
659 struct posix_mq *mq = get(self, 1);
661 if (mq_close(mq->des) < 0)
662 rb_sys_fail("mq_close");
664 mq->des = MQD_INVALID;
665 MQ_IO_SET(mq, Qnil);
667 return Qnil;
671 * call-seq:
672 * mq.closed? => true or false
674 * Returns +true+ if the message queue descriptor is closed and therefore
675 * unusable, otherwise +false+
677 static VALUE closed(VALUE self)
679 struct posix_mq *mq = get(self, 0);
681 return mq->des == MQD_INVALID ? Qtrue : Qfalse;
685 * call-seq:
686 * mq.name => string
688 * Returns the string name of message queue associated with +mq+
690 static VALUE name(VALUE self)
692 struct posix_mq *mq = get(self, 0);
694 return mq->name;
697 static int lookup_sig(VALUE sig)
699 static VALUE list;
700 const char *ptr;
701 long len;
703 sig = rb_obj_as_string(sig);
704 len = RSTRING_LEN(sig);
705 ptr = RSTRING_PTR(sig);
707 if (len > 3 && !memcmp("SIG", ptr, 3))
708 sig = rb_str_new(ptr + 3, len - 3);
710 if (!list) {
711 VALUE mSignal = rb_define_module("Signal"""); /* avoid RDoc */
713 list = rb_funcall(mSignal, rb_intern("list"), 0, 0);
714 rb_global_variable(&list);
717 sig = rb_hash_aref(list, sig);
718 if (NIL_P(sig))
719 rb_raise(rb_eArgError, "invalid signal: %s\n",
720 RSTRING_PTR(rb_inspect(sig)));
722 return NUM2INT(sig);
725 /* we spawn a thread just to write ONE byte into an fd (usually a pipe) */
726 static void thread_notify_fd(union sigval sv)
728 int fd = sv.sival_int;
730 while ((write(fd, "", 1) < 0) && (errno == EINTR || errno == EAGAIN));
733 static void setup_notify_io(struct sigevent *not, VALUE io)
735 VALUE fileno = rb_funcall(io, id_fileno, 0, 0);
736 int fd = NUM2INT(fileno);
737 pthread_attr_t attr;
738 int e;
740 if ((e = pthread_attr_init(&attr)))
741 goto err;
742 if ((e = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)))
743 goto err;
744 #ifdef PTHREAD_STACK_MIN
745 (void)pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
746 #else
747 # warning PTHREAD_STACK_MIN not available,
748 #endif
749 not->sigev_notify = SIGEV_THREAD;
750 not->sigev_notify_function = thread_notify_fd;
751 not->sigev_notify_attributes = &attr;
752 not->sigev_value.sival_int = fd;
753 return;
754 err:
755 rb_raise(rb_eRuntimeError, "pthread failure: %s\n", strerror(e));
759 * call-seq:
760 * mq.notify = signal => signal
762 * Registers the notification request to deliver a given +signal+
763 * to the current process when message is received.
764 * If +signal+ is +nil+, it will unregister and disable the notification
765 * request to allow other processes to register a request.
766 * If +signal+ is +false+, it will register a no-op notification request
767 * which will prevent other processes from registering a notification.
768 * If +signal+ is an +IO+ object, it will spawn a thread upon the
769 * arrival of the next message and write one "\\0" byte to the file
770 * descriptor belonging to that IO object.
771 * Only one process may have a notification request for a queue
772 * at a time, Errno::EBUSY will be raised if there is already
773 * a notification request registration for the queue.
775 * Notifications are only fired once and processes must reregister
776 * for subsequent notifications.
778 * For readers of the mq_notify(3) manpage, passing +false+
779 * is equivalent to SIGEV_NONE, and passing +nil+ is equivalent
780 * of passing a NULL notification pointer to mq_notify(3).
782 static VALUE setnotify(VALUE self, VALUE arg)
784 struct posix_mq *mq = get(self, 1);
785 struct sigevent not;
786 struct sigevent * notification = &not;
787 VALUE rv = arg;
789 if (!NIL_P(mq->thread)) {
790 rb_funcall(mq->thread, id_kill, 0, 0);
791 mq->thread = Qnil;
793 not.sigev_notify = SIGEV_SIGNAL;
795 switch (TYPE(arg)) {
796 case T_FALSE:
797 not.sigev_notify = SIGEV_NONE;
798 break;
799 case T_NIL:
800 notification = NULL;
801 break;
802 case T_FIXNUM:
803 not.sigev_signo = NUM2INT(arg);
804 break;
805 case T_SYMBOL:
806 case T_STRING:
807 not.sigev_signo = lookup_sig(arg);
808 rv = INT2NUM(not.sigev_signo);
809 break;
810 case T_FILE:
811 setup_notify_io(&not, arg);
812 break;
813 default:
814 /* maybe support Proc+thread via sigev_notify_function.. */
815 rb_raise(rb_eArgError, "must be a signal or nil");
818 if (mq_notify(mq->des, notification) < 0)
819 rb_sys_fail("mq_notify");
821 return rv;
825 * call-seq:
826 * mq.nonblock? => true or false
828 * Returns the current non-blocking state of the message queue descriptor.
830 static VALUE getnonblock(VALUE self)
832 struct posix_mq *mq = get(self, 1);
834 return mq->attr.mq_flags & O_NONBLOCK ? Qtrue : Qfalse;
838 * call-seq:
839 * mq.nonblock = boolean => boolean
841 * Enables or disables non-blocking operation for the message queue
842 * descriptor. Errno::EAGAIN will be raised in situations where
843 * the queue would block. This is not compatible with +timeout+
844 * arguments to POSIX_MQ#send and POSIX_MQ#receive.
846 static VALUE setnonblock(VALUE self, VALUE nb)
848 struct mq_attr newattr;
849 struct posix_mq *mq = get(self, 1);
851 if (nb == Qtrue)
852 newattr.mq_flags = O_NONBLOCK;
853 else if (nb == Qfalse)
854 newattr.mq_flags = 0;
855 else
856 rb_raise(rb_eArgError, "must be true or false");
858 if (mq_setattr(mq->des, &newattr, &mq->attr) < 0)
859 rb_sys_fail("mq_setattr");
861 mq->attr.mq_flags = newattr.mq_flags;
863 return nb;
866 /* :nodoc: */
867 static VALUE setnotifythread(VALUE self, VALUE thread)
869 struct posix_mq *mq = get(self, 1);
871 mq->thread = thread;
872 return thread;
875 void Init_posix_mq_ext(void)
877 cPOSIX_MQ = rb_define_class("POSIX_MQ", rb_cObject);
878 rb_define_alloc_func(cPOSIX_MQ, alloc);
879 cAttr = rb_const_get(cPOSIX_MQ, rb_intern("Attr"));
882 * The maximum number of open message descriptors supported
883 * by the system. This may be -1, in which case it is dynamically
884 * set at runtime. Consult your operating system documentation
885 * for system-specific information about this.
887 rb_define_const(cPOSIX_MQ, "OPEN_MAX",
888 LONG2NUM(sysconf(_SC_MQ_OPEN_MAX)));
891 * The maximum priority that may be specified for POSIX_MQ#send
892 * On POSIX-compliant systems, this is at least 31, but some
893 * systems allow higher limits.
894 * The minimum priority is always zero.
896 rb_define_const(cPOSIX_MQ, "PRIO_MAX",
897 LONG2NUM(sysconf(_SC_MQ_PRIO_MAX)));
899 rb_define_singleton_method(cPOSIX_MQ, "unlink", s_unlink, 1);
901 rb_define_method(cPOSIX_MQ, "initialize", init, -1);
902 rb_define_method(cPOSIX_MQ, "send", _send, -1);
903 rb_define_method(cPOSIX_MQ, "<<", send0, 1);
904 rb_define_method(cPOSIX_MQ, "receive", receive, -1);
905 rb_define_method(cPOSIX_MQ, "shift", shift, -1);
906 rb_define_method(cPOSIX_MQ, "attr", getattr, 0);
907 rb_define_method(cPOSIX_MQ, "attr=", setattr, 1);
908 rb_define_method(cPOSIX_MQ, "close", _close, 0);
909 rb_define_method(cPOSIX_MQ, "closed?", closed, 0);
910 rb_define_method(cPOSIX_MQ, "unlink", _unlink, 0);
911 rb_define_method(cPOSIX_MQ, "name", name, 0);
912 rb_define_method(cPOSIX_MQ, "notify=", setnotify, 1);
913 rb_define_method(cPOSIX_MQ, "nonblock=", setnonblock, 1);
914 rb_define_method(cPOSIX_MQ, "notify_thread=", setnotifythread, 1);
915 rb_define_method(cPOSIX_MQ, "nonblock?", getnonblock, 0);
916 #ifdef MQD_TO_FD
917 rb_define_method(cPOSIX_MQ, "to_io", to_io, 0);
918 #endif
920 id_new = rb_intern("new");
921 id_kill = rb_intern("kill");
922 id_fileno = rb_intern("fileno");
923 sym_r = ID2SYM(rb_intern("r"));
924 sym_w = ID2SYM(rb_intern("w"));
925 sym_rw = ID2SYM(rb_intern("rw"));