2007-08-24 11:26:28 -04:00
|
|
|
class Mutex
|
2010-03-29 08:48:43 -04:00
|
|
|
# call-seq:
|
|
|
|
# mutex.synchronize { ... }
|
|
|
|
#
|
|
|
|
# Obtains a lock, runs the block, and releases the lock when the
|
|
|
|
# block completes. See the example under Mutex.
|
2007-08-24 14:10:37 -04:00
|
|
|
def synchronize
|
|
|
|
self.lock
|
2007-08-27 12:48:14 -04:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
2008-01-10 04:01:30 -05:00
|
|
|
self.unlock rescue nil
|
2007-08-27 12:48:14 -04:00
|
|
|
end
|
2007-08-24 11:26:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-08-24 21:09:08 -04:00
|
|
|
class Thread
|
2010-04-10 18:09:09 -04:00
|
|
|
MUTEX_FOR_THREAD_EXCLUSIVE = Mutex.new # :nodoc:
|
2010-03-29 08:48:43 -04:00
|
|
|
|
|
|
|
# call-seq:
|
|
|
|
# Thread.exclusive { block } => obj
|
2011-05-15 07:55:52 -04:00
|
|
|
#
|
2010-03-29 08:48:43 -04:00
|
|
|
# Wraps a block in Thread.critical, restoring the original value
|
|
|
|
# upon exit from the critical section, and returns the value of the
|
|
|
|
# block.
|
2007-08-24 21:09:08 -04:00
|
|
|
def self.exclusive
|
|
|
|
MUTEX_FOR_THREAD_EXCLUSIVE.synchronize{
|
|
|
|
yield
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|