Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(732)

Side by Side Diff: src/platform/semaphore.h

Issue 23748003: Cleanup Semaphore class. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Build fix for Mac OS X. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/platform-win32.cc ('k') | src/platform/semaphore.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
4 // met:
5 //
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.
15 //
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.
27
28 #ifndef V8_PLATFORM_SEMAPHORE_H_
29 #define V8_PLATFORM_SEMAPHORE_H_
30
31 #include "lazy-instance.h"
32 #if V8_OS_WIN
33 #include "win32-headers.h"
34 #endif
35
36 #if V8_OS_MACOSX
37 #include <mach/semaphore.h> // NOLINT
38 #elif V8_OS_POSIX
39 #include <semaphore.h> // NOLINT
40 #endif
41
42 namespace v8 {
43 namespace internal {
44
45 // Forward declarations.
46 class TimeDelta;
47
48 // ----------------------------------------------------------------------------
49 // Semaphore
50 //
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.
56
57 class Semaphore V8_FINAL {
58 public:
59 explicit Semaphore(int count);
60 ~Semaphore();
61
62 // Increments the semaphore counter.
63 void Signal();
64
65 // Suspends the calling thread until the semaphore counter is non zero
66 // and then decrements the semaphore counter.
67 void Wait();
68
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
72 // true is returned.
73 bool WaitFor(const TimeDelta& rel_time) V8_WARN_UNUSED_RESULT;
74
75 #if V8_OS_MACOSX
76 typedef semaphore_t NativeHandle;
77 #elif V8_OS_POSIX
78 typedef sem_t NativeHandle;
79 #elif V8_OS_WIN
80 typedef HANDLE NativeHandle;
81 #endif
82
83 NativeHandle& native_handle() V8_WARN_UNUSED_RESULT {
84 return native_handle_;
85 }
86 const NativeHandle& native_handle() const V8_WARN_UNUSED_RESULT {
87 return native_handle_;
88 }
89
90 private:
91 NativeHandle native_handle_;
92
93 DISALLOW_COPY_AND_ASSIGN(Semaphore);
94 };
95
96
97 // POD Semaphore initialized lazily (i.e. the first time Pointer() is called).
98 // Usage:
99 // // The following semaphore starts at 0.
100 // static LazySemaphore<0>::type my_semaphore = LAZY_SEMAPHORE_INITIALIZER;
101 //
102 // void my_function() {
103 // // Do something with my_semaphore.Pointer().
104 // }
105 //
106
107 template <int N>
108 struct CreateSemaphoreTrait {
109 static Semaphore* Create() {
110 return new Semaphore(N);
111 }
112 };
113
114 template <int N>
115 struct LazySemaphore {
116 typedef typename LazyDynamicInstance<
117 Semaphore,
118 CreateSemaphoreTrait<N>,
119 ThreadSafeInitOnceTrait>::type type;
120 };
121
122 #define LAZY_SEMAPHORE_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER
123
124 } } // namespace v8::internal
125
126 #endif // V8_PLATFORM_SEMAPHORE_H_
OLDNEW
« no previous file with comments | « src/platform-win32.cc ('k') | src/platform/semaphore.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698