• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "util/build_config.h"
12 
13 #if defined(OS_WIN)
14 #include <windows.h>
15 #elif defined(OS_MACOSX)
16 #include <dispatch/dispatch.h>
17 #elif defined(OS_ZOS)
18 #include "zos-semaphore.h"
19 #elif defined(OS_POSIX)
20 #include <semaphore.h>
21 #else
22 #error Port.
23 #endif
24 
25 class Semaphore {
26  public:
27   explicit Semaphore(int count);
28   ~Semaphore();
29 
30   // Increments the semaphore counter.
31   void Signal();
32 
33   // Decrements the semaphore counter if it is positive, or blocks until it
34   // becomes positive and then decrements the counter.
35   void Wait();
36 
37 #if defined(OS_MACOSX)
38   using NativeHandle = dispatch_semaphore_t;
39 #elif defined(OS_POSIX)
40   using NativeHandle = sem_t;
41 #elif defined(OS_WIN)
42   using NativeHandle = HANDLE;
43 #endif
44 
native_handle()45   NativeHandle& native_handle() { return native_handle_; }
native_handle()46   const NativeHandle& native_handle() const { return native_handle_; }
47 
48  private:
49   NativeHandle native_handle_;
50 
51   Semaphore(const Semaphore&) = delete;
52   Semaphore& operator=(const Semaphore&) = delete;
53 };
54 
55 #endif  // UTIL_SEMAPHORE_H_
56