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 #if defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL) && defined(HAVE_RUBY_THREAD_H)
100 # include <ruby/thread.h>
101 # define WITHOUT_GVL(fn,a,ubf,b) \
102 rb_thread_call_without_gvl((fn),(a),(ubf),(b))
103 #elif defined(HAVE_RB_THREAD_BLOCKING_REGION)
104 typedef VALUE (*my_blocking_fn_t
)(void*);
105 # define WITHOUT_GVL(fn,a,ubf,b) \
106 rb_thread_blocking_region((my_blocking_fn_t)(fn),(a),(ubf),(b))
109 /* partial emulation of the 1.9 rb_thread_blocking_region under 1.8 */
110 # include <rubysig.h>
111 # define RUBY_UBF_IO ((rb_unblock_function_t *)-1)
112 typedef void rb_unblock_function_t(void *);
113 typedef void * rb_blocking_function_t(void *);
114 static void * WITHOUT_GVL(rb_blocking_function_t
*func
, void *data1
,
115 rb_unblock_function_t
*ubf
, void *data2
)
119 assert(RUBY_UBF_IO
== ubf
&& "RUBY_UBF_IO required for emulation");
127 #endif /* ! HAVE_RB_THREAD_BLOCKING_REGION */
129 /* used to pass arguments to mq_open inside blocking region */
139 /* used to pass arguments to mq_send/mq_receive inside blocking region */
149 struct timespec
*timeout
;
152 #ifndef HAVE_MQ_TIMEDSEND
154 not_timedsend(mqd_t mqdes
, const char *msg_ptr
,
155 size_t msg_len
, unsigned msg_prio
,
156 const struct timespec
*abs_timeout
)
158 rb_bug("mq_timedsend workaround failed");
161 # define mq_timedsend not_timedsend
163 #ifndef HAVE_MQ_TIMEDRECEIVE
165 not_timedreceive(mqd_t mqdes
, char *msg_ptr
,
166 size_t msg_len
, unsigned *msg_prio
,
167 const struct timespec
*abs_timeout
)
169 rb_bug("mq_timedreceive workaround failed");
172 # define mq_timedreceive not_timedreceive
175 #if defined(HAVE_MQ_TIMEDRECEIVE) && defined(HAVE_MQ_TIMEDSEND)
176 static void num2timespec(struct timespec
*ts
, VALUE t
)
181 ts
->tv_sec
= NUM2TIMET(t
);
186 double val
= RFLOAT_VALUE(t
);
190 ts
->tv_nsec
= (long)(d
* 1e9
+ 0.5);
192 ts
->tv_nsec
= (long)(-d
* 1e9
+ 0.5);
193 if (ts
->tv_nsec
> 0) {
194 ts
->tv_nsec
= 1000000000 - ts
->tv_nsec
;
198 ts
->tv_sec
= (time_t)f
;
200 rb_raise(rb_eRangeError
, "%f out of range", val
);
205 VALUE ary
= rb_funcall(t
, id_divmod
, 1, INT2FIX(1));
207 Check_Type(ary
, T_ARRAY
);
209 ts
->tv_sec
= NUM2TIMET(rb_ary_entry(ary
, 0));
210 f
= rb_ary_entry(ary
, 1);
211 f
= rb_funcall(f
, '*', 1, INT2FIX(1000000000));
212 ts
->tv_nsec
= NUM2LONG(f
);
217 static void num2timespec(struct timespec
*ts
, VALUE t
)
219 rb_raise(rb_eNotImpError
,
220 "mq_timedsend and/or mq_timedreceive missing");
224 static struct timespec
*convert_timeout(struct timespec
*dest
, VALUE t
)
226 struct timespec ts
, now
;
231 num2timespec(&ts
, t
);
232 clock_gettime(CLOCK_REALTIME
, &now
);
233 dest
->tv_sec
= now
.tv_sec
+ ts
.tv_sec
;
234 dest
->tv_nsec
= now
.tv_nsec
+ ts
.tv_nsec
;
236 if (dest
->tv_nsec
> 1000000000) {
237 dest
->tv_nsec
-= 1000000000;
244 /* (may) run without GVL */
245 static void * xopen(void *ptr
)
247 struct open_args
*x
= ptr
;
250 case 2: x
->des
= mq_open(x
->name
, x
->oflags
); break;
251 case 3: x
->des
= mq_open(x
->name
, x
->oflags
, x
->mode
, NULL
); break;
252 case 4: x
->des
= mq_open(x
->name
, x
->oflags
, x
->mode
, &x
->attr
); break;
253 default: x
->des
= MQD_INVALID
;
259 /* runs without GVL */
260 static void *xsend(void *ptr
)
262 struct rw_args
*x
= ptr
;
264 x
->retval
= x
->timeout
?
265 mq_timedsend(x
->des
, x
->msg_ptr
, x
->msg_len
,
266 x
->msg_prio
, x
->timeout
) :
267 mq_send(x
->des
, x
->msg_ptr
, x
->msg_len
, x
->msg_prio
);
272 /* runs without GVL */
273 static void * xrecv(void *ptr
)
275 struct rw_args
*x
= ptr
;
277 x
->received
= x
->timeout
?
278 mq_timedreceive(x
->des
, x
->msg_ptr
, x
->msg_len
,
279 &x
->msg_prio
, x
->timeout
) :
280 mq_receive(x
->des
, x
->msg_ptr
, x
->msg_len
, &x
->msg_prio
);
286 static void mark(void *ptr
)
288 struct posix_mq
*mq
= ptr
;
290 rb_gc_mark(mq
->name
);
291 rb_gc_mark(mq
->thread
);
296 static void _free(void *ptr
)
298 struct posix_mq
*mq
= ptr
;
300 if (mq
->des
!= MQD_INVALID
&& MQ_IO_NIL_P(mq
)) {
301 /* we ignore errors when gc-ing */
308 /* automatically called at creation (before initialize) */
309 static VALUE
alloc(VALUE klass
)
312 VALUE rv
= Data_Make_Struct(klass
, struct posix_mq
, mark
, _free
, mq
);
314 mq
->des
= MQD_INVALID
;
315 mq
->attr
.mq_flags
= 0;
316 mq
->attr
.mq_maxmsg
= 0;
317 mq
->attr
.mq_msgsize
= -1;
318 mq
->attr
.mq_curmsgs
= 0;
326 /* unwraps the posix_mq struct from self */
327 static struct posix_mq
*get(VALUE self
, int need_valid
)
331 Data_Get_Struct(self
, struct posix_mq
, mq
);
333 if (need_valid
&& mq
->des
== MQD_INVALID
)
334 rb_raise(rb_eIOError
, "closed queue descriptor");
339 static void check_struct_type(VALUE astruct
)
341 if (CLASS_OF(astruct
) == cAttr
)
343 astruct
= rb_inspect(astruct
);
344 rb_raise(rb_eTypeError
, "not a POSIX_MQ::Attr: %s",
345 StringValuePtr(astruct
));
348 static void rstruct2mqattr(struct mq_attr
*attr
, VALUE astruct
, int all
)
352 check_struct_type(astruct
);
353 attr
->mq_flags
= NUM2LONG(rb_funcall(astruct
, id_flags
, 0));
355 tmp
= rb_funcall(astruct
, id_maxmsg
, 0);
356 if (all
|| !NIL_P(tmp
))
357 attr
->mq_maxmsg
= NUM2LONG(tmp
);
359 tmp
= rb_funcall(astruct
, id_msgsize
, 0);
360 if (all
|| !NIL_P(tmp
))
361 attr
->mq_msgsize
= NUM2LONG(tmp
);
363 tmp
= rb_funcall(astruct
, id_curmsgs
, 0);
365 attr
->mq_curmsgs
= NUM2LONG(tmp
);
370 * POSIX_MQ.new(name [, flags [, mode [, mq_attr]]) => mq
372 * Opens a POSIX message queue given by +name+. +name+ should start
373 * with a slash ("/") for portable applications.
375 * If a Symbol is given in place of integer +flags+, then:
377 * * +:r+ is equivalent to IO::RDONLY
378 * * +:w+ is equivalent to IO::CREAT|IO::WRONLY
379 * * +:rw+ is equivalent to IO::CREAT|IO::RDWR
381 * +mode+ is an integer and only used when IO::CREAT is used.
382 * +mq_attr+ is a POSIX_MQ::Attr and only used if IO::CREAT is used.
383 * If +mq_attr+ is not specified when creating a queue, then the
384 * system defaults will be used.
386 * See the manpage for mq_open(3) for more details on this function.
388 static VALUE
init(int argc
, VALUE
*argv
, VALUE self
)
390 struct posix_mq
*mq
= get(self
, 0);
392 VALUE name
, oflags
, mode
, attr
;
394 rb_scan_args(argc
, argv
, "13", &name
, &oflags
, &mode
, &attr
);
396 switch (TYPE(oflags
)) {
403 else if (oflags
== sym_w
)
404 x
.oflags
= O_CREAT
|O_WRONLY
;
405 else if (oflags
== sym_rw
)
406 x
.oflags
= O_CREAT
|O_RDWR
;
408 oflags
= rb_inspect(oflags
);
409 rb_raise(rb_eArgError
,
410 "symbol must be :r, :w, or :rw: %s",
411 StringValuePtr(oflags
));
416 x
.oflags
= NUM2INT(oflags
);
419 rb_raise(rb_eArgError
, "flags must be an int, :r, :w, or :wr");
422 x
.name
= StringValueCStr(name
);
425 switch (TYPE(mode
)) {
428 x
.mode
= NUM2UINT(mode
);
431 if (x
.oflags
& O_CREAT
) {
437 rb_raise(rb_eArgError
, "mode not an integer");
440 switch (TYPE(attr
)) {
443 rstruct2mqattr(&x
.attr
, attr
, 1);
445 /* principle of least surprise */
446 if (x
.attr
.mq_flags
& O_NONBLOCK
)
447 x
.oflags
|= O_NONBLOCK
;
452 check_struct_type(attr
);
457 if (mq
->des
== MQD_INVALID
) {
467 if (mq
->des
== MQD_INVALID
)
468 rb_sys_fail("mq_open");
471 mq
->name
= rb_str_dup(name
);
472 if (x
.oflags
& O_NONBLOCK
)
473 mq
->attr
.mq_flags
= O_NONBLOCK
;
480 * POSIX_MQ.unlink(name) => 1
482 * Unlinks the message queue given by +name+. The queue will be destroyed
483 * when the last process with the queue open closes its queue descriptors.
485 static VALUE
s_unlink(VALUE self
, VALUE name
)
487 int rv
= mq_unlink(StringValueCStr(name
));
490 rb_sys_fail("mq_unlink");
499 * Unlinks the message queue to prevent other processes from accessing it.
500 * All existing queue descriptors to this queue including those opened by
501 * other processes are unaffected. The queue will only be destroyed
502 * when the last process with open descriptors to this queue closes
505 static VALUE
_unlink(VALUE self
)
507 struct posix_mq
*mq
= get(self
, 0);
510 assert(TYPE(mq
->name
) == T_STRING
&& "mq->name is not a string");
512 rv
= mq_unlink(RSTRING_PTR(mq
->name
));
514 rb_sys_fail("mq_unlink");
519 static void setup_send_buffer(struct rw_args
*x
, VALUE buffer
)
521 buffer
= rb_obj_as_string(buffer
);
522 x
->msg_ptr
= RSTRING_PTR(buffer
);
523 x
->msg_len
= (size_t)RSTRING_LEN(buffer
);
526 static VALUE
_send(int sflags
, int argc
, VALUE
*argv
, VALUE self
);
530 * mq.send(string [,priority[, timeout]]) => true
532 * Inserts the given +string+ into the message queue with an optional,
533 * unsigned integer +priority+. If the optional +timeout+ is specified,
534 * then Errno::ETIMEDOUT will be raised if the operation cannot complete
535 * before +timeout+ seconds has elapsed. Without +timeout+, this method
536 * may block until the queue is writable.
538 * On some older systems, the +timeout+ argument is not currently
539 * supported and may raise NotImplementedError if +timeout+ is used.
541 static VALUE
my_send(int argc
, VALUE
*argv
, VALUE self
)
543 return _send(0, argc
, argv
, self
);
546 static VALUE
_send(int sflags
, int argc
, VALUE
*argv
, VALUE self
)
548 struct posix_mq
*mq
= get(self
, 1);
550 VALUE buffer
, prio
, timeout
;
551 struct timespec expire
;
553 rb_scan_args(argc
, argv
, "12", &buffer
, &prio
, &timeout
);
555 setup_send_buffer(&x
, buffer
);
557 x
.timeout
= convert_timeout(&expire
, timeout
);
558 x
.msg_prio
= NIL_P(prio
) ? 0 : NUM2UINT(prio
);
561 WITHOUT_GVL(xsend
, &x
, RUBY_UBF_IO
, 0);
562 if (x
.retval
== -1) {
565 if (errno
== EAGAIN
&& (sflags
& PMQ_TRY
))
567 rb_sys_fail("mq_send");
577 * Inserts the given +string+ into the message queue with a
578 * default priority of 0 and no timeout.
580 * Returns itself so its calls may be chained. This use is only
581 * recommended only for users who expect blocking behavior from
584 static VALUE
send0(VALUE self
, VALUE buffer
)
586 struct posix_mq
*mq
= get(self
, 1);
589 setup_send_buffer(&x
, buffer
);
595 WITHOUT_GVL(xsend
, &x
, RUBY_UBF_IO
, 0);
596 if (x
.retval
== -1) {
599 rb_sys_fail("mq_send");
610 * Returns an IO.select-able +IO+ object. This method is only available
611 * under Linux and FreeBSD and is not intended to be portable.
613 static VALUE
to_io(VALUE self
)
615 struct posix_mq
*mq
= get(self
, 1);
616 int fd
= MQD_TO_FD(mq
->des
);
619 mq
->io
= rb_funcall(rb_cIO
, id_new
, 1, INT2NUM(fd
));
625 static VALUE
_receive(int rflags
, int argc
, VALUE
*argv
, VALUE self
);
629 * mq.receive([buffer, [timeout]]) => [ message, priority ]
631 * Takes the highest priority message off the queue and returns
632 * an array containing the message as a String and the Integer
633 * priority of the message.
635 * If the optional +buffer+ is present, then it must be a String
636 * which will receive the data.
638 * If the optional +timeout+ is present, then it may be a Float
639 * or Integer specifying the timeout in seconds. Errno::ETIMEDOUT
640 * will be raised if +timeout+ has elapsed and there are no messages
643 * On some older systems, the +timeout+ argument is not currently
644 * supported and may raise NotImplementedError if +timeout+ is used.
646 static VALUE
receive(int argc
, VALUE
*argv
, VALUE self
)
648 return _receive(PMQ_WANTARRAY
, argc
, argv
, self
);
653 * mq.shift([buffer, [timeout]]) => message
655 * Takes the highest priority message off the queue and returns
656 * the message as a String.
658 * If the optional +buffer+ is present, then it must be a String
659 * which will receive the data.
661 * If the optional +timeout+ is present, then it may be a Float
662 * or Integer specifying the timeout in seconds. Errno::ETIMEDOUT
663 * will be raised if +timeout+ has elapsed and there are no messages
666 * On some older systems, the +timeout+ argument is not currently
667 * supported and may raise NotImplementedError if +timeout+ is used.
669 static VALUE
shift(int argc
, VALUE
*argv
, VALUE self
)
671 return _receive(0, argc
, argv
, self
);
674 static VALUE
_receive(int rflags
, int argc
, VALUE
*argv
, VALUE self
)
676 struct posix_mq
*mq
= get(self
, 1);
678 VALUE buffer
, timeout
;
679 struct timespec expire
;
681 if (mq
->attr
.mq_msgsize
< 0) {
682 if (mq_getattr(mq
->des
, &mq
->attr
) < 0)
683 rb_sys_fail("mq_getattr");
686 rb_scan_args(argc
, argv
, "02", &buffer
, &timeout
);
687 x
.timeout
= convert_timeout(&expire
, timeout
);
690 buffer
= rb_str_new(0, mq
->attr
.mq_msgsize
);
693 rb_str_modify(buffer
);
694 rb_str_resize(buffer
, mq
->attr
.mq_msgsize
);
697 x
.msg_ptr
= RSTRING_PTR(buffer
);
698 x
.msg_len
= (size_t)mq
->attr
.mq_msgsize
;
702 WITHOUT_GVL(xrecv
, &x
, RUBY_UBF_IO
, 0);
703 if (x
.received
< 0) {
706 if (errno
== EAGAIN
&& (rflags
& PMQ_TRY
))
708 rb_sys_fail("mq_receive");
711 rb_str_set_len(buffer
, x
.received
);
713 if (rflags
& PMQ_WANTARRAY
)
714 return rb_ary_new3(2, buffer
, UINT2NUM(x
.msg_prio
));
722 * Returns a POSIX_MQ::Attr struct containing the attributes
723 * of the message queue. See the mq_getattr(3) manpage for
726 static VALUE
getattr(VALUE self
)
728 struct posix_mq
*mq
= get(self
, 1);
730 if (mq_getattr(mq
->des
, &mq
->attr
) < 0)
731 rb_sys_fail("mq_getattr");
733 return rb_funcall(cAttr
, id_new
, 4,
734 LONG2NUM(mq
->attr
.mq_flags
),
735 LONG2NUM(mq
->attr
.mq_maxmsg
),
736 LONG2NUM(mq
->attr
.mq_msgsize
),
737 LONG2NUM(mq
->attr
.mq_curmsgs
));
742 * mq.attr = POSIX_MQ::Attr(IO::NONBLOCK) => mq_attr
744 * Only the IO::NONBLOCK flag may be set or unset (zero) in this manner.
745 * See the mq_setattr(3) manpage for more details.
747 * Consider using the POSIX_MQ#nonblock= method as it is easier and
748 * more natural to use.
750 static VALUE
setattr(VALUE self
, VALUE astruct
)
752 struct posix_mq
*mq
= get(self
, 1);
753 struct mq_attr newattr
;
755 rstruct2mqattr(&newattr
, astruct
, 0);
757 if (mq_setattr(mq
->des
, &newattr
, NULL
) < 0)
758 rb_sys_fail("mq_setattr");
767 * Closes the underlying message queue descriptor.
768 * If this descriptor had a registered notification request, the request
769 * will be removed so another descriptor or process may register a
770 * notification request. Message queue descriptors are automatically
771 * closed by garbage collection.
773 static VALUE
_close(VALUE self
)
775 struct posix_mq
*mq
= get(self
, 1);
777 if (! MQ_IO_CLOSE(mq
)) {
778 if (mq_close(mq
->des
) == -1)
779 rb_sys_fail("mq_close");
781 mq
->des
= MQD_INVALID
;
788 * mq.closed? => true or false
790 * Returns +true+ if the message queue descriptor is closed and therefore
791 * unusable, otherwise +false+
793 static VALUE
closed(VALUE self
)
795 struct posix_mq
*mq
= get(self
, 0);
797 return mq
->des
== MQD_INVALID
? Qtrue
: Qfalse
;
804 * Returns the string name of message queue associated with +mq+
806 static VALUE
name(VALUE self
)
808 struct posix_mq
*mq
= get(self
, 0);
810 return rb_str_dup(mq
->name
);
813 static int lookup_sig(VALUE sig
)
819 sig
= rb_obj_as_string(sig
);
820 len
= RSTRING_LEN(sig
);
821 ptr
= RSTRING_PTR(sig
);
823 if (len
> 3 && !memcmp("SIG", ptr
, 3))
824 sig
= rb_str_new(ptr
+ 3, len
- 3);
827 VALUE mSignal
= rb_const_get(rb_cObject
, rb_intern("Signal"));
829 list
= rb_funcall(mSignal
, rb_intern("list"), 0, 0);
830 rb_global_variable(&list
);
833 sig
= rb_hash_aref(list
, sig
);
835 rb_raise(rb_eArgError
, "invalid signal: %s\n", ptr
);
841 * TODO: Under Linux, we could just use netlink directly
842 * the same way glibc does...
844 /* we spawn a thread just to write ONE byte into an fd (usually a pipe) */
845 static void thread_notify_fd(union sigval sv
)
847 int fd
= sv
.sival_int
;
849 while ((write(fd
, "", 1) < 0) && (errno
== EINTR
|| errno
== EAGAIN
));
852 static void my_mq_notify(mqd_t des
, struct sigevent
*not)
854 int rv
= mq_notify(des
, not);
857 if (errno
== ENOMEM
) {
859 rv
= mq_notify(des
, not);
862 rb_sys_fail("mq_notify");
866 static void lower_stack_size(pthread_attr_t
*attr
)
868 /* some OSes have ridiculously small stack sizes */
869 #ifdef PTHREAD_STACK_MIN
870 size_t stack_size
= PTHREAD_STACK_MIN
;
871 size_t min_size
= 4096;
873 if (stack_size
< min_size
)
874 stack_size
= min_size
;
875 pthread_attr_setstacksize(attr
, stack_size
);
880 static VALUE
setnotify_exec(VALUE self
, VALUE io
, VALUE thr
)
882 int fd
= NUM2INT(rb_funcall(io
, id_fileno
, 0, 0));
883 struct posix_mq
*mq
= get(self
, 1);
887 errno
= pthread_attr_init(&attr
);
888 if (errno
) rb_sys_fail("pthread_attr_init");
890 errno
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
891 if (errno
) rb_sys_fail("pthread_attr_setdetachstate");
893 lower_stack_size(&attr
);
894 not.sigev_notify
= SIGEV_THREAD
;
895 not.sigev_notify_function
= thread_notify_fd
;
896 not.sigev_notify_attributes
= &attr
;
897 not.sigev_value
.sival_int
= fd
;
899 if (!NIL_P(mq
->thread
))
900 rb_funcall(mq
->thread
, id_kill
, 0, 0);
903 my_mq_notify(mq
->des
, ¬);
909 static VALUE
notify_cleanup(VALUE self
)
911 struct posix_mq
*mq
= get(self
, 1);
913 if (!NIL_P(mq
->thread
)) {
914 rb_funcall(mq
->thread
, id_kill
, 0, 0);
922 * mq.notify = signal => signal
924 * Registers the notification request to deliver a given +signal+
925 * to the current process when message is received.
926 * If +signal+ is +nil+, it will unregister and disable the notification
927 * request to allow other processes to register a request.
928 * If +signal+ is +false+, it will register a no-op notification request
929 * which will prevent other processes from registering a notification.
930 * If +signal+ is an +IO+ object, it will spawn a thread upon the
931 * arrival of the next message and write one "\\0" byte to the file
932 * descriptor belonging to that IO object.
933 * Only one process may have a notification request for a queue
934 * at a time, Errno::EBUSY will be raised if there is already
935 * a notification request registration for the queue.
937 * Notifications are only fired once and processes must reregister
938 * for subsequent notifications.
940 * For readers of the mq_notify(3) manpage, passing +false+
941 * is equivalent to SIGEV_NONE, and passing +nil+ is equivalent
942 * of passing a NULL notification pointer to mq_notify(3).
944 static VALUE
setnotify(VALUE self
, VALUE arg
)
946 struct posix_mq
*mq
= get(self
, 1);
948 struct sigevent
* notification
= ¬
951 notify_cleanup(self
);
952 not.sigev_notify
= SIGEV_SIGNAL
;
956 not.sigev_notify
= SIGEV_NONE
;
962 not.sigev_signo
= NUM2INT(arg
);
966 not.sigev_signo
= lookup_sig(arg
);
967 rv
= INT2NUM(not.sigev_signo
);
970 rb_raise(rb_eArgError
, "must be a signal or nil");
973 my_mq_notify(mq
->des
, notification
);
980 * mq.nonblock? => true or false
982 * Returns the current non-blocking state of the message queue descriptor.
984 static VALUE
nonblock_p(VALUE self
)
986 struct posix_mq
*mq
= get(self
, 1);
988 if (mq_getattr(mq
->des
, &mq
->attr
) < 0)
989 rb_sys_fail("mq_getattr");
990 return mq
->attr
.mq_flags
& O_NONBLOCK
? Qtrue
: Qfalse
;
995 * mq.nonblock = boolean => boolean
997 * Enables or disables non-blocking operation for the message queue
998 * descriptor. Errno::EAGAIN will be raised in situations where
999 * the queue would block. This is not compatible with +timeout+
1000 * arguments to POSIX_MQ#send and POSIX_MQ#receive.
1002 static VALUE
setnonblock(VALUE self
, VALUE nb
)
1004 struct mq_attr newattr
;
1005 struct posix_mq
*mq
= get(self
, 1);
1008 newattr
.mq_flags
= O_NONBLOCK
;
1009 else if (nb
== Qfalse
)
1010 newattr
.mq_flags
= 0;
1012 rb_raise(rb_eArgError
, "must be true or false");
1014 if (mq_setattr(mq
->des
, &newattr
, &mq
->attr
) < 0)
1015 rb_sys_fail("mq_setattr");
1017 mq
->attr
.mq_flags
= newattr
.mq_flags
;
1024 * mq.trysend(string [,priority[, timeout]]) => +true+ or +false+
1026 * Exactly like POSIX_MQ#send, except it returns +false+ instead of raising
1027 * Errno::EAGAIN when non-blocking operation is desired and returns +true+
1028 * on success instead of +nil+.
1030 * This does not guarantee non-blocking behavior, the message queue must
1031 * be made non-blocking before calling this method.
1033 static VALUE
trysend(int argc
, VALUE
*argv
, VALUE self
)
1035 return _send(PMQ_TRY
, argc
, argv
, self
);
1040 * mq.tryshift([buffer [, timeout]]) => message or nil
1042 * Exactly like POSIX_MQ#shift, except it returns +nil+ instead of raising
1043 * Errno::EAGAIN when non-blocking operation is desired.
1045 * This does not guarantee non-blocking behavior, the message queue must
1046 * be made non-blocking before calling this method.
1048 static VALUE
tryshift(int argc
, VALUE
*argv
, VALUE self
)
1050 return _receive(PMQ_TRY
, argc
, argv
, self
);
1055 * mq.tryreceive([buffer [, timeout]]) => [ message, priority ] or nil
1057 * Exactly like POSIX_MQ#receive, except it returns +nil+ instead of raising
1058 * Errno::EAGAIN when non-blocking operation is desired.
1060 * This does not guarantee non-blocking behavior, the message queue must
1061 * be made non-blocking before calling this method.
1063 static VALUE
tryreceive(int argc
, VALUE
*argv
, VALUE self
)
1065 return _receive(PMQ_WANTARRAY
|PMQ_TRY
, argc
, argv
, self
);
1068 void Init_posix_mq_ext(void)
1070 VALUE cPOSIX_MQ
= rb_define_class("POSIX_MQ", rb_cObject
);
1071 rb_define_alloc_func(cPOSIX_MQ
, alloc
);
1072 cAttr
= rb_const_get(cPOSIX_MQ
, rb_intern("Attr"));
1075 * The maximum number of open message descriptors supported
1076 * by the system. This may be -1, in which case it is dynamically
1077 * set at runtime. Consult your operating system documentation
1078 * for system-specific information about this.
1080 rb_define_const(cPOSIX_MQ
, "OPEN_MAX",
1081 LONG2NUM(sysconf(_SC_MQ_OPEN_MAX
)));
1084 * The maximum priority that may be specified for POSIX_MQ#send
1085 * On POSIX-compliant systems, this is at least 31, but some
1086 * systems allow higher limits.
1087 * The minimum priority is always zero.
1089 rb_define_const(cPOSIX_MQ
, "PRIO_MAX",
1090 LONG2NUM(sysconf(_SC_MQ_PRIO_MAX
)));
1092 rb_define_singleton_method(cPOSIX_MQ
, "unlink", s_unlink
, 1);
1094 rb_define_private_method(cPOSIX_MQ
, "initialize", init
, -1);
1095 rb_define_method(cPOSIX_MQ
, "send", my_send
, -1);
1096 rb_define_method(cPOSIX_MQ
, "<<", send0
, 1);
1097 rb_define_method(cPOSIX_MQ
, "trysend", trysend
, -1);
1098 rb_define_method(cPOSIX_MQ
, "receive", receive
, -1);
1099 rb_define_method(cPOSIX_MQ
, "tryreceive", tryreceive
, -1);
1100 rb_define_method(cPOSIX_MQ
, "shift", shift
, -1);
1101 rb_define_method(cPOSIX_MQ
, "tryshift", tryshift
, -1);
1102 rb_define_method(cPOSIX_MQ
, "attr", getattr
, 0);
1103 rb_define_method(cPOSIX_MQ
, "attr=", setattr
, 1);
1104 rb_define_method(cPOSIX_MQ
, "close", _close
, 0);
1105 rb_define_method(cPOSIX_MQ
, "closed?", closed
, 0);
1106 rb_define_method(cPOSIX_MQ
, "unlink", _unlink
, 0);
1107 rb_define_method(cPOSIX_MQ
, "name", name
, 0);
1108 rb_define_method(cPOSIX_MQ
, "notify=", setnotify
, 1);
1109 rb_define_method(cPOSIX_MQ
, "nonblock=", setnonblock
, 1);
1110 rb_define_private_method(cPOSIX_MQ
, "notify_exec", setnotify_exec
, 2);
1111 rb_define_private_method(cPOSIX_MQ
, "notify_cleanup", notify_cleanup
, 0);
1112 rb_define_method(cPOSIX_MQ
, "nonblock?", nonblock_p
, 0);
1114 rb_define_method(cPOSIX_MQ
, "to_io", to_io
, 0);
1117 id_new
= rb_intern("new");
1118 id_kill
= rb_intern("kill");
1119 id_fileno
= rb_intern("fileno");
1120 id_divmod
= rb_intern("divmod");
1121 id_flags
= rb_intern("flags");
1122 id_maxmsg
= rb_intern("maxmsg");
1123 id_msgsize
= rb_intern("msgsize");
1124 id_curmsgs
= rb_intern("curmsgs");
1125 sym_r
= ID2SYM(rb_intern("r"));
1126 sym_w
= ID2SYM(rb_intern("w"));
1127 sym_rw
= ID2SYM(rb_intern("rw"));