Panda3D
mutexSpinlockImpl.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file mutexSpinlockImpl.h
10  * @author drose
11  * @date 2006-04-11
12  */
13 
14 #ifndef MUTEXSPINLOCKIMPL_H
15 #define MUTEXSPINLOCKIMPL_H
16 
17 #include "dtoolbase.h"
18 #include "selectThreadImpl.h"
19 
20 #ifdef MUTEX_SPINLOCK
21 
22 #ifdef PHAVE_ATOMIC
23 #include <atomic>
24 #endif
25 
26 /**
27  * Uses a simple user-space spinlock to implement a mutex. It is usually not
28  * a good idea to use this implementation, unless you are building Panda for a
29  * specific application on a specific SMP machine, and you are confident that
30  * you have at least as many CPU's as you have threads.
31  */
32 class EXPCL_DTOOL_DTOOLBASE MutexSpinlockImpl {
33 public:
34  constexpr MutexSpinlockImpl() noexcept = default;
35  MutexSpinlockImpl(const MutexSpinlockImpl &copy) = delete;
36 
37  MutexSpinlockImpl &operator = (const MutexSpinlockImpl &copy) = delete;
38 
39 public:
40  INLINE void lock();
41  INLINE bool try_lock();
42  INLINE void unlock();
43 
44 private:
45  void do_lock();
46 
47  std::atomic_flag _flag = ATOMIC_FLAG_INIT;
48 };
49 
50 #include "mutexSpinlockImpl.I"
51 
52 #endif // MUTEX_SPINLOCK
53 
54 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.