about summary refs log tree commit homepage
path: root/ext/posix_mq/posix_mq.c
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-01-07 01:45:25 -0800
committerEric Wong <normalperson@yhbt.net>2010-01-07 01:45:25 -0800
commitb3c31cf444e2ca3dae0f6d2370944bfbf3382d88 (patch)
treee8c2776d4ff5abd590a30d1569c40f62b80358d9 /ext/posix_mq/posix_mq.c
parent3700db51399e4949ed314ad0545d037b7762064e (diff)
downloadruby_posix_mq-b3c31cf444e2ca3dae0f6d2370944bfbf3382d88.tar.gz
This acts like POSIX_MQ#receive but only returns the message
without the priority.
Diffstat (limited to 'ext/posix_mq/posix_mq.c')
-rw-r--r--ext/posix_mq/posix_mq.c32
1 files changed, 31 insertions, 1 deletions
diff --git a/ext/posix_mq/posix_mq.c b/ext/posix_mq/posix_mq.c
index 70d9125..ca0cab8 100644
--- a/ext/posix_mq/posix_mq.c
+++ b/ext/posix_mq/posix_mq.c
@@ -509,6 +509,8 @@ static void get_msgsize(struct posix_mq *mq)
         mq->msgsize = attr.mq_msgsize;
 }
 
+static VALUE _receive(int wantarray, int argc, VALUE *argv, VALUE self);
+
 /*
  * call-seq:
  *        mq.receive([buffer, [timeout]])                => [ message, priority ]
@@ -527,6 +529,31 @@ static void get_msgsize(struct posix_mq *mq)
  */
 static VALUE receive(int argc, VALUE *argv, VALUE self)
 {
+        return _receive(1, argc, argv, self);
+}
+
+/*
+ * call-seq:
+ *        mq.shift([buffer, [timeout]])                => message
+ *
+ * Takes the highest priority message off the queue and returns
+ * the message as a String.
+ *
+ * If the optional +buffer+ is present, then it must be a String
+ * which will receive the data.
+ *
+ * If the optional +timeout+ is present, then it may be a Float
+ * or Integer specifying the timeout in seconds.  Errno::ETIMEDOUT
+ * will be raised if +timeout+ has elapsed and there are no messages
+ * in the queue.
+ */
+static VALUE shift(int argc, VALUE *argv, VALUE self)
+{
+        return _receive(0, argc, argv, self);
+}
+
+static VALUE _receive(int wantarray, int argc, VALUE *argv, VALUE self)
+{
         struct posix_mq *mq = get(self, 1);
         struct rw_args x;
         VALUE buffer, timeout;
@@ -557,7 +584,9 @@ static VALUE receive(int argc, VALUE *argv, VALUE self)
 
         rb_str_set_len(buffer, r);
 
-        return rb_ary_new3(2, buffer, UINT2NUM(x.msg_prio));
+        if (wantarray)
+                return rb_ary_new3(2, buffer, UINT2NUM(x.msg_prio));
+        return buffer;
 }
 
 /*
@@ -889,6 +918,7 @@ void Init_posix_mq_ext(void)
         rb_define_method(cPOSIX_MQ, "send", _send, -1);
         rb_define_method(cPOSIX_MQ, "<<", send0, 1);
         rb_define_method(cPOSIX_MQ, "receive", receive, -1);
+        rb_define_method(cPOSIX_MQ, "shift", shift, -1);
         rb_define_method(cPOSIX_MQ, "attr", getattr, 0);
         rb_define_method(cPOSIX_MQ, "attr=", setattr, 1);
         rb_define_method(cPOSIX_MQ, "close", _close, 0);