Class: Mutex

Inherits:
Object
  • Object
show all
Defined in:
lib/rubysl/thread/thread.rb

Instance Method Summary collapse

Constructor Details

#initializeMutex

Returns a new instance of Mutex.



41
42
43
# File 'lib/rubysl/thread/thread.rb', line 41

def initialize
  @owner = nil
end

Instance Method Details

#lockObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubysl/thread/thread.rb', line 73

def lock
  Rubinius.memory_barrier
  if @owner == Thread.current
    raise ThreadError, "Recursively locking not allowed"
  end

  Rubinius.lock self
  @owner = Thread.current
  Rubinius.memory_barrier
  return self
end

#locked?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rubysl/thread/thread.rb', line 58

def locked?
  Rubinius.locked?(self)
end

#marshal_dumpObject

Check and only allow it to be marshal’d if there are no waiters.



46
47
48
49
# File 'lib/rubysl/thread/thread.rb', line 46

def marshal_dump
  raise "Unable to dump locked mutex" unless @waiters.empty?
  1
end

#marshal_load(bunk) ⇒ Object

Implemented because we must since we use marshal_load PLUS we need to create AND prime @lock. If we didn’t do this, then Marshal wouldn’t prime the lock anyway.



54
55
56
# File 'lib/rubysl/thread/thread.rb', line 54

def marshal_load(bunk)
  initialize
end

#synchronizeObject



96
97
98
99
100
101
102
103
# File 'lib/rubysl/thread/thread.rb', line 96

def synchronize
  lock
  begin
    yield
  ensure
    unlock
  end
end

#try_lockObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/rubysl/thread/thread.rb', line 62

def try_lock
  # Locking implies a memory barrier, so we don't need to use
  # one explicitly.
  if Rubinius.try_lock(self)
    @owner = Thread.current
    true
  else
    false
  end
end

#unlockObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/rubysl/thread/thread.rb', line 85

def unlock
  Rubinius.memory_barrier

  if @owner != Thread.current
    raise ThreadError, "#{Thread.current.inspect} not owner, #{@owner.inspect} is"
  end

  @owner = nil
  Rubinius.unlock self
end