Class: Tengine::Core::Mutex
- Inherits:
-
Object
- Object
- Tengine::Core::Mutex
- Defined in:
- lib/tengine/core/mutex.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#heartbeat ⇒ Object
If you need to lock it longer than ttl, call this and you can refresh the ttl.
-
#synchronize(&block) ⇒ Object
delays until you get a lock.
Class Method Details
.new(name, ttl = 2.048) ⇒ Tengine::Core::Mutex
Returns An instance.
129 130 131 132 133 134 135 |
# File 'lib/tengine/core/mutex.rb', line 129 def new name, ttl=2.048 t = 0.0 + ttl # type check raise TypeError, "finite numeric expected (got #{t})" unless t.finite? raise ArgumentError, "TTL doesn't make sense." unless t > 0 return oldnew(Tengine::Core::Mutex::Mutex.find_or_create(name, t), Moped::BSON::ObjectId.new, 0) end |
.oldnew ⇒ Object
123 |
# File 'lib/tengine/core/mutex.rb', line 123 alias oldnew new |
Instance Method Details
#heartbeat ⇒ Object
If you need to lock it longer than ttl, call this and you can refresh the ttl.
196 197 198 |
# File 'lib/tengine/core/mutex.rb', line 196 def heartbeat mutex.heartbeat self end |
#synchronize(&block) ⇒ Object
delays until you get a lock.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/tengine/core/mutex.rb', line 176 def synchronize(&block) raise ArgumentError, "no block given" unless block_given? if lock # OK, locked EM.schedule do begin heartbeat yield ensure unlock end end else # NG, try again later synchronize_internal mutex.ttl, block end end |