diff options
author | sfan5 <sfan5@live.de> | 2019-11-14 17:58:07 +0100 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2019-11-14 20:20:14 +0100 |
commit | 49365b25d9b2857e3f828032ac4d7daa049b4907 (patch) | |
tree | d2ccd8e25f52592180d474b34168e10af01b6917 /src | |
parent | b5f5e00b29beeda591f74540f74ab7b5dc41a4bb (diff) | |
download | minetest-49365b25d9b2857e3f828032ac4d7daa049b4907.tar.gz minetest-49365b25d9b2857e3f828032ac4d7daa049b4907.tar.bz2 minetest-49365b25d9b2857e3f828032ac4d7daa049b4907.zip |
Optimize semaphore wait with zero timeout on POSIX
Diffstat (limited to 'src')
-rw-r--r-- | src/threading/semaphore.cpp | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/src/threading/semaphore.cpp b/src/threading/semaphore.cpp index 77ceff509..ce22dcd05 100644 --- a/src/threading/semaphore.cpp +++ b/src/threading/semaphore.cpp @@ -140,22 +140,27 @@ bool Semaphore::wait(unsigned int time_ms) errno = EINVAL; } # else - struct timespec wait_time; - struct timeval now; + int ret; + if (time_ms > 0) { + struct timespec wait_time; + struct timeval now; - if (gettimeofday(&now, NULL) == -1) { - std::cerr << "Semaphore::wait(ms): Unable to get time with gettimeofday!" << std::endl; - abort(); - } + if (gettimeofday(&now, NULL) == -1) { + std::cerr << "Semaphore::wait(ms): Unable to get time with gettimeofday!" << std::endl; + abort(); + } - wait_time.tv_nsec = ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000); - wait_time.tv_sec = (time_ms / 1000) + (wait_time.tv_nsec / (1000 * 1000 * 1000)) + now.tv_sec; - wait_time.tv_nsec %= 1000 * 1000 * 1000; + wait_time.tv_nsec = ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000); + wait_time.tv_sec = (time_ms / 1000) + (wait_time.tv_nsec / (1000 * 1000 * 1000)) + now.tv_sec; + wait_time.tv_nsec %= 1000 * 1000 * 1000; - int ret = sem_timedwait(&semaphore, &wait_time); + ret = sem_timedwait(&semaphore, &wait_time); + } else { + ret = sem_trywait(&semaphore); + } # endif - assert(!ret || (errno == ETIMEDOUT || errno == EINTR)); + assert(!ret || (errno == ETIMEDOUT || errno == EINTR || errno == EAGAIN)); return !ret; #endif } |