mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27287 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
		
			
				
	
	
		
			31 lines
		
	
	
	
		
			663 B
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			31 lines
		
	
	
	
		
			663 B
		
	
	
	
		
			Ruby
		
	
	
	
	
	
class Mutex
 | 
						|
  # call-seq:
 | 
						|
  #    mutex.synchronize { ... }
 | 
						|
  #
 | 
						|
  # Obtains a lock, runs the block, and releases the lock when the
 | 
						|
  # block completes.  See the example under Mutex.
 | 
						|
  def synchronize
 | 
						|
    self.lock
 | 
						|
    begin
 | 
						|
      yield
 | 
						|
    ensure
 | 
						|
      self.unlock rescue nil
 | 
						|
    end
 | 
						|
  end
 | 
						|
end
 | 
						|
 | 
						|
class Thread
 | 
						|
  MUTEX_FOR_THREAD_EXCLUSIVE = Mutex.new # :nodoc:
 | 
						|
 | 
						|
  # 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.
 | 
						|
  def self.exclusive
 | 
						|
    MUTEX_FOR_THREAD_EXCLUSIVE.synchronize{
 | 
						|
      yield
 | 
						|
    }
 | 
						|
  end
 | 
						|
end
 |