Kicking it Olde Sküül! Burgerlib on Github Follow Olde Sküül on Twitter Burgerbecky on LinkedIn Burgerbecky on LinkedIn
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | Private Member Functions | List of all members
Burger::Semaphore Class Reference

Class for creating a semaphore. More...

Public Member Functions

 Semaphore (uint32_t uCount=0) noexcept
 Initialize a semaphore.
 
 ~Semaphore ()
 Shut down a semaphore.
 
eError signal (void) noexcept
 Signal a semaphore.
 
eError wait_for_signal (uint32_t uMilliseconds=0) noexcept
 Wait for a resource with a timeout.
 
uint32_t get_value (void) const noexcept
 Get the current number of available resources.
 

Protected Attributes

volatile uint32_t m_uCount
 Semaphore count value.
 

Private Member Functions

 Semaphore (const Semaphore &)=delete
 
Semaphoreoperator= (const Semaphore &)=delete
 
 Semaphore (Semaphore &&)=delete
 
Semaphoreoperator= (Semaphore &&)=delete
 

Detailed Description

Class for creating a semaphore.


In a multiprocessor system, it's necessary to have multiple resources available to multiple threads at the same time. To prevent resource starvation, this class is decremented until the number of available resources is exhausted and then it will halt future threads from executing until resources are made available via calls to signal().

Further reading http://en.wikipedia.org/wiki/Semaphore_(programming)

Note
On operating systems that don't have native semaphore support, such as MSDOS, this class will always return error codes for all calls.
See also
Mutex and Thread

Constructor & Destructor Documentation

◆ Semaphore() [1/3]

Burger::Semaphore::Semaphore ( const Semaphore & )
privatedelete

◆ Semaphore() [2/3]

Burger::Semaphore::Semaphore ( Semaphore && )
privatedelete

◆ Semaphore() [3/3]

Burger::Semaphore::Semaphore ( uint32_t uCount = 0)
noexcept

Initialize a semaphore.


Query the operating system for a semaphore and initialize it to the initial value.

Parameters
uCountInitial number of resources available (0 means a empty semaphore)
See also
~Semaphore()

◆ ~Semaphore()

Burger::Semaphore::~Semaphore ( )

Shut down a semaphore.


Release any operating system resources allocated in the creation of the semaphore.

Note
Care should be exercised in ensuring that all threads are are waiting on semaphores have been shutdown down already.
See also
Semaphore(uint32_t)

Member Function Documentation

◆ get_value()

uint32_t Burger::Semaphore::get_value ( void ) const
inlinenoexcept

Get the current number of available resources.


Returns
The number of available resources.
See also
Semaphore(uint32_t)

◆ operator=() [1/2]

Semaphore & Burger::Semaphore::operator= ( const Semaphore & )
privatedelete

◆ operator=() [2/2]

Semaphore & Burger::Semaphore::operator= ( Semaphore && )
privatedelete

◆ signal()

Burger::eError BURGER_API Burger::Semaphore::signal ( void )
noexcept

Signal a semaphore.


When a resource is made available, signal that one is available with this call. This function will increment the count, and may release a waiting thread.

Returns
Zero on success, nonzero in the case of a semaphore failure
See also
wait_for_signal(uint32_t)

◆ wait_for_signal()

Burger::eError BURGER_API Burger::Semaphore::wait_for_signal ( uint32_t uMilliseconds = 0)
noexcept

Wait for a resource with a timeout.


If the semaphore's resource count has not gone to zero or less, decrement the count and immediately return. Otherwise, block until another thread posts to the semaphore or the time in milliseconds has elapsed. If the timeout is zero, do not block.

Parameters
uMillisecondsNumber of milliseconds to wait for the resource, 0 means no wait, UINT32_MAX means never time out
Returns
Zero on success, kErrorTimeout on a timeout, or other error code in case of a semaphore failure
See also
signal(void)

Member Data Documentation

◆ m_uCount

volatile uint32_t Burger::Semaphore::m_uCount
protected

Semaphore count value.