Class: FSDB::Modex

Inherits:
Object
  • Object
show all
Defined in:
lib/fsdb/modex.rb

Overview

Modex is a modal exclusion semaphore. The two modes are shared (SH) and exclusive (EX). Modex is not nestable.

Defined Under Namespace

Modules: ForkSafely

Constant Summary collapse

SH =
:SH
EX =
:EX

Instance Method Summary collapse

Constructor Details

#initializeModex

Returns a new instance of Modex.



11
12
13
14
15
16
17
# File 'lib/fsdb/modex.rb', line 11

def initialize
  @waiting  = []
  @locked   = []
  @mode     = nil
  @first    = true
  @m        = Mutex.new
end

Instance Method Details

#lock(mode) ⇒ Object

the block is executed in the exclusive context



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fsdb/modex.rb', line 38

def lock mode
  @m.synchronize do
    thread = Thread.current
    raise ThreadError, "nesting not allowed" if @locked.include?(thread)

    if @mode == mode and mode == SH and @waiting.empty? # strict queue
      @locked << thread
    elsif not @mode
      @mode = mode
      @locked << thread
    else
      @waiting << thread << mode
      @m.unlock
      Thread.stop
      @m.lock
    end
    
    yield if block_given?

#      if @mode != mode
#        raise "@mode == #{@mode} but mode == #{mode}"
#      end
#
#      if @mode == EX and @locked.size > 1
#        raise "@mode == EX but @locked.size == #{@locked.size}"
#      end

    self
  end
end

#remove_deadObject

:nodoc:



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/fsdb/modex.rb', line 125

def remove_dead # :nodoc:
  @m.synchronize do
    waiting = @waiting; @waiting = []
    until waiting.empty?
    
      t = waiting.shift; m = waiting.shift
      @waiting << t << m if t.alive?
    end
    
    @locked = @locked.select {|t| t.alive?}
    wake_next_waiter if @locked.empty?
  end
end

#synchronize(mode, do_when_first = nil, do_when_last = nil, arg = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fsdb/modex.rb', line 91

def synchronize mode, do_when_first = nil, do_when_last = nil, arg = nil
  lock mode do
    if @first
      @first = false
      
      if do_when_first
        if mode == SH
          @mode = EX
        end

        nonexclusive { do_when_first[arg] }

        if mode == SH
          @mode = SH
          wake_waiting_sharers
        end
      end
    end
  end
  
  yield
  
ensure
  unlock do
    if @locked.size == 1
      if do_when_last
        @mode = EX
        nonexclusive { do_when_last[arg] }
      end
      @first = true
    end
  end
end

#try_lock(mode) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fsdb/modex.rb', line 19

def try_lock mode
  @m.synchronize do
    thread = Thread.current
    raise ThreadError, "nesting not allowed" if @locked.include?(thread)

    if @mode == mode and mode == SH and @waiting.empty? # strict queue
      @locked << thread
      true
    elsif not @mode
      @mode = mode
      @locked << thread
      true
    else
      false
    end
  end
end

#unlockObject

the block is executed in the exclusive context

Raises:

  • (ThreadError)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fsdb/modex.rb', line 70

def unlock
  raise ThreadError, "already unlocked" unless @mode
  
  @m.synchronize do
    if block_given?
      begin
        yield
      ensure
        @locked.delete Thread.current
      end
    
    else
      @locked.delete Thread.current
    end
    
    wake_next_waiter if @locked.empty?
  end

  self
end