diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-02-10 20:33:50 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-02-10 20:33:50 +0000 |
commit | 2a7ec27be2f17b74ea8383316d6948a3b3334ad9 (patch) | |
tree | e63f226e58748ea03b6a819fb8f41281fba445fe /ext/thread | |
parent | f9c378051a6d29e7680f2172926417981666ad4c (diff) |
* 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
Diffstat (limited to 'ext/thread')
-rw-r--r-- | ext/thread/thread.c | 34 |
1 files changed, 34 insertions, 0 deletions
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); |