OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SemaphoreShim_Namespace::BinarySemaphore Class Reference

Utility type that implemnts behavior of a blocking binary semaphore, with an optional timeout. May be removed in favor of the C++20 std::binary_semaphore as soon as we can upgrade to gcc 11. More...

#include <BinarySemaphore.h>

Public Member Functions

void try_acquire ()
 Blocks calling thread until it can acquire the semaphore, i.e. release is called by another thread. More...
 
bool try_acquire_for (const size_t max_wait_in_ms)
 Blocks calling thread until it can acquire the semaphore, i.e. release is called by another thread, or max_wait_in_ms duration passes, whichever occurs first. More...
 
void release ()
 Sets internal is_ready variable to true, allowing another thread waiting for the semaphore to proceed. More...
 
void reset ()
 Resets the semaphore's ready condition to false. More...
 
void set_ready ()
 

Private Attributes

bool is_ready_ {false}
 
std::mutex state_mutex_
 
std::condition_variable condition_
 

Detailed Description

Utility type that implemnts behavior of a blocking binary semaphore, with an optional timeout. May be removed in favor of the C++20 std::binary_semaphore as soon as we can upgrade to gcc 11.

BinarySemaphore

Definition at line 32 of file BinarySemaphore.h.

Member Function Documentation

void SemaphoreShim_Namespace::BinarySemaphore::release ( )
inline

Sets internal is_ready variable to true, allowing another thread waiting for the semaphore to proceed.

Definition at line 74 of file BinarySemaphore.h.

References condition_, is_ready_, and state_mutex_.

74  {
75  std::unique_lock<std::mutex> state_lock(state_mutex_);
76  is_ready_ = true;
77  // notify the waiting thread
78  condition_.notify_one();
79  }
void SemaphoreShim_Namespace::BinarySemaphore::reset ( )
inline

Resets the semaphore's ready condition to false.

Definition at line 86 of file BinarySemaphore.h.

References is_ready_, and state_mutex_.

86  {
87  std::unique_lock<std::mutex> state_lock(state_mutex_);
88  is_ready_ = false;
89  }
void SemaphoreShim_Namespace::BinarySemaphore::set_ready ( )
inline

Definition at line 91 of file BinarySemaphore.h.

References is_ready_, and state_mutex_.

91  {
92  std::unique_lock<std::mutex> state_lock(state_mutex_);
93  is_ready_ = true;
94  }
void SemaphoreShim_Namespace::BinarySemaphore::try_acquire ( )
inline

Blocks calling thread until it can acquire the semaphore, i.e. release is called by another thread.

Note that this call will block indefinitely. If a timeout is required, use try_acquire_for()

Definition at line 42 of file BinarySemaphore.h.

References condition_, is_ready_, and state_mutex_.

42  {
43  std::unique_lock<std::mutex> state_lock(state_mutex_);
44  condition_.wait(state_lock, [&is_ready = is_ready_] { return is_ready; });
45  }
bool SemaphoreShim_Namespace::BinarySemaphore::try_acquire_for ( const size_t  max_wait_in_ms)
inline

Blocks calling thread until it can acquire the semaphore, i.e. release is called by another thread, or max_wait_in_ms duration passes, whichever occurs first.

Note that this function does not throw to maximize its performance in performance-critical scenarios. Instead, it returns true if the call completed successfully and false if it timed out.

Returns
A boolean value that is set to true if the semaphore lock was able to be acquired successfully, or false if it timed out (wait duration exceeded max_wait_in_ms)

Definition at line 62 of file BinarySemaphore.h.

References condition_, is_ready_, and state_mutex_.

62  {
63  std::unique_lock<std::mutex> state_lock(state_mutex_);
64  return condition_.wait_for(state_lock,
65  std::chrono::milliseconds(max_wait_in_ms),
66  [&is_ready = is_ready_]() { return is_ready; });
67  }

Member Data Documentation

std::condition_variable SemaphoreShim_Namespace::BinarySemaphore::condition_
private

Definition at line 99 of file BinarySemaphore.h.

Referenced by release(), try_acquire(), and try_acquire_for().

bool SemaphoreShim_Namespace::BinarySemaphore::is_ready_ {false}
private

Definition at line 97 of file BinarySemaphore.h.

Referenced by release(), reset(), set_ready(), try_acquire(), and try_acquire_for().

std::mutex SemaphoreShim_Namespace::BinarySemaphore::state_mutex_
private

Definition at line 98 of file BinarySemaphore.h.

Referenced by release(), reset(), set_ready(), try_acquire(), and try_acquire_for().


The documentation for this class was generated from the following file: