Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
spinmutex.cc
Go to the documentation of this file.
1 
18 #include "ion/base/spinmutex.h"
19 
20 #include "ion/base/logging.h"
21 #include "ion/port/threadutils.h"
22 
23 namespace ion {
24 namespace base {
25 
27 
29  unsigned int try_count = 0;
30  bool already_locked = false;
31  while (!locked_.compare_exchange_weak(
32  already_locked, true, std::memory_order_acquire)) {
33  already_locked = false;
34  if (++try_count > 1000) {
36  }
37  }
38 }
39 
41  bool already_locked = false;
42  return locked_.compare_exchange_strong(
43  already_locked, true, std::memory_order_acquire);
44 }
45 
47  DCHECK(IsLocked());
48  locked_.store(false, std::memory_order_release);
49 }
50 
51 } // namespace base
52 } // namespace ion
bool TryLock()
Returns true if the mutex was successfully locked, and false otherwise.
Definition: spinmutex.cc:40
#define DCHECK(expr)
Definition: logging.h:331
void YieldThread()
Causes the calling thread to relinquish the CPU if there are other threads waiting to execute...
Definition: threadutils.cc:359
bool IsLocked() const
Returns whether the Mutex is currently locked. Does not block.
Definition: spinmutex.h:42
void Lock()
Locks the Mutex.
Definition: spinmutex.cc:28
Copyright 2016 Google Inc.
void Unlock()
Unlocks the Mutex.
Definition: spinmutex.cc:46