75b38e40b2c5a532b2e4dbda05544ae04a26c8f2
1 #define _XOPEN_SOURCE 600
2 #ifdef HAVE_SYS_SELECT_H
3 # include <sys/select.h>
14 # define NUM2TIMET NUM2INT
27 #if defined(__linux__)
28 # define MQD_TO_FD(mqd) (int)(mqd)
29 #elif defined(HAVE___MQ_OSHANDLE) /* FreeBSD */
30 # define MQD_TO_FD(mqd) __mq_oshandle(mqd)
32 # define MQ_IO_MARK(mq) ((void)(0))
33 # define MQ_IO_SET(mq,val) ((void)(0))
34 # define MQ_IO_CLOSE(mq) ((int)(0))
35 # define MQ_IO_NIL_P(mq) ((int)(1))
49 # define MQ_IO_MARK(mq) rb_gc_mark((mq)->io)
50 # define MQ_IO_SET(mq,val) do { (mq)->io = (val); } while (0)
51 # define MQ_IO_NIL_P(mq) NIL_P((mq)->io)
52 static int MQ_IO_CLOSE(struct posix_mq
*mq
)
57 /* not safe during GC */
65 # define PMQ_WANTARRAY (1<<0)
66 # define PMQ_TRY (1<<1)
69 static ID id_new
, id_kill
, id_fileno
, id_divmod
;
70 static ID id_flags
, id_maxmsg
, id_msgsize
, id_curmsgs
;
71 static VALUE sym_r
, sym_w
, sym_rw
;
72 static const mqd_t MQD_INVALID
= (mqd_t
)-1;
74 /* Ruby 1.8.6+ macros (for compatibility with Ruby 1.9) */
76 # define RSTRING_PTR(s) (RSTRING(s)->ptr)
79 # define RSTRING_LEN(s) (RSTRING(s)->len)
82 # define RFLOAT_VALUE(f) (RFLOAT(f)->value)
85 #ifndef HAVE_RB_STR_SET_LEN
86 /* this is taken from Ruby 1.8.7, 1.8.6 may not have it */
88 # error upgrade Rubinius, rb_str_set_len should be available
90 static void rb_18_str_set_len(VALUE str
, long len
)
92 RSTRING(str
)->len
= len
;
93 RSTRING(str
)->ptr
[len
] = '\0';
95 #define rb_str_set_len rb_18_str_set_len
96 #endif /* !defined(HAVE_RB_STR_SET_LEN) */
98 /* partial emulation of the 1.9 rb_thread_blocking_region under 1.8 */
99 #ifndef HAVE_RB_THREAD_BLOCKING_REGION
100 # include <rubysig.h>
101 # define RUBY_UBF_IO ((rb_unblock_function_t *)-1)
102 typedef void rb_unblock_function_t(void *);
103 typedef VALUE
rb_blocking_function_t(void *);
105 rb_thread_blocking_region(
106 rb_blocking_function_t
*func
, void *data1
,
107 rb_unblock_function_t
*ubf
, void *data2
)
111 assert(RUBY_UBF_IO
== ubf
&& "RUBY_UBF_IO required for emulation");
119 #endif /* ! HAVE_RB_THREAD_BLOCKING_REGION */
121 /* used to pass arguments to mq_open inside blocking region */
130 /* used to pass arguments to mq_send/mq_receive inside blocking region */
136 struct timespec
*timeout
;
139 #ifndef HAVE_MQ_TIMEDSEND
141 not_timedsend(mqd_t mqdes
, const char *msg_ptr
,
142 size_t msg_len
, unsigned msg_prio
,
143 const struct timespec
*abs_timeout
)
145 rb_bug("mq_timedsend workaround failed");
148 # define mq_timedsend not_timedsend
150 #ifndef HAVE_MQ_TIMEDRECEIVE
152 not_timedreceive(mqd_t mqdes
, char *msg_ptr
,
153 size_t msg_len
, unsigned *msg_prio
,
154 const struct timespec
*abs_timeout
)
156 rb_bug("mq_timedreceive workaround failed");
159 # define mq_timedreceive not_timedreceive
162 #if defined(HAVE_MQ_TIMEDRECEIVE) && defined(HAVE_MQ_TIMEDSEND)
163 static void num2timespec(struct timespec
*ts
, VALUE t
)
168 ts
->tv_sec
= NUM2TIMET(t
);
173 double val
= RFLOAT_VALUE(t
);
177 ts
->tv_nsec
= (long)(d
* 1e9
+ 0.5);
179 ts
->tv_nsec
= (long)(-d
* 1e9
+ 0.5);
180 if (ts
->tv_nsec
> 0) {
181 ts
->tv_nsec
= 1000000000 - ts
->tv_nsec
;
185 ts
->tv_sec
= (time_t)f
;
187 rb_raise(rb_eRangeError
, "%f out of range", val
);
192 VALUE ary
= rb_funcall(t
, id_divmod
, 1, INT2FIX(1));
194 Check_Type(ary
, T_ARRAY
);
196 ts
->tv_sec
= NUM2TIMET(rb_ary_entry(ary
, 0));
197 f
= rb_ary_entry(ary
, 1);
198 f
= rb_funcall(f
, '*', 1, INT2FIX(1000000000));
199 ts
->tv_nsec
= NUM2LONG(f
);
204 static void num2timespec(struct timespec
*ts
, VALUE t
)
206 rb_raise(rb_eNotImpError
,
207 "mq_timedsend and/or mq_timedreceive missing");
211 static struct timespec
*convert_timeout(struct timespec
*dest
, VALUE t
)
213 struct timespec ts
, now
;
218 num2timespec(&ts
, t
);
219 clock_gettime(CLOCK_REALTIME
, &now
);
220 dest
->tv_sec
= now
.tv_sec
+ ts
.tv_sec
;
221 dest
->tv_nsec
= now
.tv_nsec
+ ts
.tv_nsec
;
223 if (dest
->tv_nsec
> 1000000000) {
224 dest
->tv_nsec
-= 1000000000;
231 /* (may) run without GVL */
232 static VALUE
xopen(void *ptr
)
234 struct open_args
*x
= ptr
;
238 case 2: rv
= mq_open(x
->name
, x
->oflags
); break;
239 case 3: rv
= mq_open(x
->name
, x
->oflags
, x
->mode
, NULL
); break;
240 case 4: rv
= mq_open(x
->name
, x
->oflags
, x
->mode
, &x
->attr
); break;
241 default: rv
= MQD_INVALID
;
247 /* runs without GVL */
248 static VALUE
xsend(void *ptr
)
250 struct rw_args
*x
= ptr
;
253 return (VALUE
)mq_timedsend(x
->des
, x
->msg_ptr
, x
->msg_len
,
254 x
->msg_prio
, x
->timeout
);
256 return (VALUE
)mq_send(x
->des
, x
->msg_ptr
, x
->msg_len
, x
->msg_prio
);
259 /* runs without GVL */
260 static VALUE
xrecv(void *ptr
)
262 struct rw_args
*x
= ptr
;
265 return (VALUE
)mq_timedreceive(x
->des
, x
->msg_ptr
, x
->msg_len
,
266 &x
->msg_prio
, x
->timeout
);
268 return (VALUE
)mq_receive(x
->des
, x
->msg_ptr
, x
->msg_len
, &x
->msg_prio
);
272 static void mark(void *ptr
)
274 struct posix_mq
*mq
= ptr
;
276 rb_gc_mark(mq
->name
);
277 rb_gc_mark(mq
->thread
);
282 static void _free(void *ptr
)
284 struct posix_mq
*mq
= ptr
;
286 if (mq
->des
!= MQD_INVALID
&& MQ_IO_NIL_P(mq
)) {
287 /* we ignore errors when gc-ing */
294 /* automatically called at creation (before initialize) */
295 static VALUE
alloc(VALUE klass
)
298 VALUE rv
= Data_Make_Struct(klass
, struct posix_mq
, mark
, _free
, mq
);
300 mq
->des
= MQD_INVALID
;
301 mq
->attr
.mq_flags
= 0;
302 mq
->attr
.mq_maxmsg
= 0;
303 mq
->attr
.mq_msgsize
= -1;
304 mq
->attr
.mq_curmsgs
= 0;
312 /* unwraps the posix_mq struct from self */
313 static struct posix_mq
*get(VALUE self
, int need_valid
)
317 Data_Get_Struct(self
, struct posix_mq
, mq
);
319 if (need_valid
&& mq
->des
== MQD_INVALID
)
320 rb_raise(rb_eIOError
, "closed queue descriptor");
325 static void check_struct_type(VALUE astruct
)
327 if (CLASS_OF(astruct
) == cAttr
)
329 astruct
= rb_inspect(astruct
);
330 rb_raise(rb_eTypeError
, "not a POSIX_MQ::Attr: %s",
331 StringValuePtr(astruct
));
334 static void rstruct2mqattr(struct mq_attr
*attr
, VALUE astruct
, int all
)
338 check_struct_type(astruct
);
339 attr
->mq_flags
= NUM2LONG(rb_funcall(astruct
, id_flags
, 0));
341 tmp
= rb_funcall(astruct
, id_maxmsg
, 0);
342 if (all
|| !NIL_P(tmp
))
343 attr
->mq_maxmsg
= NUM2LONG(tmp
);
345 tmp
= rb_funcall(astruct
, id_msgsize
, 0);
346 if (all
|| !NIL_P(tmp
))
347 attr
->mq_msgsize
= NUM2LONG(tmp
);
349 tmp
= rb_funcall(astruct
, id_curmsgs
, 0);
351 attr
->mq_curmsgs
= NUM2LONG(tmp
);
356 * POSIX_MQ.new(name [, flags [, mode [, mq_attr]]) => mq
358 * Opens a POSIX message queue given by +name+. +name+ should start
359 * with a slash ("/") for portable applications.
361 * If a Symbol is given in place of integer +flags+, then:
363 * * +:r+ is equivalent to IO::RDONLY
364 * * +:w+ is equivalent to IO::CREAT|IO::WRONLY
365 * * +:rw+ is equivalent to IO::CREAT|IO::RDWR
367 * +mode+ is an integer and only used when IO::CREAT is used.
368 * +mq_attr+ is a POSIX_MQ::Attr and only used if IO::CREAT is used.
369 * If +mq_attr+ is not specified when creating a queue, then the
370 * system defaults will be used.
372 * See the manpage for mq_open(3) for more details on this function.
374 static VALUE
init(int argc
, VALUE
*argv
, VALUE self
)
376 struct posix_mq
*mq
= get(self
, 0);
378 VALUE name
, oflags
, mode
, attr
;
380 rb_scan_args(argc
, argv
, "13", &name
, &oflags
, &mode
, &attr
);
382 switch (TYPE(oflags
)) {
389 else if (oflags
== sym_w
)
390 x
.oflags
= O_CREAT
|O_WRONLY
;
391 else if (oflags
== sym_rw
)
392 x
.oflags
= O_CREAT
|O_RDWR
;
394 oflags
= rb_inspect(oflags
);
395 rb_raise(rb_eArgError
,
396 "symbol must be :r, :w, or :rw: %s",
397 StringValuePtr(oflags
));
402 x
.oflags
= NUM2INT(oflags
);
405 rb_raise(rb_eArgError
, "flags must be an int, :r, :w, or :wr");
408 x
.name
= StringValueCStr(name
);
411 switch (TYPE(mode
)) {
414 x
.mode
= NUM2UINT(mode
);
417 if (x
.oflags
& O_CREAT
) {
423 rb_raise(rb_eArgError
, "mode not an integer");
426 switch (TYPE(attr
)) {
429 rstruct2mqattr(&x
.attr
, attr
, 1);
431 /* principle of least surprise */
432 if (x
.attr
.mq_flags
& O_NONBLOCK
)
433 x
.oflags
|= O_NONBLOCK
;
438 check_struct_type(attr
);
441 mq
->des
= (mqd_t
)xopen(&x
);
442 if (mq
->des
== MQD_INVALID
) {
449 mq
->des
= (mqd_t
)xopen(&x
);
451 if (mq
->des
== MQD_INVALID
)
452 rb_sys_fail("mq_open");
455 mq
->name
= rb_str_dup(name
);
456 if (x
.oflags
& O_NONBLOCK
)
457 mq
->attr
.mq_flags
= O_NONBLOCK
;
464 * POSIX_MQ.unlink(name) => 1
466 * Unlinks the message queue given by +name+. The queue will be destroyed
467 * when the last process with the queue open closes its queue descriptors.
469 static VALUE
s_unlink(VALUE self
, VALUE name
)
471 mqd_t rv
= mq_unlink(StringValueCStr(name
));
473 if (rv
== MQD_INVALID
)
474 rb_sys_fail("mq_unlink");
483 * Unlinks the message queue to prevent other processes from accessing it.
484 * All existing queue descriptors to this queue including those opened by
485 * other processes are unaffected. The queue will only be destroyed
486 * when the last process with open descriptors to this queue closes
489 static VALUE
_unlink(VALUE self
)
491 struct posix_mq
*mq
= get(self
, 0);
494 assert(TYPE(mq
->name
) == T_STRING
&& "mq->name is not a string");
496 rv
= mq_unlink(RSTRING_PTR(mq
->name
));
497 if (rv
== MQD_INVALID
)
498 rb_sys_fail("mq_unlink");
503 static void setup_send_buffer(struct rw_args
*x
, VALUE buffer
)
505 buffer
= rb_obj_as_string(buffer
);
506 x
->msg_ptr
= RSTRING_PTR(buffer
);
507 x
->msg_len
= (size_t)RSTRING_LEN(buffer
);
510 static VALUE
_send(int sflags
, int argc
, VALUE
*argv
, VALUE self
);
514 * mq.send(string [,priority[, timeout]]) => true
516 * Inserts the given +string+ into the message queue with an optional,
517 * unsigned integer +priority+. If the optional +timeout+ is specified,
518 * then Errno::ETIMEDOUT will be raised if the operation cannot complete
519 * before +timeout+ seconds has elapsed. Without +timeout+, this method
520 * may block until the queue is writable.
522 * On some older systems, the +timeout+ argument is not currently
523 * supported and may raise NotImplementedError if +timeout+ is used.
525 static VALUE
my_send(int argc
, VALUE
*argv
, VALUE self
)
527 return _send(0, argc
, argv
, self
);
530 static VALUE
_send(int sflags
, int argc
, VALUE
*argv
, VALUE self
)
532 struct posix_mq
*mq
= get(self
, 1);
534 VALUE buffer
, prio
, timeout
;
536 struct timespec expire
;
538 rb_scan_args(argc
, argv
, "12", &buffer
, &prio
, &timeout
);
540 setup_send_buffer(&x
, buffer
);
542 x
.timeout
= convert_timeout(&expire
, timeout
);
543 x
.msg_prio
= NIL_P(prio
) ? 0 : NUM2UINT(prio
);
545 rv
= (mqd_t
)rb_thread_blocking_region(xsend
, &x
, RUBY_UBF_IO
, 0);
546 if (rv
== MQD_INVALID
) {
547 if (errno
== EAGAIN
&& (sflags
& PMQ_TRY
))
549 rb_sys_fail("mq_send");
559 * Inserts the given +string+ into the message queue with a
560 * default priority of 0 and no timeout.
562 * Returns itself so its calls may be chained. This use is only
563 * recommended only for users who expect blocking behavior from
566 static VALUE
send0(VALUE self
, VALUE buffer
)
568 struct posix_mq
*mq
= get(self
, 1);
572 setup_send_buffer(&x
, buffer
);
577 rv
= (mqd_t
)rb_thread_blocking_region(xsend
, &x
, RUBY_UBF_IO
, 0);
578 if (rv
== MQD_INVALID
)
579 rb_sys_fail("mq_send");
589 * Returns an IO.select-able +IO+ object. This method is only available
590 * under Linux and FreeBSD and is not intended to be portable.
592 static VALUE
to_io(VALUE self
)
594 struct posix_mq
*mq
= get(self
, 1);
595 int fd
= MQD_TO_FD(mq
->des
);
598 mq
->io
= rb_funcall(rb_cIO
, id_new
, 1, INT2NUM(fd
));
604 static VALUE
_receive(int rflags
, int argc
, VALUE
*argv
, VALUE self
);
608 * mq.receive([buffer, [timeout]]) => [ message, priority ]
610 * Takes the highest priority message off the queue and returns
611 * an array containing the message as a String and the Integer
612 * priority of the message.
614 * If the optional +buffer+ is present, then it must be a String
615 * which will receive the data.
617 * If the optional +timeout+ is present, then it may be a Float
618 * or Integer specifying the timeout in seconds. Errno::ETIMEDOUT
619 * will be raised if +timeout+ has elapsed and there are no messages
622 * On some older systems, the +timeout+ argument is not currently
623 * supported and may raise NotImplementedError if +timeout+ is used.
625 static VALUE
receive(int argc
, VALUE
*argv
, VALUE self
)
627 return _receive(PMQ_WANTARRAY
, argc
, argv
, self
);
632 * mq.shift([buffer, [timeout]]) => message
634 * Takes the highest priority message off the queue and returns
635 * the message as a String.
637 * If the optional +buffer+ is present, then it must be a String
638 * which will receive the data.
640 * If the optional +timeout+ is present, then it may be a Float
641 * or Integer specifying the timeout in seconds. Errno::ETIMEDOUT
642 * will be raised if +timeout+ has elapsed and there are no messages
645 * On some older systems, the +timeout+ argument is not currently
646 * supported and may raise NotImplementedError if +timeout+ is used.
648 static VALUE
shift(int argc
, VALUE
*argv
, VALUE self
)
650 return _receive(0, argc
, argv
, self
);
653 static VALUE
_receive(int rflags
, int argc
, VALUE
*argv
, VALUE self
)
655 struct posix_mq
*mq
= get(self
, 1);
657 VALUE buffer
, timeout
;
659 struct timespec expire
;
661 if (mq
->attr
.mq_msgsize
< 0) {
662 if (mq_getattr(mq
->des
, &mq
->attr
) < 0)
663 rb_sys_fail("mq_getattr");
666 rb_scan_args(argc
, argv
, "02", &buffer
, &timeout
);
667 x
.timeout
= convert_timeout(&expire
, timeout
);
670 buffer
= rb_str_new(0, mq
->attr
.mq_msgsize
);
673 rb_str_modify(buffer
);
674 rb_str_resize(buffer
, mq
->attr
.mq_msgsize
);
677 x
.msg_ptr
= RSTRING_PTR(buffer
);
678 x
.msg_len
= (size_t)mq
->attr
.mq_msgsize
;
681 r
= (ssize_t
)rb_thread_blocking_region(xrecv
, &x
, RUBY_UBF_IO
, 0);
683 if (errno
== EAGAIN
&& (rflags
& PMQ_TRY
))
685 rb_sys_fail("mq_receive");
688 rb_str_set_len(buffer
, r
);
690 if (rflags
& PMQ_WANTARRAY
)
691 return rb_ary_new3(2, buffer
, UINT2NUM(x
.msg_prio
));
699 * Returns a POSIX_MQ::Attr struct containing the attributes
700 * of the message queue. See the mq_getattr(3) manpage for
703 static VALUE
getattr(VALUE self
)
705 struct posix_mq
*mq
= get(self
, 1);
708 if (mq_getattr(mq
->des
, &mq
->attr
) < 0)
709 rb_sys_fail("mq_getattr");
711 return rb_funcall(cAttr
, id_new
, 4,
712 LONG2NUM(mq
->attr
.mq_flags
),
713 LONG2NUM(mq
->attr
.mq_maxmsg
),
714 LONG2NUM(mq
->attr
.mq_msgsize
),
715 LONG2NUM(mq
->attr
.mq_curmsgs
));
720 * mq.attr = POSIX_MQ::Attr(IO::NONBLOCK) => mq_attr
722 * Only the IO::NONBLOCK flag may be set or unset (zero) in this manner.
723 * See the mq_setattr(3) manpage for more details.
725 * Consider using the POSIX_MQ#nonblock= method as it is easier and
726 * more natural to use.
728 static VALUE
setattr(VALUE self
, VALUE astruct
)
730 struct posix_mq
*mq
= get(self
, 1);
731 struct mq_attr newattr
;
733 rstruct2mqattr(&newattr
, astruct
, 0);
735 if (mq_setattr(mq
->des
, &newattr
, NULL
) < 0)
736 rb_sys_fail("mq_setattr");
745 * Closes the underlying message queue descriptor.
746 * If this descriptor had a registered notification request, the request
747 * will be removed so another descriptor or process may register a
748 * notification request. Message queue descriptors are automatically
749 * closed by garbage collection.
751 static VALUE
_close(VALUE self
)
753 struct posix_mq
*mq
= get(self
, 1);
755 if (! MQ_IO_CLOSE(mq
)) {
756 if (mq_close(mq
->des
) == -1)
757 rb_sys_fail("mq_close");
759 mq
->des
= MQD_INVALID
;
766 * mq.closed? => true or false
768 * Returns +true+ if the message queue descriptor is closed and therefore
769 * unusable, otherwise +false+
771 static VALUE
closed(VALUE self
)
773 struct posix_mq
*mq
= get(self
, 0);
775 return mq
->des
== MQD_INVALID
? Qtrue
: Qfalse
;
782 * Returns the string name of message queue associated with +mq+
784 static VALUE
name(VALUE self
)
786 struct posix_mq
*mq
= get(self
, 0);
788 return rb_str_dup(mq
->name
);
791 static int lookup_sig(VALUE sig
)
797 sig
= rb_obj_as_string(sig
);
798 len
= RSTRING_LEN(sig
);
799 ptr
= RSTRING_PTR(sig
);
801 if (len
> 3 && !memcmp("SIG", ptr
, 3))
802 sig
= rb_str_new(ptr
+ 3, len
- 3);
805 VALUE mSignal
= rb_const_get(rb_cObject
, rb_intern("Signal"));
807 list
= rb_funcall(mSignal
, rb_intern("list"), 0, 0);
808 rb_global_variable(&list
);
811 sig
= rb_hash_aref(list
, sig
);
813 rb_raise(rb_eArgError
, "invalid signal: %s\n", ptr
);
819 * TODO: Under Linux, we could just use netlink directly
820 * the same way glibc does...
822 /* we spawn a thread just to write ONE byte into an fd (usually a pipe) */
823 static void thread_notify_fd(union sigval sv
)
825 int fd
= sv
.sival_int
;
827 while ((write(fd
, "", 1) < 0) && (errno
== EINTR
|| errno
== EAGAIN
));
830 static void my_mq_notify(mqd_t des
, struct sigevent
*not)
832 mqd_t rv
= mq_notify(des
, not);
834 if (rv
== MQD_INVALID
) {
835 if (errno
== ENOMEM
) {
837 rv
= mq_notify(des
, not);
839 if (rv
== MQD_INVALID
)
840 rb_sys_fail("mq_notify");
845 static VALUE
setnotify_exec(VALUE self
, VALUE io
, VALUE thr
)
847 int fd
= NUM2INT(rb_funcall(io
, id_fileno
, 0, 0));
848 struct posix_mq
*mq
= get(self
, 1);
852 errno
= pthread_attr_init(&attr
);
853 if (errno
) rb_sys_fail("pthread_attr_init");
855 errno
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
856 if (errno
) rb_sys_fail("pthread_attr_setdetachstate");
858 #ifdef PTHREAD_STACK_MIN
859 (void)pthread_attr_setstacksize(&attr
, PTHREAD_STACK_MIN
);
862 not.sigev_notify
= SIGEV_THREAD
;
863 not.sigev_notify_function
= thread_notify_fd
;
864 not.sigev_notify_attributes
= &attr
;
865 not.sigev_value
.sival_int
= fd
;
867 if (!NIL_P(mq
->thread
))
868 rb_funcall(mq
->thread
, id_kill
, 0, 0);
871 my_mq_notify(mq
->des
, ¬);
877 static VALUE
notify_cleanup(VALUE self
)
879 struct posix_mq
*mq
= get(self
, 1);
881 if (!NIL_P(mq
->thread
)) {
882 rb_funcall(mq
->thread
, id_kill
, 0, 0);
890 * mq.notify = signal => signal
892 * Registers the notification request to deliver a given +signal+
893 * to the current process when message is received.
894 * If +signal+ is +nil+, it will unregister and disable the notification
895 * request to allow other processes to register a request.
896 * If +signal+ is +false+, it will register a no-op notification request
897 * which will prevent other processes from registering a notification.
898 * If +signal+ is an +IO+ object, it will spawn a thread upon the
899 * arrival of the next message and write one "\\0" byte to the file
900 * descriptor belonging to that IO object.
901 * Only one process may have a notification request for a queue
902 * at a time, Errno::EBUSY will be raised if there is already
903 * a notification request registration for the queue.
905 * Notifications are only fired once and processes must reregister
906 * for subsequent notifications.
908 * For readers of the mq_notify(3) manpage, passing +false+
909 * is equivalent to SIGEV_NONE, and passing +nil+ is equivalent
910 * of passing a NULL notification pointer to mq_notify(3).
912 static VALUE
setnotify(VALUE self
, VALUE arg
)
914 struct posix_mq
*mq
= get(self
, 1);
916 struct sigevent
* notification
= ¬
919 notify_cleanup(self
);
920 not.sigev_notify
= SIGEV_SIGNAL
;
924 not.sigev_notify
= SIGEV_NONE
;
930 not.sigev_signo
= NUM2INT(arg
);
934 not.sigev_signo
= lookup_sig(arg
);
935 rv
= INT2NUM(not.sigev_signo
);
938 rb_raise(rb_eArgError
, "must be a signal or nil");
941 my_mq_notify(mq
->des
, notification
);
948 * mq.nonblock? => true or false
950 * Returns the current non-blocking state of the message queue descriptor.
952 static VALUE
nonblock_p(VALUE self
)
954 struct posix_mq
*mq
= get(self
, 1);
956 if (mq_getattr(mq
->des
, &mq
->attr
) < 0)
957 rb_sys_fail("mq_getattr");
958 return mq
->attr
.mq_flags
& O_NONBLOCK
? Qtrue
: Qfalse
;
963 * mq.nonblock = boolean => boolean
965 * Enables or disables non-blocking operation for the message queue
966 * descriptor. Errno::EAGAIN will be raised in situations where
967 * the queue would block. This is not compatible with +timeout+
968 * arguments to POSIX_MQ#send and POSIX_MQ#receive.
970 static VALUE
setnonblock(VALUE self
, VALUE nb
)
972 struct mq_attr newattr
;
973 struct posix_mq
*mq
= get(self
, 1);
976 newattr
.mq_flags
= O_NONBLOCK
;
977 else if (nb
== Qfalse
)
978 newattr
.mq_flags
= 0;
980 rb_raise(rb_eArgError
, "must be true or false");
982 if (mq_setattr(mq
->des
, &newattr
, &mq
->attr
) < 0)
983 rb_sys_fail("mq_setattr");
985 mq
->attr
.mq_flags
= newattr
.mq_flags
;
992 * mq.trysend(string [,priority[, timeout]]) => +true+ or +false+
994 * Exactly like POSIX_MQ#send, except it returns +false+ instead of raising
995 * Errno::EAGAIN when non-blocking operation is desired and returns +true+
996 * on success instead of +nil+.
998 * This does not guarantee non-blocking behavior, the message queue must
999 * be made non-blocking before calling this method.
1001 static VALUE
trysend(int argc
, VALUE
*argv
, VALUE self
)
1003 _send(PMQ_TRY
, argc
, argv
, self
);
1008 * mq.tryshift([buffer [, timeout]]) => message or nil
1010 * Exactly like POSIX_MQ#shift, except it returns +nil+ instead of raising
1011 * Errno::EAGAIN when non-blocking operation is desired.
1013 * This does not guarantee non-blocking behavior, the message queue must
1014 * be made non-blocking before calling this method.
1016 static VALUE
tryshift(int argc
, VALUE
*argv
, VALUE self
)
1018 return _receive(PMQ_TRY
, argc
, argv
, self
);
1023 * mq.tryreceive([buffer [, timeout]]) => [ message, priority ] or nil
1025 * Exactly like POSIX_MQ#receive, except it returns +nil+ instead of raising
1026 * Errno::EAGAIN when non-blocking operation is desired.
1028 * This does not guarantee non-blocking behavior, the message queue must
1029 * be made non-blocking before calling this method.
1031 static VALUE
tryreceive(int argc
, VALUE
*argv
, VALUE self
)
1033 return _receive(PMQ_WANTARRAY
|PMQ_TRY
, argc
, argv
, self
);
1036 void Init_posix_mq_ext(void)
1038 VALUE cPOSIX_MQ
= rb_define_class("POSIX_MQ", rb_cObject
);
1039 rb_define_alloc_func(cPOSIX_MQ
, alloc
);
1040 cAttr
= rb_const_get(cPOSIX_MQ
, rb_intern("Attr"));
1043 * The maximum number of open message descriptors supported
1044 * by the system. This may be -1, in which case it is dynamically
1045 * set at runtime. Consult your operating system documentation
1046 * for system-specific information about this.
1048 rb_define_const(cPOSIX_MQ
, "OPEN_MAX",
1049 LONG2NUM(sysconf(_SC_MQ_OPEN_MAX
)));
1052 * The maximum priority that may be specified for POSIX_MQ#send
1053 * On POSIX-compliant systems, this is at least 31, but some
1054 * systems allow higher limits.
1055 * The minimum priority is always zero.
1057 rb_define_const(cPOSIX_MQ
, "PRIO_MAX",
1058 LONG2NUM(sysconf(_SC_MQ_PRIO_MAX
)));
1060 rb_define_singleton_method(cPOSIX_MQ
, "unlink", s_unlink
, 1);
1062 rb_define_private_method(cPOSIX_MQ
, "initialize", init
, -1);
1063 rb_define_method(cPOSIX_MQ
, "send", my_send
, -1);
1064 rb_define_method(cPOSIX_MQ
, "<<", send0
, 1);
1065 rb_define_method(cPOSIX_MQ
, "trysend", trysend
, -1);
1066 rb_define_method(cPOSIX_MQ
, "receive", receive
, -1);
1067 rb_define_method(cPOSIX_MQ
, "tryreceive", tryreceive
, -1);
1068 rb_define_method(cPOSIX_MQ
, "shift", shift
, -1);
1069 rb_define_method(cPOSIX_MQ
, "tryshift", tryshift
, -1);
1070 rb_define_method(cPOSIX_MQ
, "attr", getattr
, 0);
1071 rb_define_method(cPOSIX_MQ
, "attr=", setattr
, 1);
1072 rb_define_method(cPOSIX_MQ
, "close", _close
, 0);
1073 rb_define_method(cPOSIX_MQ
, "closed?", closed
, 0);
1074 rb_define_method(cPOSIX_MQ
, "unlink", _unlink
, 0);
1075 rb_define_method(cPOSIX_MQ
, "name", name
, 0);
1076 rb_define_method(cPOSIX_MQ
, "notify=", setnotify
, 1);
1077 rb_define_method(cPOSIX_MQ
, "nonblock=", setnonblock
, 1);
1078 rb_define_private_method(cPOSIX_MQ
, "notify_exec", setnotify_exec
, 2);
1079 rb_define_private_method(cPOSIX_MQ
, "notify_cleanup", notify_cleanup
, 0);
1080 rb_define_method(cPOSIX_MQ
, "nonblock?", nonblock_p
, 0);
1082 rb_define_method(cPOSIX_MQ
, "to_io", to_io
, 0);
1085 id_new
= rb_intern("new");
1086 id_kill
= rb_intern("kill");
1087 id_fileno
= rb_intern("fileno");
1088 id_divmod
= rb_intern("divmod");
1089 id_flags
= rb_intern("flags");
1090 id_maxmsg
= rb_intern("maxmsg");
1091 id_msgsize
= rb_intern("msgsize");
1092 id_curmsgs
= rb_intern("curmsgs");
1093 sym_r
= ID2SYM(rb_intern("r"));
1094 sym_w
= ID2SYM(rb_intern("w"));
1095 sym_rw
= ID2SYM(rb_intern("rw"));