diff options
author | Viktor Mladenovski <viktor.mladenovski@stericsson.com> | 2011-05-20 14:10:59 +0200 |
---|---|---|
committer | Viktor Mladenovski <viktor.mladenovski@stericsson.com> | 2011-05-20 14:10:59 +0200 |
commit | f7e95217e936da34d7124aaced590692eb515923 (patch) | |
tree | 2d9ef4750a67d9d1dd3fe9180a4fef2929fad54b /source/api_wrappers/linux/CSemaphore.cpp |
Initial contribution of loader_communication
ST-Ericsson ID: 326913
ST-Ericsson FOSS-OUT ID: STETL-FOSS-OUT-10204
Change-Id: I171cfc2ee458a8a0a91a1916137d131f0f7ecee5
Diffstat (limited to 'source/api_wrappers/linux/CSemaphore.cpp')
-rw-r--r-- | source/api_wrappers/linux/CSemaphore.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/source/api_wrappers/linux/CSemaphore.cpp b/source/api_wrappers/linux/CSemaphore.cpp new file mode 100644 index 0000000..0246091 --- /dev/null +++ b/source/api_wrappers/linux/CSemaphore.cpp @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (C) ST-Ericsson SA 2011 + * License terms: 3-clause BSD license + ******************************************************************************/ + +#include <errno.h> +#include <semaphore.h> +#include <sys/time.h> +#include "Types.h" +#include "CSemaphore.h" +#include "OS.h" + +CSemaphore::CSemaphore(unsigned int initial_count) +{ + sem_init(&m_semaphore, 0, initial_count); +} + +CSemaphore::~CSemaphore() +{ + sem_destroy(&m_semaphore); +} + +bool CSemaphore::Release(unsigned int count) +{ + if (!count) { + return false; + } + + for (unsigned int i = 0; i < count; ++i) { + if (sem_post(&m_semaphore)) { + return false; + } + } + + return true; +} + +DWORD CSemaphore::Wait(DWORD timeout) +{ + if (INFINITE == timeout) { + sem_wait(&m_semaphore); + } else { + timespec absoulute_time = OS::GetAbsoluteTime(timeout); + int ret; + + /* coverity[returned_null] */ + while (-1 == (ret = sem_timedwait(&m_semaphore, &absoulute_time)) && errno == EINTR); + + if (0 != ret) { + return WAIT_TIMEOUT; + } + } + + return WAIT_OBJECT_0; +} |