diff --git a/util/synchronization/semaphore.cc b/util/synchronization/semaphore.cc index d513fa0d..5f0a206a 100644 --- a/util/synchronization/semaphore.cc +++ b/util/synchronization/semaphore.cc @@ -14,6 +14,8 @@ #include "util/synchronization/semaphore.h" +#include + #include "base/logging.h" #include "base/posix/eintr_wrapper.h" @@ -38,6 +40,28 @@ void Semaphore::Signal() { dispatch_semaphore_signal(semaphore_); } +#elif defined(OS_WIN) + +Semaphore::Semaphore(int value) + : semaphore_(CreateSemaphore(nullptr, + value, + std::numeric_limits::max(), + nullptr)) { + PCHECK(semaphore_) << "CreateSemaphore"; +} + +Semaphore::~Semaphore() { + PCHECK(CloseHandle(semaphore_)); +} + +void Semaphore::Wait() { + PCHECK(WaitForSingleObject(semaphore_, INFINITE) == WAIT_OBJECT_0); +} + +void Semaphore::Signal() { + PCHECK(ReleaseSemaphore(semaphore_, 1, nullptr)); +} + #else Semaphore::Semaphore(int value) { diff --git a/util/synchronization/semaphore.h b/util/synchronization/semaphore.h index 4e36f8cc..8cff205d 100644 --- a/util/synchronization/semaphore.h +++ b/util/synchronization/semaphore.h @@ -19,6 +19,8 @@ #if defined(OS_MACOSX) #include +#elif defined(OS_WIN) +#include #else #include #endif @@ -53,6 +55,8 @@ class Semaphore { private: #if defined(OS_MACOSX) dispatch_semaphore_t semaphore_; +#elif defined(OS_WIN) + HANDLE semaphore_; #else sem_t semaphore_; #endif