Xyris  0.5
Semaphore Class Reference

#include <Semaphore.hpp>

+ Collaboration diagram for Semaphore:

Public Member Functions

 Semaphore (uint32_t val, bool share, const char *name=nullptr)
 
bool wait ()
 
bool tryWait ()
 
bool timeWait (const uint32_t *usec)
 
bool post ()
 
uint32_t count ()
 

Private Attributes

bool m_isShared
 
uint32_t m_count
 
struct task_sync m_taskSync
 

Detailed Description

Definition at line 16 of file Semaphore.hpp.

Constructor & Destructor Documentation

◆ Semaphore()

Semaphore::Semaphore ( uint32_t  val,
bool  share,
const char *  name = nullptr 
)

Initializes a semaphore struct using the values defined. Semaphores are used in order to maintain mutual exclusion when multiple threads need to access a particular variable.

Parameters
valInitial value
shareAllow semaphore to be shared across threads
nameSemaphore name (for debugging / printing)

Definition at line 23 of file Semaphore.cpp.

24  : m_isShared(share)
25  , m_count(val)
26 {
28  tasks_sync_init(&m_taskSync);
29 }

Member Function Documentation

◆ count()

uint32_t Semaphore::count ( )

Atomically get the semaphore's current counter value.

Returns
uint32_t Returns the semaphore count.

Definition at line 81 of file Semaphore.cpp.

82 {
83  uint32_t ret = 0;
84  __atomic_load(&m_count, &ret, __ATOMIC_ACQUIRE);
85 
86  return ret;
87 }
+ Here is the caller graph for this function:

◆ post()

bool Semaphore::post ( )

Post to the semaphore.

Returns
int Returns true on success.

Definition at line 73 of file Semaphore.cpp.

74 {
75  __atomic_fetch_add(&m_count, 1, __ATOMIC_RELEASE);
77 
78  return true;
79 }
+ Here is the call graph for this function:

◆ timeWait()

bool Semaphore::timeWait ( const uint32_t *  usec)

Wait on the semaphore for a set duration of time.

Parameters
usecMicroseconds to wait until returning if unsuccessful.
Returns
int Returns true on success.

Definition at line 58 of file Semaphore.cpp.

59 {
60  // TODO: Add the timer functionality here.
61  (void)usec;
62  uint32_t curVal = count();
63  do {
64  while (curVal == 0) {
65  curVal = count();
66  }
67  // Fail using atomic relaxed because it may allow us to get to the "waiting" state faster.
68  } while (!COMPARE_EXCHANGE(curVal, __ATOMIC_ACQUIRE));
69 
70  return true;
71 }
+ Here is the call graph for this function:

◆ tryWait()

bool Semaphore::tryWait ( )

Check if the semaphore is available. If unavailable, return immediately, otherwise wait on the semaphore.

Returns
int Returns 0 on success.

Definition at line 45 of file Semaphore.cpp.

46 {
47  uint32_t curVal = count();
48  do {
49  if (curVal == 0) {
50  return false;
51  }
52  // We need to fail on an Atomic Acquire Release because it will fail less often (i.e. fewer loop iterations)
53  } while (!COMPARE_EXCHANGE(curVal, __ATOMIC_ACQUIRE));
54 
55  return true;
56 }
+ Here is the call graph for this function:

◆ wait()

bool Semaphore::wait ( )

Wait for the semaphore to become available.

Returns
int Returns true on success.

Definition at line 31 of file Semaphore.cpp.

32 {
33  uint32_t curVal = count();
34  do {
35  while (curVal == 0) {
37  curVal = count();
38  }
39  // Fail using atomic relaxed because it may allow us to get to the "waiting" state faster.
40  } while (!COMPARE_EXCHANGE(curVal, __ATOMIC_RELAXED));
41 
42  return true;
43 }
+ Here is the call graph for this function:

Field Documentation

◆ m_count

uint32_t Semaphore::m_count
private

Definition at line 68 of file Semaphore.hpp.

◆ m_isShared

bool Semaphore::m_isShared
private

Definition at line 67 of file Semaphore.hpp.

◆ m_taskSync

struct task_sync Semaphore::m_taskSync
private

Definition at line 69 of file Semaphore.hpp.


The documentation for this class was generated from the following files:
Semaphore::count
uint32_t count()
Atomically get the semaphore's current counter value.
Definition: Semaphore.cpp:81
Semaphore::m_count
uint32_t m_count
Definition: Semaphore.hpp:68
COMPARE_EXCHANGE
#define COMPARE_EXCHANGE(curVal, failure_memorder)
Definition: Semaphore.cpp:15
task_sync::dbg_name
const char * dbg_name
Definition: tasks.hpp:58
Semaphore::m_taskSync
struct task_sync m_taskSync
Definition: Semaphore.hpp:69
Semaphore::m_isShared
bool m_isShared
Definition: Semaphore.hpp:67
tasks_sync_unblock
void tasks_sync_unblock(struct task_sync *ts)
Definition: tasks.cpp:575
TASK_ONLY
#define TASK_ONLY
Definition: tasks.hpp:46
task::name
const char * name
Definition: tasks.hpp:40
tasks_sync_block
void tasks_sync_block(struct task_sync *ts)
Definition: tasks.cpp:560