Class: QuackConcurrency::ConditionVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/quack_concurrency/condition_variable.rb,
lib/quack_concurrency/condition_variable/waitable.rb

Overview

ConditionVariable is similar to ::ConditionVariable.

A a few differences include:

Direct Known Subclasses

SafeConditionVariable

Defined Under Namespace

Classes: Waitable

Instance Method Summary collapse

Constructor Details

#initializeConditionVariable

Creates a new QuackConcurrency::ConditionVariable concurrency tool.



12
13
14
15
16
# File 'lib/quack_concurrency/condition_variable.rb', line 12

def initialize
  @mutex = ::Mutex.new
  @waitables = []
  @waitables_to_resume = []
end

Instance Method Details

#any_waiting_threads?Boolean

Checks if any threads are waiting on it.

Returns:

  • (Boolean)


20
21
22
# File 'lib/quack_concurrency/condition_variable.rb', line 20

def any_waiting_threads?
  waiting_threads_count >= 1
end

#broadcastself

Resumes all threads waiting on it.

Returns:

  • (self)


26
27
28
29
30
31
# File 'lib/quack_concurrency/condition_variable.rb', line 26

def broadcast
  @mutex.synchronize do
    signal_next until @waitables_to_resume.empty?
  end
  self
end

#next_waitable_to_wakeWaitable

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 the Waitable representing the next thread to be woken. It will return the thread that made the earliest call to #wait.

Returns:



37
38
39
# File 'lib/quack_concurrency/condition_variable.rb', line 37

def next_waitable_to_wake
  @mutex.synchronize { @waitables.first }
end

#signalself

Resumes next thread waiting on it if one exists.

Returns:

  • (self)


43
44
45
46
47
48
# File 'lib/quack_concurrency/condition_variable.rb', line 43

def signal
  @mutex.synchronize do
    signal_next if @waitables_to_resume.any?
  end
  self
end

#wait(mutex, timeout = nil) ⇒ self

Note:

Will block until resumed

Puts this thread to sleep until another thread resumes it. Threads will be woken in the chronological order that this was called.

Parameters:

  • mutex (Mutex)

    mutex to be unlocked while this thread is sleeping

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

    maximum time to sleep in seconds, nil for forever

Returns:

  • (self)

Raises:

  • (TypeError)

    if timeout is not nil or Numeric

  • (ArgumentError)

    if timeout is negative

  • (Exception)

    any exception raised by ::ConditionVariable#wait (eg. interrupts, ThreadError)



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/quack_concurrency/condition_variable.rb', line 59

def wait(mutex, timeout = nil)
  validate_mutex(mutex)
  validate_timeout(timeout)
  waitable = waitable_for_current_thread
  @mutex.synchronize do
    @waitables.push(waitable)
    @waitables_to_resume.push(waitable)
  end
  waitable.wait(mutex, timeout)
  self
end

#waitable_woken(waitable) ⇒ void

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.

This method returns an undefined value.

Remove a Waitable whose thread has been woken.



74
75
76
# File 'lib/quack_concurrency/condition_variable.rb', line 74

def waitable_woken(waitable)
  @mutex.synchronize { @waitables.delete(waitable) }
end

#waiting_threads_countInteger

Returns the number of threads currently waiting on it.

Returns:

  • (Integer)


80
81
82
# File 'lib/quack_concurrency/condition_variable.rb', line 80

def waiting_threads_count
  @waitables.length
end