PLearn 0.1
|
#include <Semaphores.h>
Public Member Functions | |
ResourceSemaphore (int nb_semaphores=1) | |
construct given number of semaphores for controlling so many exclusive resources. | |
ResourceSemaphore (SemId semid) | |
access an existing semaphore | |
void | lock (int resource=0) |
void | unlock (int resource=0) |
Release a lock on a resource. | |
bool | locked (int resource=0) |
to check without waiting wether a resource is locked. | |
void | clearAnyLock () |
make sure we are back in unlocked mode: clear locks of all resources | |
void | clearAnyLock (int resource) |
make sure we are back in unlocked mode: clear lock for given resource | |
~ResourceSemaphore () | |
release the semaphore upon destruction of object | |
Protected Attributes | |
SemId | id |
semaphore id provided by the operating system at construction | |
bool | owner |
true if this process is the owner of the semaphore, i.e. | |
int | n_semaphores |
the semaphore will be released with this object is deleted |
Definition at line 94 of file Semaphores.h.
PLearn::ResourceSemaphore::ResourceSemaphore | ( | int | nb_semaphores = 1 | ) |
construct given number of semaphores for controlling so many exclusive resources.
Definition at line 49 of file Semaphores.cc.
References clearAnyLock(), PLearn::endl(), n_semaphores, and PLERROR.
: owner(true), n_semaphores(nb_semaphores) { // allocate a semaphore int rv=semget(IPC_PRIVATE, n_semaphores, 0666 | IPC_CREAT); if (rv == -1) PLERROR("ResourceSemaphore::ResourceSemaphore(%d) semget returns -1, %s", nb_semaphores,strerror(errno)); else id.id=rv; cout << "allocated semaphore " << id.id << endl; clearAnyLock(); // sets all the semaphore values to 1 }
PLearn::ResourceSemaphore::ResourceSemaphore | ( | SemId | semid | ) |
access an existing semaphore
Definition at line 62 of file Semaphores.cc.
References n_semaphores, PLERROR, and u.
: id(semid), owner(false) { struct semid_ds buf; semun u; u.buf = &buf; int r=semctl(id.id,0,IPC_STAT,u); if (r == -1) PLERROR("ResourceSemaphore:: slave ResourceSemaphore(%d) semctl returns -1, %s", id.id,strerror(errno)); n_semaphores = u.buf->sem_nsems; }
PLearn::ResourceSemaphore::~ResourceSemaphore | ( | ) |
release the semaphore upon destruction of object
Definition at line 124 of file Semaphores.cc.
References PLearn::endl(), owner, PLERROR, and PLearn::semun::val.
{ if (owner) { semun v; v.val=0; int rv=semctl(id.id,0,IPC_RMID,v); if (rv == -1) PLERROR("ResourceSemaphore::~ResourceSemaphore semctl failed, %s", strerror(errno)); cout << "released semaphore " << id.id << endl; } }
void PLearn::ResourceSemaphore::clearAnyLock | ( | ) |
make sure we are back in unlocked mode: clear locks of all resources
Definition at line 108 of file Semaphores.cc.
References i, and n_semaphores.
Referenced by ResourceSemaphore().
{ for (int i=0;i<n_semaphores;i++) clearAnyLock(i); }
void PLearn::ResourceSemaphore::clearAnyLock | ( | int | resource | ) |
make sure we are back in unlocked mode: clear lock for given resource
Definition at line 114 of file Semaphores.cc.
References PLERROR, and PLearn::semun::val.
{ // set the semaphore values to 1 (meaning they are in "unlocked" state") semun v; v.val=1; int rv=semctl(id.id,resource,SETVAL,v); if (rv == -1) PLERROR("ResourceSemaphore::clearAnyLock(%d) semctl returns -1, %s", resource,strerror(errno)); }
void PLearn::ResourceSemaphore::lock | ( | int | resource = 0 | ) |
Wait for a resource to be freed and then take control of it. This is done by waiting for the resource semaphore's to become 1 and then decrementing its value to 0 (thus taking the lock).
Definition at line 74 of file Semaphores.cc.
References PLERROR.
{ struct sembuf op; op.sem_num = resource; op.sem_op = -1; // wait for value = 1 and then decrement to zero op.sem_flg = 0; int rv=semop(id.id,&op,1); if (rv == -1) PLERROR("ResourceSemaphore::lock(%d) semop failed, %s", resource,strerror(errno)); }
to check without waiting wether a resource is locked.
Definition at line 101 of file Semaphores.cc.
References PLearn::semun::val.
Referenced by unlock().
{ semun v; v.val=0; int value=semctl(id.id,resource,GETVAL,v); return (value==0); }
void PLearn::ResourceSemaphore::unlock | ( | int | resource = 0 | ) |
Release a lock on a resource.
This is done by incrementing the resource's semaphore. No waiting.
Definition at line 86 of file Semaphores.cc.
References locked(), and PLERROR.
{ if (!locked(resource)) PLERROR("ResourceSemaphore::unlock(%d), trying to unlock an unlocked resource", resource); struct sembuf op; op.sem_num = resource; op.sem_op = 1; // increment value back to 1 op.sem_flg = 0; int rv=semop(id.id,&op,1); if (rv == -1) PLERROR("ResourceSemaphore::unlock(%d) semop failed, %s", resource,strerror(errno)); }
SemId PLearn::ResourceSemaphore::id [protected] |
semaphore id provided by the operating system at construction
Definition at line 96 of file Semaphores.h.
int PLearn::ResourceSemaphore::n_semaphores [protected] |
the semaphore will be released with this object is deleted
there can be several semaphores in the same object
Definition at line 99 of file Semaphores.h.
Referenced by clearAnyLock(), and ResourceSemaphore().
bool PLearn::ResourceSemaphore::owner [protected] |
true if this process is the owner of the semaphore, i.e.
Definition at line 97 of file Semaphores.h.
Referenced by ~ResourceSemaphore().