1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/lib/mutex_fix.rb

34 lines
553 B
Ruby
Raw Normal View History

require 'thread'
# monkey patch Mutex so it does not leak memory.
class Mutex
def lock
while (Thread.critical = true; @locked)
@waiting.unshift Thread.current
Thread.stop
end
@locked = true
Thread.critical = false
self
end
def unlock
return unless @locked
Thread.critical = true
@locked = false
begin
t = @waiting.pop
t.wakeup if t
rescue ThreadError
retry
end
Thread.critical = false
begin
t.run if t
rescue ThreadError
end
self
end
end