From 2a7ec27be2f17b74ea8383316d6948a3b3334ad9 Mon Sep 17 00:00:00 2001 From: knu Date: Sat, 10 Feb 2007 20:33:50 +0000 Subject: [PATCH] * ext/thread/thread.c (rb_thread_exclusive): Implement Thread.exclusive. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@11694 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ ext/thread/thread.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/ChangeLog b/ChangeLog index 67fde827cb..99e1187f9d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sun Feb 11 05:32:54 2007 Akinori MUSHA + + * ext/thread/thread.c (rb_thread_exclusive): Implement + Thread.exclusive. + Sun Feb 11 05:26:51 2007 Akinori MUSHA * ext/thread/thread.c: Get rid of use of a dummy function. diff --git a/ext/thread/thread.c b/ext/thread/thread.c index a70f8b75f6..b2aa7a9fb3 100644 --- a/ext/thread/thread.c +++ b/ext/thread/thread.c @@ -18,6 +18,38 @@ static VALUE rb_cConditionVariable; static VALUE rb_cQueue; static VALUE rb_cSizedQueue; +static VALUE +thread_exclusive_do() +{ + rb_thread_critical = Qtrue; + + return rb_yield(Qundef); +} + +static VALUE +thread_exclusive_ensure(val) + VALUE val; +{ + rb_thread_critical = val; + + return Qundef; +} + +/* + * call-seq: + * Thread.exclusive { block } => obj + * + * Wraps a block in Thread.critical, restoring the original value + * upon exit from the critical section, and returns the value of the + * block. + */ + +static VALUE +rb_thread_exclusive() +{ + return rb_ensure(thread_exclusive_do, Qundef, thread_exclusive_ensure, rb_thread_critical); +} + typedef struct _Entry { VALUE value; struct _Entry *next; @@ -1067,6 +1099,8 @@ dummy_dump(VALUE self) void Init_thread(void) { + rb_define_singleton_method(rb_cThread, "exclusive", rb_thread_exclusive, 0); + rb_cMutex = rb_define_class("Mutex", rb_cObject); rb_define_alloc_func(rb_cMutex, rb_mutex_alloc); rb_define_method(rb_cMutex, "marshal_load", dummy_load, 1);