Class: Object

Inherits:
BasicObject
Defined in:
lib/osc-ruby/core_ext/object.rb

Overview

thread safety.

Instance Method Summary collapse

Instance Method Details

#mutexObject

Return the Mutex for this object, creating it if necessary. The tricky part is making sure that two threads don’t call this at the same time and end up creating two different mutexes.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/osc-ruby/core_ext/object.rb', line 18

def mutex 
  # If this object already has a mutex, just return it 
  return @__mutex if @__mutex 
      
  # Otherwise, we've got to create a mutex for the object. 
  # To do this safely we've got to synchronize on our class object. 
  synchronized(self.class) { 
    # Check again: by the time we enter this synchronized block, 
    # some other thread might have already created the mutex. 
    @__mutex = @__mutex || Mutex.new 
  } 
  # The return value is @__mutex 
end