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 | |
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')
-rw-r--r-- | src/jthread/jsemaphore.h | 1 | ||||
-rw-r--r-- | src/jthread/pthread/jsemaphore.cpp | 31 | ||||
-rwxr-xr-x | src/jthread/win32/jsemaphore.cpp | 15 |
3 files changed, 47 insertions, 0 deletions
diff --git a/src/jthread/jsemaphore.h b/src/jthread/jsemaphore.h index 70318d5da..b62add253 100644 --- a/src/jthread/jsemaphore.h +++ b/src/jthread/jsemaphore.h @@ -36,6 +36,7 @@ public: void Post(); void Wait(); + bool Wait(unsigned int time_ms); int GetValue(); 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; diff --git a/src/jthread/win32/jsemaphore.cpp b/src/jthread/win32/jsemaphore.cpp index 3a1f2715c..34167f391 100755 --- a/src/jthread/win32/jsemaphore.cpp +++ b/src/jthread/win32/jsemaphore.cpp @@ -51,6 +51,21 @@ void JSemaphore::Wait() { INFINITE); } +bool JSemaphore::Wait(unsigned int time_ms) { + unsigned int retval = WaitForSingleObject( + m_hSemaphore, + time_ms); + + if (retval == WAIT_OBJECT_0) + { + return true; + } + else { + assert(retval == WAIT_TIMEOUT); + return false; + } +} + int JSemaphore::GetValue() { long int retval = 0; |