1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef V8_PLATFORM_SEMAPHORE_H_
29 #define V8_PLATFORM_SEMAPHORE_H_
31 #include "../lazy-instance.h"
33 #include "../win32-headers.h"
37 #include <mach/semaphore.h> // NOLINT
39 #include <semaphore.h> // NOLINT
45 // Forward declarations.
48 // ----------------------------------------------------------------------------
51 // A semaphore object is a synchronization object that maintains a count. The
52 // count is decremented each time a thread completes a wait for the semaphore
53 // object and incremented each time a thread signals the semaphore. When the
54 // count reaches zero, threads waiting for the semaphore blocks until the
55 // count becomes non-zero.
57 class Semaphore V8_FINAL {
59 explicit Semaphore(int count);
62 // Increments the semaphore counter.
65 // Suspends the calling thread until the semaphore counter is non zero
66 // and then decrements the semaphore counter.
69 // Suspends the calling thread until the counter is non zero or the timeout
70 // time has passed. If timeout happens the return value is false and the
71 // counter is unchanged. Otherwise the semaphore counter is decremented and
73 bool WaitFor(const TimeDelta& rel_time) V8_WARN_UNUSED_RESULT;
76 typedef semaphore_t NativeHandle;
78 typedef sem_t NativeHandle;
80 typedef HANDLE NativeHandle;
83 NativeHandle& native_handle() {
84 return native_handle_;
86 const NativeHandle& native_handle() const {
87 return native_handle_;
91 NativeHandle native_handle_;
93 DISALLOW_COPY_AND_ASSIGN(Semaphore);
97 // POD Semaphore initialized lazily (i.e. the first time Pointer() is called).
99 // // The following semaphore starts at 0.
100 // static LazySemaphore<0>::type my_semaphore = LAZY_SEMAPHORE_INITIALIZER;
102 // void my_function() {
103 // // Do something with my_semaphore.Pointer().
108 struct CreateSemaphoreTrait {
109 static Semaphore* Create() {
110 return new Semaphore(N);
115 struct LazySemaphore {
116 typedef typename LazyDynamicInstance<
118 CreateSemaphoreTrait<N>,
119 ThreadSafeInitOnceTrait>::type type;
122 #define LAZY_SEMAPHORE_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER
124 } } // namespace v8::internal
126 #endif // V8_PLATFORM_SEMAPHORE_H_