#ifdef _TRY_POSIX /// Try posix. #include "SemaphoreWrapper.h" #include #include #include #include #include #include #include #include "log.h" class ProcessSemaphore::impl { public: sem_t* ps; int status; }; ProcessSemaphore::ProcessSemaphore(int key, int action, int default_value) : _p(new impl) { if(_p) { _p->status=0; char buff[64]; memset(buff,0,64); sprintf(buff,"LibSemaphoreWrapper_%d",key); if(action==1) /// Must Create New. { _p->ps=sem_open(buff,O_CREAT|O_EXCL,0777,default_value); } else /// Must Open { _p->ps=sem_open(buff,O_RDWR,0777,0); } if(_p->ps!=SEM_FAILED) { _p->status=2; } else { /// Notice: Most platform reports "Function not implemented" on sem_open. dprintf("Failed to sem_open. %s\n",strerror(errno)); } } } ProcessSemaphore::~ProcessSemaphore() { if(_p) { /// Notice that sem_close() is used for named semaphore and sem_destroy() is for unnamed semaphore. if(isReady()) { sem_close(_p->ps); } delete _p; } } bool ProcessSemaphore::isReady() const { return _p&&_p->status==2; } int ProcessSemaphore::p() { return sem_wait(_p->ps); } int ProcessSemaphore::v() { return sem_post(_p->ps); } int ProcessSemaphore::wait() { return p(); } int ProcessSemaphore::notify() { return v(); } #endif /// End of Try Posix