PLearn 0.1
|
#include <Semaphores.h>
Public Member Functions | |
CountEventsSemaphore (int nb_semaphores=1) | |
construct given number of semaphores for event counters | |
CountEventsSemaphore (SemId semid) | |
access an existing semaphore | |
void | signal (int type=0) |
Signal the occurence of event of given "type", where type can range from 0 to n_semaphores-1. | |
void | wait (int n_occurences, int type=0) |
int | value (int type=0) |
return current value of counter for given "type" of event | |
void | setValue (int value, int resource=0) |
set value of counter | |
~CountEventsSemaphore () | |
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 131 of file Semaphores.h.
PLearn::CountEventsSemaphore::CountEventsSemaphore | ( | int | nb_semaphores = 1 | ) |
construct given number of semaphores for event counters
Definition at line 137 of file Semaphores.cc.
References PLearn::endl(), i, n_semaphores, PLERROR, and PLearn::semun::val.
: owner(true), n_semaphores(nb_semaphores) { // allocate a semaphore int rv=semget(IPC_PRIVATE, n_semaphores, 0666 | IPC_CREAT); if (rv == -1) PLERROR("CountEventsSemaphore::CountEventsSemaphore(%d) semget returns -1, %s", nb_semaphores,strerror(errno)); else id.id=rv; cout << "allocated semaphore " << id.id << endl; // set the semaphore values to 0 (meaning that initially the counters are at 0) semun v; v.val=0; for (int i=0;i<n_semaphores;i++) { rv=semctl(id.id,i,SETVAL,v); if (rv == -1) PLERROR("CountEventsSemaphore::CountEventsSemaphore(%d) semctl returns -1, %s", nb_semaphores,strerror(errno)); } }
PLearn::CountEventsSemaphore::CountEventsSemaphore | ( | SemId | semid | ) |
access an existing semaphore
Definition at line 160 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("CountEventsSemaphore:: slave CountEventsSemaphore(%d) semctl returns -1, %s", id.id,strerror(errno)); n_semaphores = u.buf->sem_nsems; }
PLearn::CountEventsSemaphore::~CountEventsSemaphore | ( | ) |
release the semaphore upon destruction of object
Definition at line 215 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("CountEventsSemaphore::~CountEventsSemaphore semctl failed, %s", strerror(errno)); cout << "released semaphore " << id.id << endl; } }
set value of counter
Definition at line 206 of file Semaphores.cc.
References PLERROR, PLearn::semun::val, and value().
{ semun v; v.val=value; int rv=semctl(id.id,resource,SETVAL,v); if (rv == -1) PLERROR("ResourceSemaphore::setValue(%d,%d) semctl returns -1, %s", value,resource,strerror(errno)); }
void PLearn::CountEventsSemaphore::signal | ( | int | type = 0 | ) |
Signal the occurence of event of given "type", where type can range from 0 to n_semaphores-1.
No waiting.
Definition at line 173 of file Semaphores.cc.
References PLERROR.
{ struct sembuf op; op.sem_num = type; op.sem_op = 1; // increment value op.sem_flg = 0; int rv=semop(id.id,&op,1); if (rv == -1) PLERROR("CountEventsSemaphore::signal(%d) semop failed, %s", type,strerror(errno)); }
return current value of counter for given "type" of event
Definition at line 185 of file Semaphores.cc.
References PLearn::semun::val.
Referenced by setValue().
{ semun v; v.val=0; return semctl(id.id,type,GETVAL,v); }
Wait for n_occurences of event of given "type" to have occured, where "type" can range from 0 to n_semaphoes-1. When this occurs, the event count for this type of event is reset to 0.
Definition at line 191 of file Semaphores.cc.
References PLERROR.
{ struct sembuf op; op.sem_num = type; op.sem_op = -n_occurences; // wait until n_occurences is reached op.sem_flg = 0; int rv=0; do // this loop is to deal with possible interrupts which rv=semop(id.id,&op,1); // will force return of semop before while (rv==-1 && errno==EINTR); // the count is reached if (rv == -1) PLERROR("CountEventsSemaphore::wait(%d,%d) semop failed, %s", n_occurences,type,strerror(errno)); }
SemId PLearn::CountEventsSemaphore::id [protected] |
semaphore id provided by the operating system at construction
Definition at line 133 of file Semaphores.h.
int PLearn::CountEventsSemaphore::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 136 of file Semaphores.h.
Referenced by CountEventsSemaphore().
bool PLearn::CountEventsSemaphore::owner [protected] |
true if this process is the owner of the semaphore, i.e.
Definition at line 134 of file Semaphores.h.
Referenced by ~CountEventsSemaphore().