diff options
author | sapier <Sapier at GMX dot net> | 2014-01-06 12:45:42 +0100 |
---|---|---|
committer | sapier <Sapier at GMX dot net> | 2014-01-10 10:10:45 +0100 |
commit | 8b0b857eaaa50c6ec217a46c0577395c78ec04c7 (patch) | |
tree | b9fd5e1edb099aee1498b29a43702568a283aa34 /src/jthread/pthread | |
parent | 10fdbf737513e48bfcfb9e90fe243dcd989c0476 (diff) | |
download | minetest-8b0b857eaaa50c6ec217a46c0577395c78ec04c7.tar.gz minetest-8b0b857eaaa50c6ec217a46c0577395c78ec04c7.tar.bz2 minetest-8b0b857eaaa50c6ec217a46c0577395c78ec04c7.zip |
Make MutexQueue use jsemaphore for signaling
Diffstat (limited to 'src/jthread/pthread')
-rw-r--r-- | src/jthread/pthread/jsemaphore.cpp | 31 |
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; |