Class: Mutex

Inherits:
Object show all
Defined in:
opal/stdlib/thread.rb

Instance Method Summary collapse

Constructor Details

#initializeMutex

Returns a new instance of Mutex



119
120
121
122
123
# File 'opal/stdlib/thread.rb', line 119

def initialize
  # We still keep the @locked state so any logic based on try_lock while
  # held yields reasonable results.
  @locked = false
end

Instance Method Details

#lockObject

Raises:



125
126
127
128
129
# File 'opal/stdlib/thread.rb', line 125

def lock
  raise ThreadError, 'Deadlock' if @locked
  @locked = true
  self
end

#locked?Boolean

Returns:



131
132
133
# File 'opal/stdlib/thread.rb', line 131

def locked?
  @locked
end

#owned?Boolean

Returns:



135
136
137
138
# File 'opal/stdlib/thread.rb', line 135

def owned?
  # Being the only "thread", we implicitly own any locked mutex.
  @locked
end

#synchronizeObject



155
156
157
158
159
160
161
162
# File 'opal/stdlib/thread.rb', line 155

def synchronize
  lock
  begin
    yield
  ensure
    unlock
  end
end

#try_lockObject



140
141
142
143
144
145
146
147
# File 'opal/stdlib/thread.rb', line 140

def try_lock
  if locked?
    false
  else
    lock
    true
  end
end

#unlockObject

Raises:



149
150
151
152
153
# File 'opal/stdlib/thread.rb', line 149

def unlock
  raise ThreadError, 'Mutex not locked' unless @locked
  @locked = false
  self
end