Class: LightIO::Library::Thread::Mutex
- Inherits:
-
Object
- Object
- LightIO::Library::Thread::Mutex
- Extended by:
- Base::MockMethods
- Defined in:
- lib/lightio/library/thread.rb
Instance Method Summary collapse
-
#initialize ⇒ Mutex
constructor
A new instance of Mutex.
- #lock ⇒ Object
- #locked? ⇒ Boolean
- #owned? ⇒ Boolean
- #sleep(timeout = nil) ⇒ Object
- #synchronize ⇒ Object
- #try_lock ⇒ Object
- #unlock ⇒ Object
Constructor Details
Instance Method Details
#lock ⇒ Object
239 240 241 242 243 244 |
# File 'lib/lightio/library/thread.rb', line 239 def lock raise ThreadError, "deadlock; recursive locking" if owned? @queue.pop @locked_thread = LightIO::Thread.current self end |
#locked? ⇒ Boolean
253 254 255 |
# File 'lib/lightio/library/thread.rb', line 253 def locked? !@locked_thread.nil? end |
#owned? ⇒ Boolean
257 258 259 |
# File 'lib/lightio/library/thread.rb', line 257 def owned? @locked_thread == LightIO::Thread.current end |
#sleep(timeout = nil) ⇒ Object
261 262 263 264 265 |
# File 'lib/lightio/library/thread.rb', line 261 def sleep(timeout=nil) unlock LightIO.sleep(timeout) lock end |
#synchronize ⇒ Object
267 268 269 270 271 272 273 274 275 |
# File 'lib/lightio/library/thread.rb', line 267 def synchronize raise ThreadError, 'must be called with a block' unless block_given? lock begin yield ensure unlock end end |
#try_lock ⇒ Object
277 278 279 280 281 282 283 284 |
# File 'lib/lightio/library/thread.rb', line 277 def try_lock if @locked_thread.nil? lock true else false end end |
#unlock ⇒ Object
246 247 248 249 250 251 |
# File 'lib/lightio/library/thread.rb', line 246 def unlock raise ThreadError, "Attempt to unlock a mutex which is not locked" unless owned? @locked_thread = nil @queue << true self end |