Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mutex.cc
Go to the documentation of this file.
1 
18 #include <assert.h>
19 
20 #include "ion/port/mutex.h"
21 
22 namespace ion {
23 namespace port {
24 
52 
54 #if defined(ION_PLATFORM_WINDOWS)
55 # if defined(ION_MUTEX_USE_CRITICAL_SECTION)
56  InitializeCriticalSection(&critical_section_);
57 # else
58  blockers_ = 0;
59  thread_id_ = -1;
60  event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
61 # endif
62 #elif defined(ION_PLATFORM_ASMJS)
63  locked_ = false;
64 #else
65  pthread_mutex_init(&pthread_mutex_, NULL);
66 #endif
67 }
68 
70 #if defined(ION_PLATFORM_WINDOWS)
71 # if defined(ION_MUTEX_USE_CRITICAL_SECTION)
72  DeleteCriticalSection(&critical_section_);
73 # else
74  CloseHandle(event_);
75 # endif
76 #elif defined(ION_PLATFORM_ASMJS)
77  locked_ = false;
78 #else
79  pthread_mutex_destroy(&pthread_mutex_);
80 #endif
81 }
82 
84  if (TryLock()) {
85  Unlock();
86  return false;
87  } else {
88  return true;
89  }
90 }
91 
92 void Mutex::Lock() {
93 #if defined(ION_PLATFORM_WINDOWS)
94 # if defined(ION_MUTEX_USE_CRITICAL_SECTION)
95  EnterCriticalSection(&critical_section_);
96 # else
97  if (InterlockedIncrement(&blockers_) > 1)
103  WaitForSingleObject(event_, INFINITE);
106  thread_id_ = GetCurrentThreadId();
107 # endif
108 #elif defined(ION_PLATFORM_ASMJS)
109  assert(!locked_);
110  locked_ = true;
111 #else
112  pthread_mutex_lock(&pthread_mutex_);
113 #endif
114 }
115 
117 #if defined(ION_PLATFORM_WINDOWS)
118 # if defined(ION_MUTEX_USE_CRITICAL_SECTION)
119  return TryEnterCriticalSection(&critical_section_);
120 # else
121  if (InterlockedIncrement(&blockers_) > 1) {
126  InterlockedDecrement(&blockers_);
127  return false;
128  } else {
129  thread_id_ = GetCurrentThreadId();
130  return true;
131  }
132 # endif
133 #elif defined(ION_PLATFORM_ASMJS)
134  if (!locked_) {
135  locked_ = true;
136  return true;
137  } else {
138  return false;
139  }
140 #else
141  return pthread_mutex_trylock(&pthread_mutex_) == 0;
142 #endif
143 }
144 
146 #if defined(ION_PLATFORM_WINDOWS)
147 # if defined(ION_MUTEX_USE_CRITICAL_SECTION)
148  LeaveCriticalSection(&critical_section_);
149 # else
150  if (thread_id_ == GetCurrentThreadId()) {
153  thread_id_ = -1;
157  if (InterlockedDecrement(&blockers_) > 0)
158  SetEvent(event_);
159  }
160 # endif
161 #elif defined(ION_PLATFORM_ASMJS)
162  assert(locked_);
163  locked_ = false;
164 #else
165  pthread_mutex_unlock(&pthread_mutex_);
166 #endif
167 }
168 
169 #undef ION_MUTEX_USE_CRITICAL_SECTION
170 
171 } // namespace port
172 } // namespace ion
bool TryLock()
Returns true if the mutex was successfully locked, and false otherwise.
Definition: mutex.cc:116
bool IsLocked()
Returns whether the Mutex is currently locked. Does not block.
Definition: mutex.cc:83
Mutex()
POSIX mutexes are non-recursive, meaning that if the same thread tries to Lock() the mutex...
Definition: mutex.cc:53
void Unlock()
Unlocks the Mutex.
Definition: mutex.cc:145
void Lock()
Locks the Mutex.
Definition: mutex.cc:92
ThreadId GetCurrentThreadId()
Thread ID functions.
Definition: threadutils.cc:363