semaphore.h 2.81 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_BASE_PLATFORM_SEMAPHORE_H_
#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 13 14
#endif

#if V8_OS_MACOSX
15
#include <dispatch/dispatch.h>  // NOLINT
16 17 18 19
#elif V8_OS_POSIX
#include <semaphore.h>  // NOLINT
#endif

johnx's avatar
johnx committed
20 21 22 23
#if V8_OS_STARBOARD
#include "starboard/common/semaphore.h"
#endif

24
namespace v8 {
25
namespace base {
26 27 28 29 30 31 32 33 34 35 36 37 38

// Forward declarations.
class TimeDelta;

// ----------------------------------------------------------------------------
// Semaphore
//
// A semaphore object is a synchronization object that maintains a count. The
// count is decremented each time a thread completes a wait for the semaphore
// object and incremented each time a thread signals the semaphore. When the
// count reaches zero,  threads waiting for the semaphore blocks until the
// count becomes non-zero.

39
class V8_BASE_EXPORT Semaphore final {
40 41 42 43 44
 public:
  explicit Semaphore(int count);
  ~Semaphore();

  // Increments the semaphore counter.
45
  void Signal();
46

47 48
  // Decrements the semaphore counter if it is positive, or blocks until it
  // becomes positive and then decrements the counter.
49 50
  void Wait();

51 52 53
  // Like Wait() but returns after rel_time time has passed. If the timeout
  // happens the return value is false and the counter is unchanged. Otherwise
  // the semaphore counter is decremented and true is returned.
54
  bool WaitFor(const TimeDelta& rel_time) V8_WARN_UNUSED_RESULT;
55 56

#if V8_OS_MACOSX
57
  using NativeHandle = dispatch_semaphore_t;
58
#elif V8_OS_POSIX
59
  using NativeHandle = sem_t;
60
#elif V8_OS_WIN
61
  using NativeHandle = HANDLE;
johnx's avatar
johnx committed
62 63
#elif V8_OS_STARBOARD
  using NativeHandle = starboard::Semaphore;
64 65
#endif

66
  NativeHandle& native_handle() {
67 68
    return native_handle_;
  }
69
  const NativeHandle& native_handle() const {
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    return native_handle_;
  }

 private:
  NativeHandle native_handle_;

  DISALLOW_COPY_AND_ASSIGN(Semaphore);
};


// POD Semaphore initialized lazily (i.e. the first time Pointer() is called).
// Usage:
//   // The following semaphore starts at 0.
//   static LazySemaphore<0>::type my_semaphore = LAZY_SEMAPHORE_INITIALIZER;
//
//   void my_function() {
//     // Do something with my_semaphore.Pointer().
//   }
//

template <int N>
struct CreateSemaphoreTrait {
  static Semaphore* Create() {
    return new Semaphore(N);
  }
};

template <int N>
struct LazySemaphore {
99 100
  using typename LazyDynamicInstance<Semaphore, CreateSemaphoreTrait<N>,
                                     ThreadSafeInitOnceTrait>::type;
101 102 103 104
};

#define LAZY_SEMAPHORE_INITIALIZER LAZY_DYNAMIC_INSTANCE_INITIALIZER

105 106
}  // namespace base
}  // namespace v8
107

108
#endif  // V8_BASE_PLATFORM_SEMAPHORE_H_