Module: Mongoid::Lock
- Extended by:
- Semaphore
- Defined in:
- lib/mongoid/lock.rb,
lib/mongoid/lock/synch_methods.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Semaphore
reset_semaphore!, sema_count, sema_down, sema_ready?, sema_up
Class Method Details
.included(base) ⇒ Object
14
15
16
17
18
19
20
|
# File 'lib/mongoid/lock.rb', line 14
def self.included(base)
base.class_eval do
include Mongoid::Semaphore
default_sema_count 1
field :used_by, :type => String, :default => nil
end
end
|
Instance Method Details
#lock_acquire ⇒ Object
10
11
12
13
14
15
16
17
18
|
# File 'lib/mongoid/lock/synch_methods.rb', line 10
def lock_acquire
self.sema_down
if (self.sema_ready?)
self.set(:used_by, "#{Socket.hostname}:#{Process.pid}")
else
self.sema_up
raise Mongoid::Semaphore::UnsynchronizedAccess.new(self.sema_count, "Lock in Use! (#{self.used_by})")
end
end
|
#lock_release ⇒ Object
20
21
22
23
24
25
|
# File 'lib/mongoid/lock/synch_methods.rb', line 20
def lock_release
if (self.used_by == "#{Socket.hostname}:#{Process.pid}")
self.sema_up
self.set(:used_by, nil)
end
end
|
#reset_lock! ⇒ Object
6
7
8
|
# File 'lib/mongoid/lock/synch_methods.rb', line 6
def reset_lock!
self.update_attributes!({:sema_count => __semaphore_initial_count, :used_by => nil})
end
|
#synchronized(&block) ⇒ Object
28
29
30
31
32
33
34
35
|
# File 'lib/mongoid/lock/synch_methods.rb', line 28
def synchronized(&block)
self.lock_acquire
begin
block.call()
ensure
self.lock_release
end
end
|
#try_synchronized(&try_block) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/mongoid/lock/synch_methods.rb', line 37
def try_synchronized(&try_block)
return false unless self.sema_ready?
begin
self.synchronized do
try_block.call()
end
return true
rescue Mongoid::Semaphore::UnsynchronizedAccess => ua
return false
end
end
|
#used_by ⇒ Object
> Volatile Access, Reload to Ensure state
8
9
10
11
12
|
# File 'lib/mongoid/lock.rb', line 8
def used_by
self.save
self.reload
return super
end
|