Class: Mutex
Instance Method Summary collapse
-
#initialize ⇒ Mutex
constructor
A new instance of Mutex.
- #lock ⇒ Object
- #locked? ⇒ Boolean
- #owned? ⇒ Boolean
- #synchronize ⇒ Object
- #try_lock ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize ⇒ Mutex
Returns a new instance of Mutex.
146 147 148 149 150
# File 'opal/stdlib/thread.rb', line 146 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
#lock ⇒ Object
Raises:
- (ThreadError )
152 153 154 155 156
# File 'opal/stdlib/thread.rb', line 152 def lock raise ThreadError , 'Deadlock' if @locked @locked = true self end
#locked? ⇒ Boolean
Returns:
- (Boolean )
158 159 160
# File 'opal/stdlib/thread.rb', line 158 def locked? @locked end
#owned? ⇒ Boolean
Returns:
- (Boolean )
162 163 164 165
# File 'opal/stdlib/thread.rb', line 162 def owned? # Being the only "thread", we implicitly own any locked mutex. @locked end
#synchronize ⇒ Object
182 183 184 185 186 187 188 189
# File 'opal/stdlib/thread.rb', line 182 def synchronize lock begin yield ensure unlock end end
#try_lock ⇒ Object
167 168 169 170 171 172 173 174
# File 'opal/stdlib/thread.rb', line 167 def try_lock if locked? false else lock true end end
#unlock ⇒ Object
Raises:
- (ThreadError )
176 177 178 179 180
# File 'opal/stdlib/thread.rb', line 176 def unlock raise ThreadError , 'Mutex not locked' unless @locked @locked = false self end