X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fsemaphore.cpp;fp=source%2Fcore%2Fsemaphore.cpp;h=0000000000000000000000000000000000000000;hp=4bfd3036e3ae0d127a195e3f014b367fb21fd208;hb=609c9a508cfdc7b42c46c4f21d17639204165a00;hpb=b4806214e905752617691f851717033fd3f266c2 diff --git a/source/core/semaphore.cpp b/source/core/semaphore.cpp deleted file mode 100644 index 4bfd303..0000000 --- a/source/core/semaphore.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#ifdef WIN32 -#include -#else -#include -#include -#endif -#include -#include -#include -#include "mutex_private.h" -#include "semaphore.h" -#include "systemerror.h" - -namespace Msp { - -struct Semaphore::Private -{ -#ifdef WIN32 - HANDLE handle; -#else - Mutex mutex; - pthread_cond_t cond; - unsigned limit; - unsigned count; -#endif -}; - - -Semaphore::Semaphore(unsigned limit): - priv(new Private) -{ -#ifdef WIN32 - priv->handle = CreateSemaphore(0, 0, limit, 0); -#else - pthread_cond_init(&priv->cond, 0); - priv->limit = limit; - priv->count = 0; -#endif -} - -Semaphore::~Semaphore() -{ -#ifdef WIN32 - CloseHandle(priv->handle); -#else - pthread_cond_destroy(&priv->cond); -#endif - delete priv; -} - -void Semaphore::signal() -{ -#ifdef WIN32 - if(!ReleaseSemaphore(priv->handle, 1, 0)) - throw system_error("ReleaseSemaphore"); -#else - MutexLock mlock(priv->mutex); - if(priv->countlimit) - ++priv->count; - if(int err = pthread_cond_signal(&priv->cond)) - throw system_error("pthread_cond_signal", err); -#endif -} - -void Semaphore::wait() -{ -#ifdef WIN32 - DWORD ret = WaitForSingleObject(priv->handle, INFINITE); - if(ret==WAIT_FAILED) - throw system_error("WaitForSingleObject"); -#else - MutexLock mlock(priv->mutex); - while(!priv->count) - if(int err = pthread_cond_wait(&priv->cond, &priv->mutex.priv->mutex)) - throw system_error("pthread_cond_wait", err); - --priv->count; -#endif -} - -bool Semaphore::wait(const Time::TimeDelta &d) -{ -#ifdef WIN32 - DWORD ret = WaitForSingleObject(priv->handle, (DWORD)(d/Time::usec)); - if(ret==WAIT_FAILED) - throw system_error("WaitForSingleObject"); - return ret==WAIT_OBJECT_0; -#else - timespec timeout = Time::rawtime_to_timespec((Time::now()+d).raw()); - - int err = pthread_cond_timedwait(&priv->cond, &priv->mutex.priv->mutex, &timeout); - if(err && err!=ETIMEDOUT) - throw system_error("pthread_cond_timedwait", err); - return err==0; -#endif -} - -} // namespace Msp