Class: Higgs::ReadWriteLock

Inherits:
Object
  • Object
show all
Defined in:
lib/higgs/thread.rb

Defined Under Namespace

Classes: ChildLock, ReadLock, WriteLock

Constant Summary collapse

CVS_ID =

for ident(1)

'$Id: thread.rb 841 2008-12-24 09:23:20Z toki $'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReadWriteLock

Returns a new instance of ReadWriteLock.



286
287
288
289
290
291
292
293
294
295
296
# File 'lib/higgs/thread.rb', line 286

def initialize
  @lock = Mutex.new
  @read_cond = ConditionVariable.new
  @write_cond = ConditionVariable.new
  @count_of_working_readers = 0
  @count_of_standby_writers = 0
  @priority_to_writer = true
  @writing = false
  @read_lock = ReadLock.new(self)
  @write_lock = WriteLock.new(self)
end

Instance Attribute Details

#read_lockObject (readonly)

Returns the value of attribute read_lock.



298
299
300
# File 'lib/higgs/thread.rb', line 298

def read_lock
  @read_lock
end

#write_lockObject (readonly)

Returns the value of attribute write_lock.



299
300
301
# File 'lib/higgs/thread.rb', line 299

def write_lock
  @write_lock
end

Instance Method Details

#__read_lock__Object



301
302
303
304
305
306
307
308
309
# File 'lib/higgs/thread.rb', line 301

def __read_lock__
  @lock.synchronize{
    while (@writing || (@priority_to_writer && @count_of_standby_writers > 0))
      @read_cond.wait(@lock)
    end
    @count_of_working_readers += 1
  }
  nil
end

#__read_try_lock__Object



311
312
313
314
315
316
317
318
319
320
# File 'lib/higgs/thread.rb', line 311

def __read_try_lock__
  @lock.synchronize{
    if (@writing || (@priority_to_writer && @count_of_standby_writers > 0)) then
      return false
    else
      @count_of_working_readers += 1
      return true
    end
  }
end

#__read_unlock__Object



322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/higgs/thread.rb', line 322

def __read_unlock__
  @lock.synchronize{
    @count_of_working_readers -= 1
    @priority_to_writer = true
    if (@count_of_standby_writers > 0) then
      @write_cond.signal
    else
      @read_cond.broadcast
    end
  }
  nil
end

#__write_lock__Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/higgs/thread.rb', line 335

def __write_lock__
  @lock.synchronize{
    @count_of_standby_writers += 1
    begin
      while (@writing || @count_of_working_readers > 0)
        @write_cond.wait(@lock)
      end
      @writing = true
    ensure
      @count_of_standby_writers -= 1
    end
  }
  nil
end

#__write_try_lock__Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/higgs/thread.rb', line 350

def __write_try_lock__
  @lock.synchronize{
    @count_of_standby_writers += 1
    begin
      if (@writing || @count_of_working_readers > 0) then
        return false
      else
        @writing = true
        return true
      end
    ensure
      @count_of_standby_writers -= 1
    end
  }
  raise 'not to reach'
end

#__write_unlock__Object



367
368
369
370
371
372
373
374
375
376
377
# File 'lib/higgs/thread.rb', line 367

def __write_unlock__
  @lock.synchronize{
    @writing = false
    @priority_to_writer = false
    @read_cond.broadcast
    if (@count_of_standby_writers > 0) then
      @write_cond.signal
    end
  }
  nil
end

#to_aObject



411
412
413
# File 'lib/higgs/thread.rb', line 411

def to_a
  [ read_lock, write_lock ]
end