vpr::SemaphorePosix Class Reference

Semaphore wrapper for POSIX.4-compliant systems. More...

#include <vpr/Sync/Semaphore.h>

List of all members.

Public Member Functions

 SemaphorePosix (int initialValue=1)
 Constructor for vpr::SemaphorePosix class.
 ~SemaphorePosix ()
 Destructor for vpr::SemaphorePosix class.
vpr::ReturnStatus acquire () const
 Locks this semaphore.
vpr::ReturnStatus acquireRead () const
 Acquires a read lock on this semaphore.
vpr::ReturnStatus acquireWrite () const
 Acquires a write lock on this semaphore.
vpr::ReturnStatus tryAcquire () const
 Tries to acquire the semaphore immediately (does not block).
vpr::ReturnStatus tryAcquireRead () const
 Tries to acquire a read lock on this semaphore (does not block).
vpr::ReturnStatus tryAcquireWrite () const
 Tries to acquire a write lock on this semaphore (does not block).
vpr::ReturnStatus release () const
 Releases this semaphore.
vpr::ReturnStatus reset (int val)
 Resets the semaphore using the given value.
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 SemaphorePosix &)
 SemaphorePosix (const SemaphorePosix &)

Protected Attributes

sem_t * mSema
 Semaphore variable for the class.


Detailed Description

Semaphore wrapper for POSIX.4-compliant systems.

This is typedef'd to vpr::Semaphore.

Definition at line 81 of file SemaphorePosix.h.


Constructor & Destructor Documentation

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

Constructor for vpr::SemaphorePosix class.

Postcondition:
The semaphore variable for this object is initialized as an unnamed semaphore on all platforms except Darwin (Mac OS X). On Darwin 7.5 and earlier, unnamed semaphores are not supported.
Parameters:
initialValue The initial number of resources controlled by the semaphore. If not specified, the default value is 1.

Definition at line 96 of file SemaphorePosix.h.

References mSema.

00097    {
00098 #ifdef VPR_USE_NAMED_SEMAPHORE
00099       // Use strdup(3) here so that mktemp(3) can modify the memory.  Trying
00100       // to modify a constant string would be bad.
00101       // NOTE: The memory allocated by strdup(3) will be released in the
00102       // destructor.
00103       mSemaFile = mktemp(strdup("/tmp/vprsema.XXXXXX"));
00104 
00105       // ----- Allocate the named semaphore ----- //
00106       mSema = sem_open(mSemaFile, O_CREAT, 0600, initialValue);
00107 
00108       if ( mSema == (sem_t*) SEM_FAILED )
00109       {
00110          perror("sem_init() error");
00111       }
00112 #else
00113       // ----- Allocate the unnamed semaphore ----- //
00114       mSema = (sem_t*) malloc(sizeof(sem_t));
00115 
00116       if ( sem_init(mSema, 0, initialValue) != 0 )
00117       {
00118          perror("sem_init() error");
00119       }
00120 #endif
00121    }

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

Destructor for vpr::SemaphorePosix class.

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

Definition at line 128 of file SemaphorePosix.h.

References mSema.

00129    {
00130       // ---- Delete the semaphore --- //
00131 #ifdef VPR_USE_NAMED_SEMAPHORE
00132       if ( sem_close(mSema) != 0 )
00133       {
00134          perror("sem_close() error");
00135       }
00136 
00137       sem_unlink(mSemaFile);
00138       free(mSemaFile);
00139 #else
00140       if ( sem_destroy(mSema) != 0 )
00141       {
00142          perror("sem_destroy() error");
00143       }
00144 
00145       free(mSema);
00146 #endif
00147    }

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

Definition at line 377 of file SemaphorePosix.h.

00378    {
00379       /* Do nothing. */ ;
00380    }


Member Function Documentation

vpr::ReturnStatus vpr::SemaphorePosix::acquire (  )  const [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 the lock is acquired.

vpr::ReturnStatus::Fail is returnd if an error occurred.

Definition at line 160 of file SemaphorePosix.h.

References errno, vpr::ReturnStatus::Fail, and mSema.

Referenced by acquireRead(), acquireWrite(), vpr::ThreadPool::getThread(), vpr::ThreadPool::threadSleep(), and vpr::CondVarGeneric::wait().

00161    {
00162       int result;
00163 
00164       do
00165       {
00166          result = sem_wait(mSema);
00167       }
00168       while ( result == -1 && errno == EINTR );
00169 
00170       if ( result == 0 )
00171       {
00172          return vpr::ReturnStatus();
00173       }
00174       else
00175       {
00176          perror("sem_wait() error");
00177          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00178       }
00179    }

vpr::ReturnStatus vpr::SemaphorePosix::acquireRead (  )  const [inline]

Acquires a read lock on 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 the read lock is acquired.

vpr::ReturnStatus::Fail is returnd if an error occurred.

Note:
There is no special read lock for now.

Definition at line 195 of file SemaphorePosix.h.

References acquire().

00196    {
00197       return this->acquire();
00198    }

vpr::ReturnStatus vpr::SemaphorePosix::acquireWrite (  )  const [inline]

Acquires a write lock on 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 the write lock is acquired.

vpr::ReturnStatus::Fail is returnd if an error occurred.

Note:
There is no special write lock for now.

Definition at line 214 of file SemaphorePosix.h.

References acquire().

00215    {
00216       return this->acquire();
00217    }

vpr::ReturnStatus vpr::SemaphorePosix::tryAcquire (  )  const [inline]

Tries to acquire the semaphore 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 the lock on this semaphore is acquired.

vpr::ReturnStatus::Fail is returned if the lock is not acquired.

Definition at line 231 of file SemaphorePosix.h.

References vpr::ReturnStatus::Fail, and mSema.

Referenced by tryAcquireRead(), and tryAcquireWrite().

00232    {
00233       if ( sem_trywait(mSema) == 0 )
00234       {
00235          return vpr::ReturnStatus();
00236       }
00237       else
00238       {
00239          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00240       }
00241    }

vpr::ReturnStatus vpr::SemaphorePosix::tryAcquireRead (  )  const [inline]

Tries to acquire a read lock on this semaphore (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 the lock on this semaphore is acquired.

vpr::ReturnStatus::Fail is returned if the lock is not acquired.

Definition at line 255 of file SemaphorePosix.h.

References tryAcquire().

00256    {
00257       return this->tryAcquire();
00258    }

vpr::ReturnStatus vpr::SemaphorePosix::tryAcquireWrite (  )  const [inline]

Tries to acquire a write lock on this semaphore (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 the lock on this semaphore is acquired.

vpr::ReturnStatus::Fail is returned if the lock is not acquired.

Definition at line 272 of file SemaphorePosix.h.

References tryAcquire().

00273    {
00274       return this->tryAcquire();
00275    }

vpr::ReturnStatus vpr::SemaphorePosix::release (  )  const [inline]

Releases this semaphore.

Precondition:
The semaphore should have been locked before being released.
Postcondition:
The semaphore is released and the thread at the haed of the wait queue is allowed to execute again.
Returns:
vpr::ReturnStatus::Succeed is returned if the lock on this semaphore is released.

vpr::ReturnStatus::Fail is returned if an error occurred.

Definition at line 288 of file SemaphorePosix.h.

References vpr::ReturnStatus::Fail, and mSema.

Referenced by vpr::ThreadPool::startFunc(), and vpr::ThreadPool::threadSleep().

00289    {
00290       if ( sem_post(mSema) == 0 )
00291       {
00292          return vpr::ReturnStatus();
00293       }
00294       else
00295       {
00296          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00297       }
00298    }

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

Resets the semaphore using the given value.

Postcondition:
The 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 this semaphore is reset with the given value successfully.

vpr::ReturnStatus::Fail is returned if this semaphore could not be reset.

Note:
If processes are waiting on the semaphore, the results are undefined.

Definition at line 315 of file SemaphorePosix.h.

References vpr::ReturnStatus::Fail, mSema, and vpr::ReturnStatus::setCode().

00316    {
00317       vpr::ReturnStatus status;
00318 
00319 #ifdef VPR_USE_NAMED_SEMAPHORE
00320       // First destroy the current semaphore.
00321       sem_unlink(mSemaFile);
00322 
00323       // Now recreate it with the new value in val.
00324       mSema = sem_open(mSemaFile, O_CREAT, 0600, val);
00325 
00326       if ( mSema == (sem_t*) SEM_FAILED )
00327       {
00328          perror("sem_init() error");
00329          status.setCode(vpr::ReturnStatus::Fail);
00330       }
00331 #else
00332       // First destroy the current semaphore.
00333       sem_destroy(mSema);
00334 
00335       // Now recreate it with the new value in val.
00336       if ( sem_init(mSema, 0, val) != 0 )
00337       {
00338          status.setCode(vpr::ReturnStatus::Fail);
00339       }
00340 #endif
00341 
00342       return status;
00343    }

void vpr::SemaphorePosix::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 the 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 356 of file SemaphorePosix.h.

References mSema.

Referenced by vpr::CondVarGeneric::wait().

00358    {
00359       int value;
00360       sem_getvalue(mSema, &value);
00361       fprintf(dest, "%s", message);
00362       fprintf(dest, "Current semaphore value: %d", value);
00363    }

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

Definition at line 372 of file SemaphorePosix.h.

00373    {
00374       /* Do nothing. */ ;
00375    }


Member Data Documentation

sem_t* vpr::SemaphorePosix::mSema [protected]

Semaphore variable for the class.

Definition at line 369 of file SemaphorePosix.h.

Referenced by acquire(), dump(), release(), reset(), SemaphorePosix(), tryAcquire(), and ~SemaphorePosix().


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