Class: OsCtld::Lockable::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/osctld/lockable.rb

Constant Summary collapse

TIMEOUT =
90

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Lock

Returns a new instance of Lock

Parameters:

  • object (Object)


34
35
36
37
38
39
40
41
42
43
# File 'lib/osctld/lockable.rb', line 34

def initialize(object)
  @mutex = OsCtl::Lib::Mutex.new
  @in_held = []
  @in_queued = []
  @ex_queued = []
  @ex = nil
  @cond_ex = ConditionVariable.new
  @cond_in = ConditionVariable.new
  @object = object
end

Instance Method Details

#acquire_exclusiveObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/osctld/lockable.rb', line 117

def acquire_exclusive
  return if @mutex.owned? && @ex == Thread.current

  t = Time.now
  is_timeout = false

  LockRegistry.register(@object, :exclusive, :waiting)

  begin
    @mutex.lock(TIMEOUT)
  rescue OsCtl::Lib::Mutex::Timeout
    LockRegistry.register(@object, :exclusive, :timeout)
    raise OsCtld::DeadlockDetected.new(@object, :exclusive)
  end

  if @in_held.empty?
    @ex = Thread.current
    LockRegistry.register(@object, :exclusive, :locked)

  elsif @in_held.include?(Thread.current)
    @mutex.unlock
    fail 'attempted to acquire exclusive lock while holding inclusive lock'

  else
    @ex_queued << Thread.current

    # Wait for all inclusive blocks to finish
    LockRegistry.register(@object, :exclusive, :waiting)

    loop do
      now = Time.now

      if (now - t) >= TIMEOUT
        is_timeout = true
        break

      elsif @in_held.empty? && @ex.nil?
        break
      end

      @cond_ex.wait(@mutex, TIMEOUT - (now - t))
    end

    if (!@in_held.empty? || @ex) && is_timeout
      LockRegistry.register(@object, :exclusive, :timeout)
      @mutex.unlock
      raise OsCtld::DeadlockDetected.new(@object, :exclusive)
    else
      @ex = @ex_queued.shift
      LockRegistry.register(@object, :exclusive, :locked)
    end
  end
end

#acquire_inclusiveObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/osctld/lockable.rb', line 45

def acquire_inclusive
  t = Time.now
  is_timeout = false

  sync do
    if @ex_queued.any?
      @in_queued << Thread.current

      # Wait for the exclusive lock to finish, if there is one
      LockRegistry.register(@object, :inclusive, :waiting)

      loop do
        now = Time.now

        if (now - t) >= TIMEOUT
          is_timeout = true
          break

        elsif @ex.nil?
          break
        end

        @cond_in.wait(@mutex, TIMEOUT - (now - t))
      end

      if @ex && is_timeout
        LockRegistry.register(@object, :inclusive, :timeout)
        raise OsCtld::DeadlockDetected.new(@object, :inclusive)
      else
        @in_queued.delete(Thread.current)
        @in_held << Thread.current
        LockRegistry.register(@object, :inclusive, :locked)
      end

    else
      @in_held << Thread.current
      LockRegistry.register(@object, :inclusive, :locked)
    end
  end
end

#exclusivelyObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/osctld/lockable.rb', line 192

def exclusively
  if @mutex.owned? && @ex == Thread.current
    yield

  else
    acquire_exclusive

    begin
      yield

    ensure
      release_exclusive
    end
  end
end

#inclusivelyObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/osctld/lockable.rb', line 96

def inclusively
  return yield if @mutex.owned? && @ex == Thread.current

  held = false
  sync { held = @in_held.include?(Thread.current) }

  if held
    yield

  else
    acquire_inclusive

    begin
      yield

    ensure
      release_inclusive
    end
  end
end

#release_exclusiveObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/osctld/lockable.rb', line 171

def release_exclusive
  unless @mutex.owned?
    raise "expected to own the mutex, have you called acquire_exclusive first?"
  end

  # Leave exlusive block, signal waiting inclusive blocks to continue
  @ex = nil
  LockRegistry.register(@object, :exclusive, :unlocked)

  # Give the first chance to a round of inclusive locks, then exclusive
  # ones
  if @in_queued.any?
    @cond_in.broadcast

  elsif @ex_queued.any?
    @cond_ex.signal
  end

  @mutex.unlock
end

#release_inclusiveObject



86
87
88
89
90
91
92
93
94
# File 'lib/osctld/lockable.rb', line 86

def release_inclusive
  sync do
    @in_held.delete(Thread.current)
    LockRegistry.register(@object, :inclusive, :unlocked)

    # Start exclusive block, if there is one waiting
    @cond_ex.signal if @in_held.empty? && @ex_queued.any?
  end
end