aboutsummaryrefslogtreecommitdiff
path: root/src/jthread/pthread/jsemaphore.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/jthread/pthread/jsemaphore.cpp')
-rw-r--r--src/jthread/pthread/jsemaphore.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/jthread/pthread/jsemaphore.cpp b/src/jthread/pthread/jsemaphore.cpp
index 962b582f1..ee1431065 100644
--- a/src/jthread/pthread/jsemaphore.cpp
+++ b/src/jthread/pthread/jsemaphore.cpp
@@ -17,8 +17,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <assert.h>
+#include <errno.h>
+#include <sys/time.h>
#include "jthread/jsemaphore.h"
+
#define UNUSED(expr) do { (void)(expr); } while (0)
+
JSemaphore::JSemaphore() {
int sem_init_retval = sem_init(&m_semaphore,0,0);
assert(sem_init_retval == 0);
@@ -49,6 +53,33 @@ void JSemaphore::Wait() {
UNUSED(sem_wait_retval);
}
+bool JSemaphore::Wait(unsigned int time_ms) {
+ struct timespec waittime;
+ struct timeval now;
+
+ if (gettimeofday(&now, NULL) == -1) {
+ assert("Unable to get time by clock_gettime!" == 0);
+ return false;
+ }
+
+ waittime.tv_nsec = ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000);
+ waittime.tv_sec = (time_ms / 1000) + (waittime.tv_nsec / (1000*1000*1000)) + now.tv_sec;
+ waittime.tv_nsec %= 1000*1000*1000;
+
+ errno = 0;
+ int sem_wait_retval = sem_timedwait(&m_semaphore,&waittime);
+
+ if (sem_wait_retval == 0)
+ {
+ return true;
+ }
+ else {
+ assert((errno == ETIMEDOUT) || (errno == EINTR));
+ return false;
+ }
+ return sem_wait_retval == 0 ? true : false;
+}
+
int JSemaphore::GetValue() {
int retval = 0;