Class: Moneta::SynchronizePrimitive Private
- Inherits:
-
Object
- Object
- Moneta::SynchronizePrimitive
- Defined in:
- lib/moneta/synchronize.rb
Overview
Instance Method Summary collapse
-
#enter(timeout = nil, wait = 0.01) ⇒ Boolean
(also: #lock)
Enter critical section (blocking).
-
#leave ⇒ Object
(also: #unlock)
Leave critical section.
-
#locked? ⇒ Boolean
Is the lock acquired?.
-
#synchronize {|Synchronized| ... } ⇒ Object
Synchronize block.
-
#try_enter ⇒ Boolean
(also: #try_lock)
Try to enter critical section (nonblocking).
Instance Method Details
#enter(timeout = nil, wait = 0.01) ⇒ Boolean Also known as: lock
Enter critical section (blocking)
33 34 35 36 37 38 39 40 |
# File 'lib/moneta/synchronize.rb', line 33 def enter(timeout = nil, wait = 0.01) time_at_timeout = Time.now + timeout if timeout while !timeout || Time.now < time_at_timeout return true if try_enter sleep(wait) end false end |
#leave ⇒ Object Also known as: unlock
Leave critical section
46 47 48 49 50 51 |
# File 'lib/moneta/synchronize.rb', line 46 def leave raise 'Not locked' unless @locked leave_primitive @locked = false nil end |
#locked? ⇒ Boolean
Is the lock acquired?
57 58 59 |
# File 'lib/moneta/synchronize.rb', line 57 def locked? @locked end |
#synchronize {|Synchronized| ... } ⇒ Object
Synchronize block
10 11 12 13 14 15 |
# File 'lib/moneta/synchronize.rb', line 10 def synchronize enter yield ensure leave end |
#try_enter ⇒ Boolean Also known as: try_lock
Try to enter critical section (nonblocking)
21 22 23 24 |
# File 'lib/moneta/synchronize.rb', line 21 def try_enter raise 'Already locked' if @locked enter_primitive ? @locked = true : false end |