#include <vpr/Sync/Semaphore.h>
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. | |
This is typedef'd to vpr::Semaphore.
Definition at line 81 of file SemaphorePosix.h.
| vpr::SemaphorePosix::SemaphorePosix | ( | int | initialValue = 1 |
) | [inline] |
Constructor for vpr::SemaphorePosix class.
| 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.
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] |
| vpr::ReturnStatus vpr::SemaphorePosix::acquire | ( | ) | const [inline] |
Locks this semaphore.
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.
vpr::ReturnStatus::Fail is returnd if an error occurred.
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.
vpr::ReturnStatus::Fail is returnd if an error occurred.
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).
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).
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).
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.
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.
| val | The value to which the semaphore is reset. |
vpr::ReturnStatus::Fail is returned if this semaphore could not be reset.
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.
| 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] |
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().
1.5.1