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



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

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



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

def lock
  fail ThreadError, "Deadlock" if @locked
  @locked = true
  self
end

#locked?Boolean

Returns:



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

def locked?
  @locked
end

#owned?Boolean

Returns:



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

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

#synchronizeObject



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

def synchronize
  lock
  begin
    yield
  ensure
    unlock
  end
end

#try_lockObject



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

def try_lock
  if locked?
    false
  else
    lock
    true
  end
end

#unlockObject



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

def unlock
  fail ThreadError, "Mutex not locked" unless @locked
  @locked = false
  self
end