vpr::SemaphoreNSPR Class Reference

Wrapper for semaphores implemented using condition variables. More...

#include <vpr/Sync/Semaphore.h>

Collaboration diagram for vpr::SemaphoreNSPR:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 SemaphoreNSPR (int initialValue=1)
 Custructor.
 ~SemaphoreNSPR ()
 Destructor.
vpr::ReturnStatus acquire ()
 Locks this semaphore.
vpr::ReturnStatus acquireRead ()
 Acquires a read lock on a resource protected by this semaphore.
vpr::ReturnStatus acquireWrite ()
 Acquires a write lock on a resource protected by this semaphore.
vpr::ReturnStatus tryAcquire ()
 Tries to acquire the a resource lock immediately (does not block).
vpr::ReturnStatus tryAcquireRead ()
 Tries to acquire a read lock on a resource (does not block).
vpr::ReturnStatus tryAcquireWrite ()
 Tries to acquire a write lock on a resource (does not block).
vpr::ReturnStatus release ()
 Releases a resource lock.
vpr::ReturnStatus reset (int val)
 Resets the resource count for this semaphore.
void dump (FILE *dest=stderr, const char *message="\n------ Semaphore Dump -----\n") const
 Dumps the semaphore debug stuff and current state.

Protected Member Functions

void operator= (const SemaphoreNSPR &)
 SemaphoreNSPR (const SemaphoreNSPR &)

Protected Attributes

CondVarmCondVar
 Semaphore simulator variable for the class.
PRInt32 mValue
 The resource count.

Detailed Description

Wrapper for semaphores implemented using condition variables.

This is typedef'd to vpr::Semaphore.

Definition at line 61 of file SemaphoreNSPR.h.


Constructor & Destructor Documentation

vpr::SemaphoreNSPR::SemaphoreNSPR ( int  initialValue = 1  )  [inline]

Custructor.

Postcondition:
The semaphore variable for the class is initilized as an unnamed semaphore.
Parameters:
initialValue The initial number of resources controlled by the semaphore. If not specified, the default value is 1.

Definition at line 73 of file SemaphoreNSPR.h.

00074    {
00075       mCondVar = new CondVar;
00076       PR_AtomicSet(&mValue, initialValue);
00077    }

vpr::SemaphoreNSPR::~SemaphoreNSPR (  )  [inline]

Destructor.

Postcondition:
The resources used by the semaphore variable are freed.

Definition at line 84 of file SemaphoreNSPR.h.

00085    {
00086       delete mCondVar;
00087    }

vpr::SemaphoreNSPR::SemaphoreNSPR ( const SemaphoreNSPR  )  [inline, protected]

Definition at line 285 of file SemaphoreNSPR.h.

00286    {
00287       /* Do nothing. */ ;
00288    }


Member Function Documentation

vpr::ReturnStatus vpr::SemaphoreNSPR::acquire (  )  [inline]

Locks this semaphore.

Postcondition:
The calling thread either acquires the semaphore until release() is called, or the caller is put at the tail of a wait and is suspended until such time as it can be freed and allowed to acquire the semaphore itself.
Returns:
vpr::ReturnStatus::Succeed is returned if a resource lock is acquired. vpr::ReturnStatus::Fail is returned otherwise.

Definition at line 100 of file SemaphoreNSPR.h.

00101    {
00102       mCondVar->acquire();
00103       PR_AtomicDecrement(&mValue);
00104 
00105       while ( mValue < 0 )
00106       {
00107          mCondVar->wait();
00108       }
00109       mCondVar->release();
00110 
00111       return vpr::ReturnStatus();
00112    }

vpr::ReturnStatus vpr::SemaphoreNSPR::acquireRead (  )  [inline]

Acquires a read lock on a resource protected by this semaphore.

Postcondition:
The calling thread either acquires the semaphore until release() is called, or the caller is put at the tail of a wait and is suspended until such time as it can be freed and allowed to acquire the semaphore itself.
Returns:
vpr::ReturnStatus::Succeed is returned if a resource read lock is acquired. vpr::ReturnStatus::Fail is returned otherwise.
Note:
There is no special read semaphore for now.

Definition at line 127 of file SemaphoreNSPR.h.

00128    {
00129       return this->acquire();
00130    }

vpr::ReturnStatus vpr::SemaphoreNSPR::acquireWrite (  )  [inline]

Acquires a write lock on a resource protected by this semaphore.

Postcondition:
The calling thread either acquires the semaphore until release() is called, or the caller is put at the tail of a wait and is suspended until such time as it can be freed and allowed to acquire the semaphore itself.
Returns:
vpr::ReturnStatus::Succeed is returned if a resource write lock is acquired. vpr::ReturnStatus::Fail is returned otherwise.
Note:
There is no special write semaphore for now.

Definition at line 145 of file SemaphoreNSPR.h.

00146    {
00147       return this->acquire();
00148    }

vpr::ReturnStatus vpr::SemaphoreNSPR::tryAcquire (  )  [inline]

Tries to acquire the a resource lock immediately (does not block).

Postcondition:
If the semaphore could be acquired by the caller, the caller gets control of the semaphore. If the semaphore was already locked, the routine returns immediately without suspending the calling thread.
Returns:
vpr::ReturnStatus::Succeed is returned if a lock is acquired. vpr::ReturnStatus::Fail is returned if no resource could be locked without blocking.

Definition at line 162 of file SemaphoreNSPR.h.

References vpr::ReturnStatus::Fail.

00163    {
00164       vpr::ReturnStatus status(vpr::ReturnStatus::Fail);
00165 
00166       if ( mValue >= 0 )
00167       {
00168          status = this->acquire();
00169       }
00170 
00171       return status;
00172    }

vpr::ReturnStatus vpr::SemaphoreNSPR::tryAcquireRead (  )  [inline]

Tries to acquire a read lock on a resource (does not block).

Postcondition:
If the semaphore could be acquired by the caller, the caller gets control of the semaphore. If the semaphore was already locked, the routine returns immediately without suspending the calling thread.
Returns:
vpr::ReturnStatus::Succeed is returned if a read lock is acquired. vpr::ReturnStatus::Fail is returned if no resource could be locked without blocking.

Definition at line 186 of file SemaphoreNSPR.h.

00187    {
00188       return this->tryAcquire();
00189    }

vpr::ReturnStatus vpr::SemaphoreNSPR::tryAcquireWrite (  )  [inline]

Tries to acquire a write lock on a resource (does not block).

Postcondition:
If the semaphore could be acquired by the caller, the caller gets control of the semaphore. If the semaphore was already locked, the routine returns immediately without suspending the calling thread.
Returns:
vpr::ReturnStatus::Succeed is returned if a write lock is acquired. vpr::ReturnStatus::Fail is returned if no resource could be locked without blocking.

Definition at line 203 of file SemaphoreNSPR.h.

00204    {
00205       return this->tryAcquire();
00206    }

vpr::ReturnStatus vpr::SemaphoreNSPR::release (  )  [inline]

Releases a resource lock.

Precondition:
The semaphore should have been locked before being released.
Postcondition:
The semaphore is released and the thread at the head of the wait queue is allowed to execute again.
Returns:
vpr::ReturnStatus::Succeed is returned if a resource is unlocked successfully. vpr::ReturnStatus::Fail is returned otherwise.

Definition at line 219 of file SemaphoreNSPR.h.

00220    {
00221       vpr::ReturnStatus status;
00222 
00223       mCondVar->acquire();
00224       PR_AtomicIncrement(&mValue);
00225       status = mCondVar->signal();
00226       mCondVar->release();
00227 
00228       return status;
00229    }

vpr::ReturnStatus vpr::SemaphoreNSPR::reset ( int  val  )  [inline]

Resets the resource count for this semaphore.

Postcondition:
This semaphore's count is set to the specified value.
Parameters:
val The value to which the semaphore is reset.
Returns:
vpr::ReturnStatus::Succeed is returned if the resource count is reset successfully. vpr::ReturnStatus::Fail is returned otherwise.
Note:
If processes are waiting on the semaphore, the results are undefined.

Definition at line 245 of file SemaphoreNSPR.h.

00246    {
00247       vpr::ReturnStatus status;
00248 
00249       mCondVar->acquire();
00250       PR_AtomicSet(&mValue, val);
00251       status = mCondVar->broadcast();
00252       mCondVar->release();
00253 
00254       return status;
00255    }

void vpr::SemaphoreNSPR::dump ( FILE *  dest = stderr,
const char *  message = "\n------ Semaphore Dump -----\n" 
) const [inline]

Dumps the semaphore debug stuff and current state.

Postcondition:
All important data and debugging information related to this semaphore is dumped to the specified file descriptor (or to stderr if none is given).
Parameters:
dest File descriptor to which the output will be written. It defaults to stderr if no descriptor is specified.
message Message printed out before the output is dumped.

Definition at line 268 of file SemaphoreNSPR.h.

00270    {
00271       fprintf(dest, "%s", message);
00272       fprintf(dest, "Current semaphore value: %d", mValue);
00273    }

void vpr::SemaphoreNSPR::operator= ( const SemaphoreNSPR  )  [inline, protected]

Definition at line 280 of file SemaphoreNSPR.h.

00281    {
00282       /* Do nothing. */ ;
00283    }


Member Data Documentation

CondVar* vpr::SemaphoreNSPR::mCondVar [protected]

Semaphore simulator variable for the class.

Definition at line 276 of file SemaphoreNSPR.h.

PRInt32 vpr::SemaphoreNSPR::mValue [protected]

The resource count.

Definition at line 277 of file SemaphoreNSPR.h.


The documentation for this class was generated from the following file:
Generated on Thu Jan 4 10:55:28 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1