• 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 #ifndef V8_BASE_PLATFORM_SEMAPHORE_H_
6 #define V8_BASE_PLATFORM_SEMAPHORE_H_
7 
8 #include "src/base/base-export.h"
9 #include "src/base/lazy-instance.h"
10 #if V8_OS_WIN
11 #include "src/base/win32-headers.h"
12 #endif
13 
14 #if V8_OS_MACOSX
15 #include <dispatch/dispatch.h>  // NOLINT
16 #elif V8_OS_POSIX
17 #include <semaphore.h>  // NOLINT
18 #endif
19 
20 #if V8_OS_STARBOARD
21 #include "starboard/common/semaphore.h"
22 #endif
23 
24 namespace v8 {
25 namespace base {
26 
27 // Forward declarations.
28 class TimeDelta;
29 
30 // ----------------------------------------------------------------------------
31 // Semaphore
32 //
33 // A semaphore object is a synchronization object that maintains a count. The
34 // count is decremented each time a thread completes a wait for the semaphore
35 // object and incremented each time a thread signals the semaphore. When the
36 // count reaches zero,  threads waiting for the semaphore blocks until the
37 // count becomes non-zero.
38 
39 class V8_BASE_EXPORT Semaphore final {
40  public:
41   explicit Semaphore(int count);
42   Semaphore(const Semaphore&) = delete;
43   Semaphore& operator=(const Semaphore&) = delete;
44   ~Semaphore();
45 
46   // Increments the semaphore counter.
47   void Signal();
48 
49   // Decrements the semaphore counter if it is positive, or blocks until it
50   // becomes positive and then decrements the counter.
51   void Wait();
52 
53   // Like Wait() but returns after rel_time time has passed. If the timeout
54   // happens the return value is false and the counter is unchanged. Otherwise
55   // the semaphore counter is decremented and true is returned.
56   bool WaitFor(const TimeDelta& rel_time) V8_WARN_UNUSED_RESULT;
57 
58 #if V8_OS_MACOSX
59   using NativeHandle = dispatch_semaphore_t;
60 #elif V8_OS_POSIX
61   using NativeHandle = sem_t;
62 #elif V8_OS_WIN
63   using NativeHandle = HANDLE;
64 #elif V8_OS_STARBOARD
65   using NativeHandle = starboard::Semaphore;
66 #endif
67 
native_handle()68   NativeHandle& native_handle() {
69     return native_handle_;
70   }
native_handle()71   const NativeHandle& native_handle() const {
72     return native_handle_;
73   }
74 
75  private:
76   NativeHandle native_handle_;
77 };
78 
79 
80 // POD Semaphore initialized lazily (i.e. the first time Pointer() is called).
81 // Usage:
82 //   // The following semaphore starts at 0.
83 //   static LazySemaphore<0>::type my_semaphore = LAZY_SEMAPHORE_INITIALIZER;
84 //
85 //   void my_function() {
86 //     // Do something with my_semaphore.Pointer().
87 //   }
88 //
89 
90 template <int N>
91 struct CreateSemaphoreTrait {
CreateCreateSemaphoreTrait92   static Semaphore* Create() {
93     return new Semaphore(N);
94   }
95 };
96 
97 template <int N>
98 struct LazySemaphore {
99   using typename LazyDynamicInstance<Semaphore, CreateSemaphoreTrait<N>,
100                                      ThreadSafeInitOnceTrait>::type;
101 };
102 
103 #define LAZY_SEMAPHORE_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER
104 
105 }  // namespace base
106 }  // namespace v8
107 
108 #endif  // V8_BASE_PLATFORM_SEMAPHORE_H_
109