Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
semaphore.h
Go to the documentation of this file.
1 
18 #ifndef ION_PORT_SEMAPHORE_H_
19 #define ION_PORT_SEMAPHORE_H_
20 
21 #if defined(ION_PLATFORM_IOS) || defined(ION_PLATFORM_MAC)
22 #include <dispatch/dispatch.h>
23 #elif defined(ION_PLATFORM_WINDOWS)
24 #include <windows.h>
25 #include <atomic>
26 #else
27 #include <semaphore.h>
28 #endif
29 
30 #include "base/integral_types.h"
31 #include "base/macros.h"
32 
33 namespace ion {
34 namespace port {
35 
40 class ION_API Semaphore {
41  public:
43  Semaphore();
45  explicit Semaphore(uint32 initial_value);
46  ~Semaphore();
47 
50  bool Post();
53  bool Wait();
57  bool TryWait();
61  bool TimedWaitMs(int64 timeout_in_ms);
62 
63  private:
65 #if defined(ION_PLATFORM_IOS) || defined(ION_PLATFORM_MAC)
66  dispatch_semaphore_t semaphore_;
67 #elif defined(ION_PLATFORM_WINDOWS)
68  HANDLE semaphore_;
69  std::atomic<int> value_;
70 #elif defined(ION_PLATFORM_ASMJS)
71  int value_;
72 #else
73  sem_t semaphore_;
74 #endif
75 
76  DISALLOW_COPY_AND_ASSIGN(Semaphore);
77 };
78 
79 } // namespace port
80 } // namespace ion
81 
82 #endif // ION_PORT_SEMAPHORE_H_
A Semaphore enables threads and process synchronization.
Definition: semaphore.h:40