Mark3 Realtime Kernel
Mark3::Semaphore Class Reference

the Semaphore class provides Binary & Counting semaphore objects, based on BlockingObject base class. More...

#include <ksemaphore.h>

Inheritance diagram for Mark3::Semaphore:
Mark3::BlockingObject

Public Member Functions

void * operator new (size_t sz, void *pv)
 
 ~Semaphore ()
 
void Init (uint16_t u16InitVal_, uint16_t u16MaxVal_)
 Initialize a semaphore before use. Must be called before attempting post/pend operations on the object. More...
 
bool Post ()
 Increment the semaphore count. If the semaphore count is zero at the time this is called, and there are threads blocked on the object, this will immediately unblock the highest-priority blocked Thread. More...
 
void Pend ()
 Decrement the semaphore count. If the count is zero, the calling Thread will block until the semaphore is posted, and the Thread's priority is higher than that of any other Thread blocked on the object. More...
 
uint16_t GetCount ()
 Return the current semaphore counter. This can be usedd by a thread to bypass blocking on a semaphore - allowing it to do other things until a non-zero count is returned, instead of blocking until the semaphore is posted. More...
 
bool Pend (uint32_t u32WaitTimeMS_)
 Decrement the semaphore count. If the count is zero, the thread will block until the semaphore is pended. If the specified interval expires before the thread is unblocked, then the status is returned back to the user. More...
 
void WakeMe (Thread *pclChosenOne_)
 Wake a thread blocked on the semaphore. This is an internal function used for implementing timed semaphores relying on timer callbacks. Since these do not have access to the private data of the semaphore and its base classes, we have to wrap this as a public method - do not used this for any other purposes. More...
 
- Public Member Functions inherited from Mark3::BlockingObject
 BlockingObject ()
 
 ~BlockingObject ()
 

Private Member Functions

uint8_t WakeNext ()
 Wake the next thread waiting on the semaphore. Used internally. More...
 
bool Pend_i (uint32_t u32WaitTimeMS_)
 Pend_i. More...
 

Private Attributes

uint16_t m_u16Value
 Current count held by the semaphore. More...
 
uint16_t m_u16MaxValue
 Maximum count that can be held by this semaphore. More...
 

Additional Inherited Members

- Protected Member Functions inherited from Mark3::BlockingObject
void Block (Thread *pclThread_)
 Block Blocks a thread on this object. This is the fundamental operation performed by any sort of blocking operation in the operating system. All semaphores/mutexes/sleeping/messaging/etc ends up going through the blocking code at some point as part of the code that manages a transition from an "active" or "waiting" thread to a "blocked" thread. More...
 
void BlockPriority (Thread *pclThread_)
 BlockPriority Same as Block(), but ensures that threads are added to the block-list in priority-order, which optimizes the unblock procedure. More...
 
void UnBlock (Thread *pclThread_)
 UnBlock Unblock a thread that is already blocked on this object, returning it to the "ready" state by performing the following steps: More...
 
void SetInitialized (void)
 SetInitialized. More...
 
bool IsInitialized (void)
 IsInitialized. More...
 
- Protected Attributes inherited from Mark3::BlockingObject
ThreadList m_clBlockList
 
uint8_t m_u8Initialized
 
- Static Protected Attributes inherited from Mark3::BlockingObject
static constexpr auto m_uBlockingInvalidCookie = uint8_t { 0x3C }
 
static constexpr auto m_uBlockingInitCookie = uint8_t { 0xC3 }
 

Detailed Description

the Semaphore class provides Binary & Counting semaphore objects, based on BlockingObject base class.

Examples:
lab4_semaphores/main.cpp, lab6_timers/main.cpp, and lab9_dynamic_threads/main.cpp.

Definition at line 36 of file ksemaphore.h.

Constructor & Destructor Documentation

◆ ~Semaphore()

Mark3::Semaphore::~Semaphore ( )

Definition at line 58 of file ksemaphore.cpp.

Member Function Documentation

◆ GetCount()

uint16_t Mark3::Semaphore::GetCount ( )

Return the current semaphore counter. This can be usedd by a thread to bypass blocking on a semaphore - allowing it to do other things until a non-zero count is returned, instead of blocking until the semaphore is posted.

Returns
The current semaphore counter value.

Definition at line 206 of file ksemaphore.cpp.

◆ Init()

void Mark3::Semaphore::Init ( uint16_t  u16InitVal_,
uint16_t  u16MaxVal_ 
)

Initialize a semaphore before use. Must be called before attempting post/pend operations on the object.

This initialization is required to configure the behavior of the semaphore with regards to the initial and maximum values held by the semaphore. By providing access to the raw initial and maximum count elements of the semaphore, these objects are able to be used as either counting or binary semaphores.

To configure a semaphore object for use as a binary semaphore, set values of 0 and 1 respectively for the initial/maximum value parameters.

Any other combination of values can be used to implement a counting semaphore.

Parameters
u16InitVal_Initial value held by the semaphore
u16MaxVal_Maximum value for the semaphore. Must be nonzero.
Examples:
lab6_timers/main.cpp, and lab9_dynamic_threads/main.cpp.

Definition at line 94 of file ksemaphore.cpp.

◆ operator new()

void* Mark3::Semaphore::operator new ( size_t  sz,
void *  pv 
)
inline

Definition at line 39 of file ksemaphore.h.

◆ Pend() [1/2]

void Mark3::Semaphore::Pend ( )

Decrement the semaphore count. If the count is zero, the calling Thread will block until the semaphore is posted, and the Thread's priority is higher than that of any other Thread blocked on the object.

Examples:
lab4_semaphores/main.cpp.

Definition at line 194 of file ksemaphore.cpp.

◆ Pend() [2/2]

bool Mark3::Semaphore::Pend ( uint32_t  u32WaitTimeMS_)

Decrement the semaphore count. If the count is zero, the thread will block until the semaphore is pended. If the specified interval expires before the thread is unblocked, then the status is returned back to the user.

Returns
true - semaphore was acquired before the timeout false - timeout occurred before the semaphore was claimed.

Definition at line 200 of file ksemaphore.cpp.

◆ Pend_i()

bool Mark3::Semaphore::Pend_i ( uint32_t  u32WaitTimeMS_)
private

Pend_i.

Internal function used to abstract timed and untimed semaphore pend operations.

Parameters
u32WaitTimeMS_Time in MS to wait
Returns
true on success, false on failure.

Definition at line 152 of file ksemaphore.cpp.

◆ Post()

bool Mark3::Semaphore::Post ( )

Increment the semaphore count. If the semaphore count is zero at the time this is called, and there are threads blocked on the object, this will immediately unblock the highest-priority blocked Thread.

Note that if the priority of that Thread is higher than the current thread's priority, a context switch will occur and control will be relinquished to that Thread.

Returns
true if the semaphore was posted, false if the count is already maxed out.
Examples:
lab4_semaphores/main.cpp, and lab6_timers/main.cpp.

Definition at line 108 of file ksemaphore.cpp.

◆ WakeMe()

void Mark3::Semaphore::WakeMe ( Thread pclChosenOne_)

Wake a thread blocked on the semaphore. This is an internal function used for implementing timed semaphores relying on timer callbacks. Since these do not have access to the private data of the semaphore and its base classes, we have to wrap this as a public method - do not used this for any other purposes.

Definition at line 68 of file ksemaphore.cpp.

◆ WakeNext()

uint8_t Mark3::Semaphore::WakeNext ( )
private

Wake the next thread waiting on the semaphore. Used internally.

Definition at line 78 of file ksemaphore.cpp.

Member Data Documentation

◆ m_u16MaxValue

uint16_t Mark3::Semaphore::m_u16MaxValue
private

Maximum count that can be held by this semaphore.

Definition at line 140 of file ksemaphore.h.

◆ m_u16Value

uint16_t Mark3::Semaphore::m_u16Value
private

Current count held by the semaphore.

Definition at line 139 of file ksemaphore.h.


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