Method: Concurrent::ReentrantReadWriteLock#try_write_lock

Defined in:
lib/concurrent/atomic/reentrant_read_write_lock.rb

#try_write_lockBoolean

Try to acquire a write lock and return true if we succeed. If it cannot be acquired immediately, return false.

Returns:

  • (Boolean)

    true if the lock is successfully acquired



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/concurrent/atomic/reentrant_read_write_lock.rb', line 308

def try_write_lock
  if (held = @HeldCount.value) >= WRITE_LOCK_HELD
    @HeldCount.value = held + WRITE_LOCK_HELD
    return true
  else
    c = @Counter.value
    if !waiting_or_running_writer?(c) &&
       running_readers(c) == held &&
       @Counter.compare_and_set(c, c+RUNNING_WRITER)
       @HeldCount.value = held + WRITE_LOCK_HELD
      return true
    end
  end
  false
end