Class: QuackConcurrency::Waiter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/quack_concurrency/waiter.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.

Waiter is similar to ConditionVariable.

A few differences include:

  • the ability to force any future request to #wait to return immediately

  • every call to #wait can only be resumed via the Waiter (not with Thread#run, Thread#wakeup, etc.)

  • #wait does not accept a mutex

  • some methods have been renamed to be more intuitive

Instance Method Summary collapse

Constructor Details

#initializeWaiter

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.

Creates a new QuackConcurrency::Waiter concurrency tool.



16
17
18
19
20
# File 'lib/quack_concurrency/waiter.rb', line 16

def initialize
  @condition_variable = SafeConditionVariable.new
  @resume_all_indefinitely = false
  @mutex = ::Mutex.new
end

Instance Method Details

#any_waiting_threads?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.

Checks if any threads are waiting on it.

Returns:

  • (Boolean)


24
25
26
# File 'lib/quack_concurrency/waiter.rb', line 24

def any_waiting_threads?
  @condition_variable.any_waiting_threads?
end

#resume_allvoid

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.

Resumes all threads waiting on it.



30
31
32
# File 'lib/quack_concurrency/waiter.rb', line 30

def resume_all
  @condition_variable.broadcast
end

#resume_all_indefinitelyvoid

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.

Resumes all threads waiting on it and will cause

any future calls to {#wait} to return immediately.


37
38
39
40
41
42
# File 'lib/quack_concurrency/waiter.rb', line 37

def resume_all_indefinitely
  @mutex.synchronize do
    @resume_all_indefinitely = true
    resume_all
  end
end

#resume_nextvoid

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.

Resumes next thread waiting on it if one exists.



46
47
48
# File 'lib/quack_concurrency/waiter.rb', line 46

def resume_next
  @condition_variable.signal
end

#waitvoid

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.

Note:

Will block until resumed.

This method returns an undefined value.

Puts this thread to sleep until another thread resumes it via this QuackConcurrency::Waiter.



53
54
55
56
57
58
# File 'lib/quack_concurrency/waiter.rb', line 53

def wait
  @mutex.synchronize do
    return if @resume_all_indefinitely
    @condition_variable.wait(@mutex)
  end
end

#waiting_threads_countInteger

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 number of threads waiting on it.

Returns:

  • (Integer)


62
63
64
# File 'lib/quack_concurrency/waiter.rb', line 62

def waiting_threads_count
  @condition_variable.waiting_threads_count
end