From f14356733e83e5af35ec5c9f2346bf9607fc3a16 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 2 Feb 2016 22:14:16 +0000 Subject: support idempotent POSIX_MQ#close IO#close under Ruby 2.3 is idempotent, we shall follow suit with POSIX_MQ#close --- ext/posix_mq/extconf.rb | 11 +++++++++++ ext/posix_mq/posix_mq.c | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) (limited to 'ext/posix_mq') diff --git a/ext/posix_mq/extconf.rb b/ext/posix_mq/extconf.rb index eb799b2..d433d1d 100644 --- a/ext/posix_mq/extconf.rb +++ b/ext/posix_mq/extconf.rb @@ -14,4 +14,15 @@ have_library("pthread") have_func("__mq_oshandle") have_func("mq_timedsend") have_func("mq_timedreceive") + +r, w = IO.pipe +r.close +w.close +begin + r.close + $CPPFLAGS += ' -DIDEMPOTENT_IO_CLOSE=1' +rescue IOError + $CPPFLAGS += ' -DIDEMPOTENT_IO_CLOSE=0' +end + create_makefile("posix_mq_ext") diff --git a/ext/posix_mq/posix_mq.c b/ext/posix_mq/posix_mq.c index 5e1b3b8..0567676 100644 --- a/ext/posix_mq/posix_mq.c +++ b/ext/posix_mq/posix_mq.c @@ -824,7 +824,15 @@ static VALUE setattr(VALUE self, VALUE astruct) */ static VALUE _close(VALUE self) { - struct posix_mq *mq = get(self, 1); + struct posix_mq *mq; + + if (IDEMPOTENT_IO_CLOSE) { /* defined in extconf.rb */ + mq = get(self, 0); + if (!mq || (mq->des == MQD_INVALID)) + return Qnil; + } else { + mq = get(self, 1); + } if (! MQ_IO_CLOSE(mq)) { if (mq_close(mq->des) < 0) -- cgit v1.2.3-24-ge0c7