Class: Green::Mutex
Overview
Instance Attribute Summary
Attributes inherited from Semaphore
#counter, #value
Instance Method Summary
collapse
Methods inherited from Semaphore
#acquire, #rawlink, #release, #unlink, #wait, #wait_avaliable, #wait_links
Constructor Details
#initialize ⇒ Mutex
Returns a new instance of Mutex.
68
69
70
71
|
# File 'lib/green/semaphore.rb', line 68
def initialize
super 1
@slept = {}
end
|
Instance Method Details
#_wakeup(green) ⇒ Object
95
96
97
98
99
|
# File 'lib/green/semaphore.rb', line 95
def _wakeup(green)
if @slept.delete(green)
Green.hub.callback { green.switch }
end
end
|
#lock ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'lib/green/semaphore.rb', line 80
def lock
if Green.current.locals["mutex_locked_#{self.object_id}"]
Green.current.locals.delete "mutex_locked_#{self.object_id}"
raise Green::GreenError.new
end
Green.current.locals["mutex_locked_#{self.object_id}"] = true
acquire
end
|
#sleep(timeout = nil) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/green/semaphore.rb', line 101
def sleep(timeout = nil)
unlock
beg = Time.now
current = Green.current
@slept[current] = true
begin
if timeout
t = Green.hub.timer(timeout) { _wakeup(current) }
Green.hub.switch
t.green_cancel
else
Green.hub.switch
end
ensure
@slept.delete current
end
yield if block_given?
lock
Time.now - beg
end
|
#synchronize ⇒ Object
73
74
75
76
77
78
|
# File 'lib/green/semaphore.rb', line 73
def synchronize
lock
yield
ensure
unlock
end
|
#unlock ⇒ Object
89
90
91
92
93
|
# File 'lib/green/semaphore.rb', line 89
def unlock
raise Green::GreenError.new unless Green.current.locals["mutex_locked_#{self.object_id}"]
Green.current.locals.delete "mutex_locked_#{self.object_id}"
release
end
|