Class: ProcessShared::SynchronizableSemaphore::FasterUncheckedMutex Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/process_shared/synchronizable_semaphore.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Presents a mutex-like facade over a semaphore. NOTE: Unlocking a locked mutex from a different process or thread than that which locked it will result in undefined behavior, whereas with the Mutex class, this error will be detected and an exception raised.

It is recommended to develop using the Mutex class, which is checked, and to use this unchecked variant only to optimized performance for code paths that have been determined to have correct lock/unlock behavior.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(sem) ⇒ FasterUncheckedMutex

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of FasterUncheckedMutex.



42
43
44
# File 'lib/process_shared/synchronizable_semaphore.rb', line 42

def initialize(sem)
  @sem = sem
end

Instance Method Details

#locked?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if currently locked.

Returns:

  • (Boolean)

    true if currently locked



47
48
49
50
51
# File 'lib/process_shared/synchronizable_semaphore.rb', line 47

def locked?
  acquired = try_lock
  unlock if acquired
  !acquired
end

#sleep(timeout = nil) ⇒ Numeric

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Releases the lock and sleeps timeout seconds if it is given and non-nil or forever.

TODO: de-duplicate this from Mutex#sleep

Returns:

  • (Numeric)


59
60
61
62
63
64
65
66
# File 'lib/process_shared/synchronizable_semaphore.rb', line 59

def sleep(timeout = nil)
  unlock
  begin
    timeout ? Kernel.sleep(timeout) : Kernel.sleep
  ensure
    lock
  end
end

#try_lockBoolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if lock was acquired, false if already locked.

Returns:

  • (Boolean)

    true if lock was acquired, false if already locked



69
70
71
72
73
74
# File 'lib/process_shared/synchronizable_semaphore.rb', line 69

def try_lock
  @sem.try_wait
  true
rescue Errno::EAGAIN
  false
end