diff -r d3f9d4839e51 dcpp/Semaphore.h --- a/dcpp/Semaphore.h Wed Oct 07 23:38:15 2015 +0200 +++ b/dcpp/Semaphore.h Fri Oct 09 01:02:07 2015 +0200 @@ -21,82 +21,33 @@ #include -#ifdef _WIN32 -#include "w.h" -#else -#include -#include -#include -#endif +#include +#include namespace dcpp { class Semaphore : boost::noncopyable { -#ifdef _WIN32 public: - Semaphore() noexcept { - h = CreateSemaphore(NULL, 0, MAXLONG, NULL); + Semaphore() noexcept : semaphore(0) { } void signal() noexcept { - ReleaseSemaphore(h, 1, NULL); - } - - bool wait() noexcept { return WaitForSingleObject(h, INFINITE) == WAIT_OBJECT_0; } - bool wait(uint32_t millis) noexcept { return WaitForSingleObject(h, millis) == WAIT_OBJECT_0; } - - ~Semaphore() { - CloseHandle(h); - } - -private: - HANDLE h; -#else -public: - Semaphore() noexcept { - sem_init(&semaphore, 0, 0); - } - - ~Semaphore() { - sem_destroy(&semaphore); - } - - void signal() noexcept { - sem_post(&semaphore); + semaphore.post(); } bool wait() noexcept { - int retval = 0; - do { - retval = sem_wait(&semaphore); - } while (retval != 0); - + semaphore.wait(); return true; } bool wait(uint32_t millis) noexcept { - timeval timev; - timespec t; - gettimeofday(&timev, NULL); - millis+=timev.tv_usec/1000; - t.tv_sec = timev.tv_sec + (millis/1000); - t.tv_nsec = (millis%1000)*1000*1000; - int ret; - do { - ret = sem_timedwait(&semaphore, &t); - } while (ret != 0 && errno == EINTR); - - if (ret != 0) { - return false; - } - - return true; + return semaphore.timed_wait( + boost::posix_time::microsec_clock::universal_time() + boost::posix_time::millisec(millis)); } private: - sem_t semaphore; -#endif + boost::interprocess::interprocess_semaphore semaphore; }; } // namespace dcpp