From 76cf72a8c3420c29c128cbb03d14cac61a0569d6 Mon Sep 17 00:00:00 2001 From: Ben Konyi Date: Wed, 29 Nov 2017 13:41:49 -0500 Subject: [PATCH] Added Semaphore implementation for Windows. (flutter/engine#4396) --- .../src/flutter/synchronization/semaphore.cc | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/synchronization/semaphore.cc b/engine/src/flutter/synchronization/semaphore.cc index cdb4d9fb59..c6e0bdf9e2 100644 --- a/engine/src/flutter/synchronization/semaphore.cc +++ b/engine/src/flutter/synchronization/semaphore.cc @@ -3,11 +3,11 @@ // found in the LICENSE file. #include "flutter/synchronization/semaphore.h" + #include "lib/fxl/build_config.h" #include "lib/fxl/logging.h" #if OS_MACOSX - #include namespace flutter { @@ -48,8 +48,48 @@ class PlatformSemaphore { } // namespace flutter -#else // OS_MACOSX +#elif OS_WIN +#include +namespace flutter { + +class PlatformSemaphore { + public: + explicit PlatformSemaphore(uint32_t count) + : _sem(CreateSemaphore(NULL, count, LONG_MAX, NULL)) {} + + ~PlatformSemaphore() { + if (_sem != nullptr) { + CloseHandle(_sem); + _sem = nullptr; + } + } + + bool IsValid() const { return _sem != nullptr; } + + bool TryWait() { + if (_sem == nullptr) { + return false; + } + + return WaitForSingleObject(_sem, 0) == WAIT_OBJECT_0; + } + + void Signal() { + if (_sem != nullptr) { + ReleaseSemaphore(_sem, 1, NULL); + } + } + + private: + HANDLE _sem; + + FXL_DISALLOW_COPY_AND_ASSIGN(PlatformSemaphore); +}; + +} // namespace flutter + +#else #include #include "lib/fxl/files/eintr_wrapper.h"