MRI 1.8 does not have rb_str_flush
[ruby_posix_mq.git] / ext / posix_mq / posix_mq.c
blob536836807b2ad4364a977155977aac3086918741
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 long msgsize;
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;
219 mq->des = MQD_INVALID;
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->msgsize = -1;
231 mq->name = Qnil;
232 mq->thread = Qnil;
233 MQ_IO_SET(mq, Qnil);
235 return rv;
238 /* unwraps the posix_mq struct from self */
239 static struct posix_mq *get(VALUE self, int need_valid)
241 struct posix_mq *mq;
243 Data_Get_Struct(self, struct posix_mq, mq);
245 if (need_valid && mq->des == MQD_INVALID)
246 rb_raise(rb_eIOError, "closed queue descriptor");
248 return mq;
251 /* converts the POSIX_MQ::Attr astruct into a struct mq_attr attr */
252 static void attr_from_struct(struct mq_attr *attr, VALUE astruct, int all)
254 VALUE *ptr;
256 if (CLASS_OF(astruct) != cAttr)
257 rb_raise(rb_eArgError, "not a POSIX_MQ::Attr: %s",
258 RSTRING_PTR(rb_inspect(astruct)));
260 ptr = RSTRUCT_PTR(astruct);
262 attr->mq_flags = NUM2LONG(ptr[0]);
264 if (all || !NIL_P(ptr[1]))
265 attr->mq_maxmsg = NUM2LONG(ptr[1]);
266 if (all || !NIL_P(ptr[2]))
267 attr->mq_msgsize = NUM2LONG(ptr[2]);
268 if (!NIL_P(ptr[3]))
269 attr->mq_curmsgs = NUM2LONG(ptr[3]);
273 * call-seq:
274 * POSIX_MQ.new(name [, flags [, mode [, mq_attr]]) => mq
276 * Opens a POSIX message queue given by +name+. +name+ should start
277 * with a slash ("/") for portable applications.
279 * If a Symbol is given in place of integer +flags+, then:
281 * * +:r+ is equivalent to IO::RDONLY
282 * * +:w+ is equivalent to IO::CREAT|IO::WRONLY
283 * * +:rw+ is equivalent to IO::CREAT|IO::RDWR
285 * +mode+ is an integer and only used when IO::CREAT is used.
286 * +mq_attr+ is a POSIX_MQ::Attr and only used if IO::CREAT is used.
287 * If +mq_attr+ is not specified when creating a queue, then the
288 * system defaults will be used.
290 * See the manpage for mq_open(3) for more details on this function.
292 static VALUE init(int argc, VALUE *argv, VALUE self)
294 struct posix_mq *mq = get(self, 0);
295 struct open_args x;
296 VALUE name, oflags, mode, attr;
298 rb_scan_args(argc, argv, "13", &name, &oflags, &mode, &attr);
300 if (TYPE(name) != T_STRING)
301 rb_raise(rb_eArgError, "name must be a string");
303 switch (TYPE(oflags)) {
304 case T_NIL:
305 x.oflags = O_RDONLY;
306 break;
307 case T_SYMBOL:
308 if (oflags == sym_r)
309 x.oflags = O_RDONLY;
310 else if (oflags == sym_w)
311 x.oflags = O_CREAT|O_WRONLY;
312 else if (oflags == sym_rw)
313 x.oflags = O_CREAT|O_RDWR;
314 else
315 rb_raise(rb_eArgError,
316 "symbol must be :r, :w, or :rw: %s",
317 RSTRING_PTR(rb_inspect(oflags)));
318 break;
319 case T_BIGNUM:
320 case T_FIXNUM:
321 x.oflags = NUM2INT(oflags);
322 break;
323 default:
324 rb_raise(rb_eArgError, "flags must be an int, :r, :w, or :wr");
327 x.name = RSTRING_PTR(name);
328 x.argc = 2;
330 switch (TYPE(mode)) {
331 case T_FIXNUM:
332 x.argc = 3;
333 x.mode = NUM2UINT(mode);
334 break;
335 case T_NIL:
336 if (x.oflags & O_CREAT) {
337 x.argc = 3;
338 x.mode = 0666;
340 break;
341 default:
342 rb_raise(rb_eArgError, "mode not an integer");
345 switch (TYPE(attr)) {
346 case T_STRUCT:
347 x.argc = 4;
348 attr_from_struct(&x.attr, attr, 1);
350 /* principle of least surprise */
351 if (x.attr.mq_flags & O_NONBLOCK)
352 x.oflags |= O_NONBLOCK;
353 break;
354 case T_NIL:
355 break;
356 default:
357 rb_raise(rb_eArgError, "attr must be a POSIX_MQ::Attr: %s",
358 RSTRING_PTR(rb_inspect(attr)));
361 mq->des = (mqd_t)rb_thread_blocking_region(xopen, &x, RUBY_UBF_IO, 0);
362 if (mq->des == MQD_INVALID)
363 rb_sys_fail("mq_open");
365 mq->name = rb_str_dup(name);
367 return self;
371 * call-seq:
372 * POSIX_MQ.unlink(name) => 1
374 * Unlinks the message queue given by +name+. The queue will be destroyed
375 * when the last process with the queue open closes its queue descriptors.
377 static VALUE s_unlink(VALUE self, VALUE name)
379 mqd_t rv;
380 void *ptr = (void *)name;
382 if (TYPE(name) != T_STRING)
383 rb_raise(rb_eArgError, "argument must be a string");
385 rv = (mqd_t)rb_thread_blocking_region(xunlink, ptr, RUBY_UBF_IO, 0);
386 if (rv == MQD_INVALID)
387 rb_sys_fail("mq_unlink");
389 return INT2NUM(1);
393 * call-seq:
394 * mq.unlink => mq
396 * Unlinks the message queue to prevent other processes from accessing it.
397 * All existing queue descriptors to this queue including those opened by
398 * other processes are unaffected. The queue will only be destroyed
399 * when the last process with open descriptors to this queue closes
400 * the descriptors.
402 static VALUE _unlink(VALUE self)
404 struct posix_mq *mq = get(self, 0);
405 mqd_t rv;
406 void *ptr = (void *)mq->name;
408 assert(TYPE(mq->name) == T_STRING && "mq->name is not a string");
410 rv = (mqd_t)rb_thread_blocking_region(xunlink, ptr, RUBY_UBF_IO, 0);
411 if (rv == MQD_INVALID)
412 rb_sys_fail("mq_unlink");
414 return self;
417 static void setup_send_buffer(struct rw_args *x, VALUE buffer)
419 buffer = rb_obj_as_string(buffer);
420 x->msg_ptr = RSTRING_PTR(buffer);
421 x->msg_len = (size_t)RSTRING_LEN(buffer);
425 * call-seq:
426 * mq.send(string [,priority[, timeout]]) => nil
428 * Inserts the given +string+ into the message queue with an optional,
429 * unsigned integer +priority+. If the optional +timeout+ is specified,
430 * then Errno::ETIMEDOUT will be raised if the operation cannot complete
431 * before +timeout+ seconds has elapsed. Without +timeout+, this method
432 * may block until the queue is writable.
434 static VALUE _send(int argc, VALUE *argv, VALUE self)
436 struct posix_mq *mq = get(self, 1);
437 struct rw_args x;
438 VALUE buffer, prio, timeout;
439 mqd_t rv;
440 struct timespec expire;
442 rb_scan_args(argc, argv, "12", &buffer, &prio, &timeout);
444 setup_send_buffer(&x, buffer);
445 x.des = mq->des;
446 x.timeout = convert_timeout(&expire, timeout);
447 x.msg_prio = NIL_P(prio) ? 0 : NUM2UINT(prio);
449 rv = (mqd_t)rb_thread_blocking_region(xsend, &x, RUBY_UBF_IO, 0);
450 if (rv == MQD_INVALID)
451 rb_sys_fail("mq_send");
453 return Qnil;
457 * call-seq:
458 * mq << string => mq
460 * Inserts the given +string+ into the message queue with a
461 * default priority of 0 and no timeout.
463 static VALUE send0(VALUE self, VALUE buffer)
465 struct posix_mq *mq = get(self, 1);
466 struct rw_args x;
467 mqd_t rv;
469 setup_send_buffer(&x, buffer);
470 x.des = mq->des;
471 x.timeout = NULL;
472 x.msg_prio = 0;
474 rv = (mqd_t)rb_thread_blocking_region(xsend, &x, RUBY_UBF_IO, 0);
475 if (rv == MQD_INVALID)
476 rb_sys_fail("mq_send");
478 return self;
481 #ifdef MQD_TO_FD
483 * call-seq:
484 * mq.to_io => IO
486 * Returns an IO.select-able +IO+ object. This method is only available
487 * under Linux and is not intended to be portable.
489 static VALUE to_io(VALUE self)
491 struct posix_mq *mq = get(self, 1);
492 int fd = MQD_TO_FD(mq->des);
494 if (NIL_P(mq->io))
495 mq->io = rb_funcall(rb_cIO, id_new, 1, INT2NUM(fd));
497 return mq->io;
499 #endif
501 static void get_msgsize(struct posix_mq *mq)
503 struct mq_attr attr;
505 if (mq_getattr(mq->des, &attr) < 0)
506 rb_sys_fail("mq_getattr");
508 mq->msgsize = attr.mq_msgsize;
511 static VALUE _receive(int wantarray, int argc, VALUE *argv, VALUE self);
514 * call-seq:
515 * mq.receive([buffer, [timeout]]) => [ message, priority ]
517 * Takes the highest priority message off the queue and returns
518 * an array containing the message as a String and the Integer
519 * priority of the message.
521 * If the optional +buffer+ is present, then it must be a String
522 * which will receive the data.
524 * If the optional +timeout+ is present, then it may be a Float
525 * or Integer specifying the timeout in seconds. Errno::ETIMEDOUT
526 * will be raised if +timeout+ has elapsed and there are no messages
527 * in the queue.
529 static VALUE receive(int argc, VALUE *argv, VALUE self)
531 return _receive(1, argc, argv, self);
535 * call-seq:
536 * mq.shift([buffer, [timeout]]) => message
538 * Takes the highest priority message off the queue and returns
539 * the message as a String.
541 * If the optional +buffer+ is present, then it must be a String
542 * which will receive the data.
544 * If the optional +timeout+ is present, then it may be a Float
545 * or Integer specifying the timeout in seconds. Errno::ETIMEDOUT
546 * will be raised if +timeout+ has elapsed and there are no messages
547 * in the queue.
549 static VALUE shift(int argc, VALUE *argv, VALUE self)
551 return _receive(0, argc, argv, self);
554 static VALUE _receive(int wantarray, int argc, VALUE *argv, VALUE self)
556 struct posix_mq *mq = get(self, 1);
557 struct rw_args x;
558 VALUE buffer, timeout;
559 ssize_t r;
560 struct timespec expire;
562 if (mq->msgsize < 0)
563 get_msgsize(mq);
565 rb_scan_args(argc, argv, "02", &buffer, &timeout);
566 x.timeout = convert_timeout(&expire, timeout);
568 if (NIL_P(buffer)) {
569 buffer = rb_str_new(0, mq->msgsize);
570 } else {
571 StringValue(buffer);
572 rb_str_modify(buffer);
573 rb_str_resize(buffer, mq->msgsize);
575 OBJ_TAINT(buffer);
576 x.msg_ptr = RSTRING_PTR(buffer);
577 x.msg_len = (size_t)mq->msgsize;
578 x.des = mq->des;
580 r = (ssize_t)rb_thread_blocking_region(xrecv, &x, RUBY_UBF_IO, 0);
581 if (r < 0)
582 rb_sys_fail("mq_receive");
584 rb_str_set_len(buffer, r);
586 if (wantarray)
587 return rb_ary_new3(2, buffer, UINT2NUM(x.msg_prio));
588 return buffer;
592 * call-seq:
593 * mq.attr => mq_attr
595 * Returns a POSIX_MQ::Attr struct containing the attributes
596 * of the message queue. See the mq_getattr(3) manpage for
597 * more details.
599 static VALUE getattr(VALUE self)
601 struct posix_mq *mq = get(self, 1);
602 struct mq_attr attr;
603 VALUE astruct;
604 VALUE *ptr;
606 if (mq_getattr(mq->des, &attr) < 0)
607 rb_sys_fail("mq_getattr");
609 astruct = rb_struct_alloc_noinit(cAttr);
610 ptr = RSTRUCT_PTR(astruct);
611 ptr[0] = LONG2NUM(attr.mq_flags);
612 ptr[1] = LONG2NUM(attr.mq_maxmsg);
613 ptr[2] = LONG2NUM(attr.mq_msgsize);
614 ptr[3] = LONG2NUM(attr.mq_curmsgs);
616 return astruct;
620 * call-seq:
621 * mq.attr = POSIX_MQ::Attr(IO::NONBLOCK) => mq_attr
623 * Only the IO::NONBLOCK flag may be set or unset (zero) in this manner.
624 * See the mq_setattr(3) manpage for more details.
626 * Consider using the POSIX_MQ#nonblock= method as it is easier and
627 * more natural to use.
629 static VALUE setattr(VALUE self, VALUE astruct)
631 struct posix_mq *mq = get(self, 1);
632 struct mq_attr newattr;
634 attr_from_struct(&newattr, astruct, 0);
636 if (mq_setattr(mq->des, &newattr, NULL) < 0)
637 rb_sys_fail("mq_setattr");
639 return astruct;
643 * call-seq:
644 * mq.close => nil
646 * Closes the underlying message queue descriptor.
647 * If this descriptor had a registered notification request, the request
648 * will be removed so another descriptor or process may register a
649 * notification request. Message queue descriptors are automatically
650 * closed by garbage collection.
652 static VALUE _close(VALUE self)
654 struct posix_mq *mq = get(self, 1);
656 if (mq_close(mq->des) < 0)
657 rb_sys_fail("mq_close");
659 mq->des = MQD_INVALID;
660 MQ_IO_SET(mq, Qnil);
662 return Qnil;
666 * call-seq:
667 * mq.closed? => true or false
669 * Returns +true+ if the message queue descriptor is closed and therefore
670 * unusable, otherwise +false+
672 static VALUE closed(VALUE self)
674 struct posix_mq *mq = get(self, 0);
676 return mq->des == MQD_INVALID ? Qtrue : Qfalse;
680 * call-seq:
681 * mq.name => string
683 * Returns the string name of message queue associated with +mq+
685 static VALUE name(VALUE self)
687 struct posix_mq *mq = get(self, 0);
689 return mq->name;
692 static int lookup_sig(VALUE sig)
694 static VALUE list;
695 const char *ptr;
696 long len;
698 sig = rb_obj_as_string(sig);
699 len = RSTRING_LEN(sig);
700 ptr = RSTRING_PTR(sig);
702 if (len > 3 && !memcmp("SIG", ptr, 3))
703 sig = rb_str_new(ptr + 3, len - 3);
705 if (!list) {
706 VALUE mSignal = rb_define_module("Signal"""); /* avoid RDoc */
708 list = rb_funcall(mSignal, rb_intern("list"), 0, 0);
709 rb_global_variable(&list);
712 sig = rb_hash_aref(list, sig);
713 if (NIL_P(sig))
714 rb_raise(rb_eArgError, "invalid signal: %s\n",
715 RSTRING_PTR(rb_inspect(sig)));
717 return NUM2INT(sig);
720 /* we spawn a thread just to write ONE byte into an fd (usually a pipe) */
721 static void thread_notify_fd(union sigval sv)
723 int fd = sv.sival_int;
725 while ((write(fd, "", 1) < 0) && (errno == EINTR || errno == EAGAIN));
728 static void setup_notify_io(struct sigevent *not, VALUE io)
730 VALUE fileno = rb_funcall(io, id_fileno, 0, 0);
731 int fd = NUM2INT(fileno);
732 pthread_attr_t attr;
733 int e;
735 if ((e = pthread_attr_init(&attr)))
736 goto err;
737 if ((e = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)))
738 goto err;
739 #ifdef PTHREAD_STACK_MIN
740 (void)pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
741 #else
742 # warning PTHREAD_STACK_MIN not available,
743 #endif
744 not->sigev_notify = SIGEV_THREAD;
745 not->sigev_notify_function = thread_notify_fd;
746 not->sigev_notify_attributes = &attr;
747 not->sigev_value.sival_int = fd;
748 return;
749 err:
750 rb_raise(rb_eRuntimeError, "pthread failure: %s\n", strerror(e));
754 * call-seq:
755 * mq.notify = signal => signal
757 * Registers the notification request to deliver a given +signal+
758 * to the current process when message is received.
759 * If +signal+ is +nil+, it will unregister and disable the notification
760 * request to allow other processes to register a request.
761 * If +signal+ is +false+, it will register a no-op notification request
762 * which will prevent other processes from registering a notification.
763 * If +signal+ is an +IO+ object, it will spawn a thread upon the
764 * arrival of the next message and write one "\\0" byte to the file
765 * descriptor belonging to that IO object.
766 * Only one process may have a notification request for a queue
767 * at a time, Errno::EBUSY will be raised if there is already
768 * a notification request registration for the queue.
770 * Notifications are only fired once and processes must reregister
771 * for subsequent notifications.
773 * For readers of the mq_notify(3) manpage, passing +false+
774 * is equivalent to SIGEV_NONE, and passing +nil+ is equivalent
775 * of passing a NULL notification pointer to mq_notify(3).
777 static VALUE setnotify(VALUE self, VALUE arg)
779 struct posix_mq *mq = get(self, 1);
780 struct sigevent not;
781 struct sigevent * notification = &not;
782 VALUE rv = arg;
784 if (!NIL_P(mq->thread)) {
785 rb_funcall(mq->thread, id_kill, 0, 0);
786 mq->thread = Qnil;
788 not.sigev_notify = SIGEV_SIGNAL;
790 switch (TYPE(arg)) {
791 case T_FALSE:
792 not.sigev_notify = SIGEV_NONE;
793 break;
794 case T_NIL:
795 notification = NULL;
796 break;
797 case T_FIXNUM:
798 not.sigev_signo = NUM2INT(arg);
799 break;
800 case T_SYMBOL:
801 case T_STRING:
802 not.sigev_signo = lookup_sig(arg);
803 rv = INT2NUM(not.sigev_signo);
804 break;
805 case T_FILE:
806 setup_notify_io(&not, arg);
807 break;
808 default:
809 /* maybe support Proc+thread via sigev_notify_function.. */
810 rb_raise(rb_eArgError, "must be a signal or nil");
813 if (mq_notify(mq->des, notification) < 0)
814 rb_sys_fail("mq_notify");
816 return rv;
820 * call-seq:
821 * mq.nonblock? => true or false
823 * Returns the current non-blocking state of the message queue descriptor.
825 static VALUE getnonblock(VALUE self)
827 struct mq_attr attr;
828 struct posix_mq *mq = get(self, 1);
830 if (mq_getattr(mq->des, &attr) < 0)
831 rb_sys_fail("mq_getattr");
833 mq->msgsize = attr.mq_msgsize; /* optimization */
835 return attr.mq_flags & O_NONBLOCK ? Qtrue : Qfalse;
839 * call-seq:
840 * mq.nonblock = boolean => boolean
842 * Enables or disables non-blocking operation for the message queue
843 * descriptor. Errno::EAGAIN will be raised in situations where
844 * the queue would block. This is not compatible with +timeout+
845 * arguments to POSIX_MQ#send and POSIX_MQ#receive.
847 static VALUE setnonblock(VALUE self, VALUE nb)
849 struct mq_attr newattr, oldattr;
850 struct posix_mq *mq = get(self, 1);
852 if (nb == Qtrue)
853 newattr.mq_flags = O_NONBLOCK;
854 else if (nb == Qfalse)
855 newattr.mq_flags = 0;
856 else
857 rb_raise(rb_eArgError, "must be true or false");
859 if (mq_setattr(mq->des, &newattr, &oldattr) < 0)
860 rb_sys_fail("mq_setattr");
862 mq->msgsize = oldattr.mq_msgsize; /* optimization */
864 return nb;
867 /* :nodoc: */
868 static VALUE setnotifythread(VALUE self, VALUE thread)
870 struct posix_mq *mq = get(self, 1);
872 mq->thread = thread;
873 return thread;
876 void Init_posix_mq_ext(void)
878 cPOSIX_MQ = rb_define_class("POSIX_MQ", rb_cObject);
879 rb_define_alloc_func(cPOSIX_MQ, alloc);
880 cAttr = rb_const_get(cPOSIX_MQ, rb_intern("Attr"));
883 * The maximum number of open message descriptors supported
884 * by the system. This may be -1, in which case it is dynamically
885 * set at runtime. Consult your operating system documentation
886 * for system-specific information about this.
888 rb_define_const(cPOSIX_MQ, "OPEN_MAX",
889 LONG2NUM(sysconf(_SC_MQ_OPEN_MAX)));
892 * The maximum priority that may be specified for POSIX_MQ#send
893 * On POSIX-compliant systems, this is at least 31, but some
894 * systems allow higher limits.
895 * The minimum priority is always zero.
897 rb_define_const(cPOSIX_MQ, "PRIO_MAX",
898 LONG2NUM(sysconf(_SC_MQ_PRIO_MAX)));
900 rb_define_singleton_method(cPOSIX_MQ, "unlink", s_unlink, 1);
902 rb_define_method(cPOSIX_MQ, "initialize", init, -1);
903 rb_define_method(cPOSIX_MQ, "send", _send, -1);
904 rb_define_method(cPOSIX_MQ, "<<", send0, 1);
905 rb_define_method(cPOSIX_MQ, "receive", receive, -1);
906 rb_define_method(cPOSIX_MQ, "shift", shift, -1);
907 rb_define_method(cPOSIX_MQ, "attr", getattr, 0);
908 rb_define_method(cPOSIX_MQ, "attr=", setattr, 1);
909 rb_define_method(cPOSIX_MQ, "close", _close, 0);
910 rb_define_method(cPOSIX_MQ, "closed?", closed, 0);
911 rb_define_method(cPOSIX_MQ, "unlink", _unlink, 0);
912 rb_define_method(cPOSIX_MQ, "name", name, 0);
913 rb_define_method(cPOSIX_MQ, "notify=", setnotify, 1);
914 rb_define_method(cPOSIX_MQ, "nonblock=", setnonblock, 1);
915 rb_define_method(cPOSIX_MQ, "notify_thread=", setnotifythread, 1);
916 rb_define_method(cPOSIX_MQ, "nonblock?", getnonblock, 0);
917 #ifdef MQD_TO_FD
918 rb_define_method(cPOSIX_MQ, "to_io", to_io, 0);
919 #endif
921 id_new = rb_intern("new");
922 id_kill = rb_intern("kill");
923 id_fileno = rb_intern("fileno");
924 sym_r = ID2SYM(rb_intern("r"));
925 sym_w = ID2SYM(rb_intern("w"));
926 sym_rw = ID2SYM(rb_intern("rw"));