Class: EM::Sequel::Mutex

Inherits:
Object
  • Object
show all
Defined in:
lib/em-postgresql-sequel/mutex.rb

Instance Method Summary collapse

Constructor Details

#initializeMutex

Returns a new instance of Mutex.



4
5
6
7
# File 'lib/em-postgresql-sequel/mutex.rb', line 4

def initialize
  @waiting = []
  @current = nil
end

Instance Method Details

#synchronizeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/em-postgresql-sequel/mutex.rb', line 9

def synchronize
  if @current
    if @current == Fiber.current
      raise "already in synchronize" 
    else
      @waiting << Fiber.current
      Fiber.yield
    end
  end
  
  @current = Fiber.current
  
  begin
    yield if block_given?
  ensure
    @current = nil
    if waiting = @waiting.shift
      waiting.resume
    end
  end
end