1 // Copyright 2013 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 // 5 // Based on 6 // https://cs.chromium.org/chromium/src/v8/src/base/platform/semaphore.h 7 8 #ifndef UTIL_SEMAPHORE_H_ 9 #define UTIL_SEMAPHORE_H_ 10 11 #include "base/macros.h" 12 #include "util/build_config.h" 13 14 #if defined(OS_WIN) 15 #include "base/win/windows_types.h" 16 #elif defined(OS_MACOSX) 17 #include <dispatch/dispatch.h> 18 #elif defined(OS_POSIX) 19 #include <semaphore.h> 20 #else 21 #error Port. 22 #endif 23 24 class Semaphore { 25 public: 26 explicit Semaphore(int count); 27 ~Semaphore(); 28 29 // Increments the semaphore counter. 30 void Signal(); 31 32 // Decrements the semaphore counter if it is positive, or blocks until it 33 // becomes positive and then decrements the counter. 34 void Wait(); 35 36 #if defined(OS_MACOSX) 37 using NativeHandle = dispatch_semaphore_t; 38 #elif defined(OS_POSIX) 39 using NativeHandle = sem_t; 40 #elif defined(OS_WIN) 41 using NativeHandle = HANDLE; 42 #endif 43 native_handle()44 NativeHandle& native_handle() { return native_handle_; } native_handle()45 const NativeHandle& native_handle() const { return native_handle_; } 46 47 private: 48 NativeHandle native_handle_; 49 50 DISALLOW_COPY_AND_ASSIGN(Semaphore); 51 }; 52 53 #endif // UTIL_SEMAPHORE_H_ 54