1 #ifndef DOZERG_SEMAPHORE_H_20090318 2 #define DOZERG_SEMAPHORE_H_20090318 12 #include <sys/types.h> 16 #include <semaphore.h> 20 #include "tools/time.hh" 35 bool init(
unsigned int init_val,
bool pshared){
36 return (0 == ::sem_init(&sem_, pshared, init_val));
42 return (0 == ::sem_destroy(&sem_));
45 bool post(){
return (0 == ::sem_post(&sem_));}
47 bool wait(){
return (0 == ::sem_wait(&sem_));}
49 bool tryWait(){
return (0 == ::sem_trywait(&sem_));}
50 #ifdef __API_HAS_SEM_TIMEWAIT 52 bool timeWait(uint32_t timeMs){
54 return (tools::GetAbsTimespec(timeMs, &ts)
55 && 0 == ::sem_timedwait(&sem_, &ts));
61 if(0 == ::sem_getvalue(&sem_, &ret) && ret < 0)
73 static const int kModeDefault = S_IRUSR | S_IWUSR;
77 static bool Unlink(
const char * name){
79 return (0 == sem_unlink(name));
87 ,
unsigned int init_val = 0
89 , mode_t mode = kModeDefault)
95 sem_t * s = sem_open(name, oflag, mode, init_val);
104 bool valid()
const{
return (semp_ != NULL);}
106 bool post(){
return (valid() && 0 == sem_post(semp_));}
108 bool wait(){
return (valid() && 0 == sem_wait(semp_));}
110 bool tryWait(){
return (valid() && 0 == sem_trywait(semp_));}
111 #ifdef __API_HAS_SEM_TIMEWAIT 113 bool timeWait(uint32_t timeMs){
116 && tools::GetAbsTimespec(timeMs, &ts)
117 && 0 == sem_timedwait(semp_, &ts));
123 if(valid() && 0 == sem_getvalue(semp_, &ret) && ret < 0)
135 struct semid_ds *buf;
136 unsigned short *array;
137 struct seminfo *__buf;
140 static const int kFlagDefault = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
142 static sembuf GenOp(
int index,
int op,
bool wait,
bool undo){
148 sb.sem_flg |= IPC_NOWAIT;
150 sb.sem_flg |= SEM_UNDO;
160 explicit CXsiSemaphoreSet(key_t key,
int nsems = 0,
int semflg = kFlagDefault)
163 init(key, nsems, semflg);
167 CXsiSemaphoreSet(
const char * pathname,
int project,
int nsems = 0,
int semflg = kFlagDefault)
170 init(pathname, project, nsems, semflg);
172 bool init(key_t key,
int nsems,
int semflg){
175 semid_ = ::semget(key, nsems, semflg);
178 bool init(
const char * pathname,
int project,
int nsems,
int semflg){
181 key_t key = ::ftok(pathname, project);
184 return init(key, nsems, semflg);
186 bool valid()
const{
return (semid_ >= 0);}
194 if(0 > ::semctl(semid_, 0, IPC_STAT, arg))
196 return sem.sem_nsems;
199 bool getAll(std::vector<unsigned short> & results)
const{
202 const size_t sz = size();
207 arg.array = &results[0];
208 return (0 == ::semctl(semid_, 0, GETALL, arg));
211 bool setAll(
const std::vector<unsigned short> & values){
214 const size_t sz = size();
215 if(!sz || values.size() < sz)
218 arg.array =
const_cast<unsigned short *
>(&values[0]);
219 return (0 == ::semctl(semid_, 0, SETALL, arg));
222 bool setVal(
int index,
int val){
227 return (0 == ::semctl(semid_, index, SETVAL, arg));
233 int getVal(
int index)
const{
236 return ::semctl(semid_, index, GETVAL);
243 bool apply(
int index,
int op,
bool wait,
bool undo){
246 sembuf sb(GenOp(index, op, wait, undo));
247 return (0 == ::semop(semid_, &sb, 1));
250 bool apply(
const std::vector<sembuf> & ops){
255 return (0 == ::semop(semid_, const_cast<sembuf *>(&ops[0]), ops.size()));
257 #ifdef __API_HAS_SEMTIMEDOP 259 bool timeApply(
int index,
int op,
bool wait,
bool undo, uint32_t timeMs){
262 sembuf sb(GenOp(index, op, wait, undo));
264 tools::GetRelativeTimespec(timeMs, &ts);
265 return (0 == ::semtimedop(semid_, &sb, 1, &ts));
267 bool timeApply(
const std::vector<sembuf> & ops, uint32_t timeMs){
273 tools::GetRelativeTimespec(timeMs, &ts);
274 return (0 == ::semtimedop(semid_, const_cast<sembuf *>(&ops[0]), ops.size(), &ts));
280 ::semctl(semid_, 0, IPC_RMID);
Definition: semaphore.hh:24
Definition: semaphore.hh:69
Definition: semaphore.hh:131