Class: Farce::Lock

Inherits:
Internal::Lock
  • Object
show all
Includes:
Internal::Copyable, Shareable::Native, Shareable::Unfreezable, Internal::MarshalSupport::Reject
Defined in:
lib/farce/lock.rb

Overview

Note:

On JRuby and TruffleRuby, this will be a subclass of Mutex. On CRuby, it will be a custom implementation, as Mutex instances are not Ractor-shareable.

A Ractor-shareable mutual-exclusion lock with the same interface as Ruby's Mutex.

Lock ownership belongs to the current Fiber. Attempting to lock the same lock recursively raises ThreadError. Waiting for a lock cooperates with an installed Fiber scheduler.

Instance Method Summary collapse

Methods included from Shareable

#initialize, #ractor_shareable?

Methods included from Internal::Copyable

#duplicable?

Instance Method Details

#lock ⇒ self

Acquire the lock, waiting until it is available.

Returns:

  • (self)

Raises:

  • (ThreadError) —

    if the current Fiber already owns the lock



46
47
48
49
50
51
52
53
# File 'lib/farce/lock.rb', line 46

class Lock < Internal::Lock
  include Internal::MarshalSupport::Reject
  include Internal::Copyable
  include Shareable::Native
  include Shareable::Unfreezable

  private def instance_variables_to_inspect = Internal::EMPTY_ARRAY
end

#locked? ⇒ Boolean

Returns whether any Fiber owns the lock.

Returns:

  • (Boolean) —

    whether any Fiber owns the lock



46
47
48
49
50
51
52
53
# File 'lib/farce/lock.rb', line 46

class Lock < Internal::Lock
  include Internal::MarshalSupport::Reject
  include Internal::Copyable
  include Shareable::Native
  include Shareable::Unfreezable

  private def instance_variables_to_inspect = Internal::EMPTY_ARRAY
end

#owned? ⇒ Boolean

Returns whether the current Fiber owns the lock.

Returns:

  • (Boolean) —

    whether the current Fiber owns the lock



46
47
48
49
50
51
52
53
# File 'lib/farce/lock.rb', line 46

class Lock < Internal::Lock
  include Internal::MarshalSupport::Reject
  include Internal::Copyable
  include Shareable::Native
  include Shareable::Unfreezable

  private def instance_variables_to_inspect = Internal::EMPTY_ARRAY
end

#sleep(timeout = nil) ⇒ nil

Release the lock while sleeping, then reacquire it before returning.

Parameters:

  • timeout (Numeric, nil) (defaults to: nil) —

    the maximum number of seconds to sleep, or nil to sleep indefinitely

Returns:

  • (nil)

Raises:

  • (ThreadError) —

    if the current Fiber does not own the lock



46
47
48
49
50
51
52
53
# File 'lib/farce/lock.rb', line 46

class Lock < Internal::Lock
  include Internal::MarshalSupport::Reject
  include Internal::Copyable
  include Shareable::Native
  include Shareable::Unfreezable

  private def instance_variables_to_inspect = Internal::EMPTY_ARRAY
end

#synchronize { ... } ⇒ Object

Acquire the lock for the duration of the block.

Yields:

  • the block to run while holding the lock

Returns:

  • (Object) —

    the block result

Raises:

  • (ThreadError) —

    if no block is given or the current Fiber already owns the lock



46
47
48
49
50
51
52
53
# File 'lib/farce/lock.rb', line 46

class Lock < Internal::Lock
  include Internal::MarshalSupport::Reject
  include Internal::Copyable
  include Shareable::Native
  include Shareable::Unfreezable

  private def instance_variables_to_inspect = Internal::EMPTY_ARRAY
end

#try_lock ⇒ Boolean

Attempt to acquire the lock without waiting.

Returns:

  • (Boolean) —

    whether the lock was acquired



46
47
48
49
50
51
52
53
# File 'lib/farce/lock.rb', line 46

class Lock < Internal::Lock
  include Internal::MarshalSupport::Reject
  include Internal::Copyable
  include Shareable::Native
  include Shareable::Unfreezable

  private def instance_variables_to_inspect = Internal::EMPTY_ARRAY
end

#unlock ⇒ self

Release the lock.

Returns:

  • (self)

Raises:

  • (ThreadError) —

    if the current Fiber does not own the lock



46
47
48
49
50
51
52
53
# File 'lib/farce/lock.rb', line 46

class Lock < Internal::Lock
  include Internal::MarshalSupport::Reject
  include Internal::Copyable
  include Shareable::Native
  include Shareable::Unfreezable

  private def instance_variables_to_inspect = Internal::EMPTY_ARRAY
end