reMutexSpinlockImpl.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 reMutexSpinlockImpl.h
  10. * @author rdb
  11. * @date 2018-09-03
  12. */
  13. #ifndef REMUTEXSPINLOCKIMPL_H
  14. #define REMUTEXSPINLOCKIMPL_H
  15. #include "dtoolbase.h"
  16. #include "selectThreadImpl.h"
  17. #ifdef MUTEX_SPINLOCK
  18. #include "atomicAdjust.h"
  19. class Thread;
  20. /**
  21. * Uses a simple user-space spinlock to implement a mutex. It is usually not
  22. * a good idea to use this implementation, unless you are building Panda for a
  23. * specific application on a specific SMP machine, and you are confident that
  24. * you have at least as many CPU's as you have threads.
  25. */
  26. class EXPCL_PANDA_PIPELINE ReMutexSpinlockImpl {
  27. public:
  28. constexpr ReMutexSpinlockImpl() noexcept = default;
  29. ReMutexSpinlockImpl(const ReMutexSpinlockImpl &copy) = delete;
  30. ReMutexSpinlockImpl &operator = (const ReMutexSpinlockImpl &copy) = delete;
  31. public:
  32. void lock();
  33. bool try_lock();
  34. INLINE void unlock();
  35. private:
  36. AtomicAdjust::Pointer _locking_thread = nullptr;
  37. unsigned int _counter = 0;
  38. };
  39. #include "reMutexSpinlockImpl.I"
  40. #endif // MUTEX_SPINLOCK
  41. #endif