Scott Graham 2014-12-18 09:08:57 -08:00
parent ec38bf152d
commit d1fdcf99e0
2 changed files with 28 additions and 0 deletions

View File

@ -14,6 +14,8 @@
#include "util/synchronization/semaphore.h"
#include <limits>
#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<LONG>::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) {

View File

@ -19,6 +19,8 @@
#if defined(OS_MACOSX)
#include <dispatch/dispatch.h>
#elif defined(OS_WIN)
#include <windows.h>
#else
#include <semaphore.h>
#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