mutexSpinlockImpl.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #ifndef MUTEXSPINLOCKIMPL_H
  14. #define MUTEXSPINLOCKIMPL_H
  15. #include "dtoolbase.h"
  16. #include "selectThreadImpl.h"
  17. #ifdef MUTEX_SPINLOCK
  18. #include <atomic>
  19. /**
  20. * Uses a simple user-space spinlock to implement a mutex. It is usually not
  21. * a good idea to use this implementation, unless you are building Panda for a
  22. * specific application on a specific SMP machine, and you are confident that
  23. * you have at least as many CPU's as you have threads.
  24. */
  25. class EXPCL_DTOOL_DTOOLBASE MutexSpinlockImpl {
  26. public:
  27. constexpr MutexSpinlockImpl() noexcept = default;
  28. MutexSpinlockImpl(const MutexSpinlockImpl &copy) = delete;
  29. MutexSpinlockImpl &operator = (const MutexSpinlockImpl &copy) = delete;
  30. public:
  31. INLINE void lock();
  32. INLINE bool try_lock();
  33. INLINE void unlock();
  34. private:
  35. void do_lock();
  36. std::atomic_flag _flag = ATOMIC_FLAG_INIT;
  37. };
  38. #include "mutexSpinlockImpl.I"
  39. #endif // MUTEX_SPINLOCK
  40. #endif