The Pedigree Project  0.1
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Semaphore Class Reference

#include <Semaphore.h>

+ Inheritance diagram for Semaphore:
+ Collaboration diagram for Semaphore:

Classes

class  SemaphoreEvent
 

Public Types

enum  SemaphoreError { TimedOut, Interrupted }
 
typedef Result< bool, SemaphoreError > SemaphoreResult
 

Public Member Functions

 Semaphore (size_t nInitialValue, bool canInterrupt=true)
 
virtual ~Semaphore ()
 
SemaphoreResult acquireWithResult (size_t n=1, size_t timeoutSecs=0, size_t timeoutUsecs=0)
 
bool acquire (size_t n=1, size_t timeoutSecs=0, size_t timeoutUsecs=0)
 
bool tryAcquire (size_t n=1)
 
void release (size_t n=1)
 
ssize_t getValue ()
 

Private Member Functions

 Semaphore (const Semaphore &)
 
void operator= (const Semaphore &)
 
void removeThread (class Thread *pThread)
 

Private Attributes

size_t magic
 
Atomic< ssize_t > m_Counter
 
Spinlock m_BeingModified
 
List< class Thread * > m_Queue
 
bool m_bCanInterrupt
 

Detailed Description

A counting semaphore.

Definition at line 35 of file Semaphore.h.

Constructor & Destructor Documentation

Semaphore::Semaphore ( size_t  nInitialValue,
bool  canInterrupt = true 
)

Constructor

Parameters
nInitialValueThe initial value of the semaphore.
canInterruptIf false, acquire() retries after interrupt rather than returning a failure status.

Definition at line 70 of file Semaphore.cc.

References assert.

Semaphore::~Semaphore ( )
virtual

Destructor

Definition at line 77 of file Semaphore.cc.

References assert, and List< T, nodePoolSize >::clear().

Semaphore::Semaphore ( const Semaphore )
private

Private copy constructor

Note
NOT implemented.

Member Function Documentation

bool Semaphore::acquire ( size_t  n = 1,
size_t  timeoutSecs = 0,
size_t  timeoutUsecs = 0 
)
inline
Semaphore::SemaphoreResult Semaphore::acquireWithResult ( size_t  n = 1,
size_t  timeoutSecs = 0,
size_t  timeoutUsecs = 0 
)

Attempts to acquire n items from the semaphore. This will block until the semaphore is non-zero.

Parameters
nThe number of semaphore items required. Must be non-zero.
timeoutSecsTimeout value in seconds - if zero, no timeout.
Returns
a Result with a bool value indicating success if no timeout took place, a Result with a SemaphoreError error otherwise.

Definition at line 99 of file Semaphore.cc.

References Spinlock::acquire(), Timer::addAlarm(), Thread::addWakeupWatcher(), assert, Thread::Continue, Machine::getTimer(), Processor::information(), NOTICE, List< T, nodePoolSize >::pushBack(), Spinlock::release(), Timer::removeAlarm(), removeThread(), Thread::removeWakeupWatcher(), Thread::setDebugState(), Thread::setInterrupted(), Processor::setInterrupts(), Thread::setUnwindState(), tryAcquire(), and Thread::wasInterrupted().

ssize_t Semaphore::getValue ( )

Gets the current value of the semaphore

Definition at line 318 of file Semaphore.cc.

Referenced by RequestQueue::getNextRequest(), ConditionVariable::wait(), and PosixSubsystem::~PosixSubsystem().

+ Here is the caller graph for this function:

void Semaphore::operator= ( const Semaphore )
private

Private operator=

Note
NOT implemented.
void Semaphore::release ( size_t  n = 1)
void Semaphore::removeThread ( class Thread pThread)
private

Removes the given pointer from the thread queue.

Note
Assume no other pointers that match

Definition at line 83 of file Semaphore.cc.

References Spinlock::acquire(), List< T, nodePoolSize >::begin(), List< T, nodePoolSize >::end(), List< T, nodePoolSize >::erase(), and Spinlock::release().

Referenced by acquireWithResult().

+ Here is the caller graph for this function:

bool Semaphore::tryAcquire ( size_t  n = 1)

Attempts to acquire n items from the semaphore. This will not block.

Parameters
nThe number of semaphore items required. Must be non-zero.
Returns
True if acquire succeeded, false otherwise.

Definition at line 223 of file Semaphore.cc.

Referenced by acquireWithResult(), LockedFile::lock(), RequestQueue::work(), and PosixSubsystem::~PosixSubsystem().

+ Here is the caller graph for this function:


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