Mutex.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #ifdef URHO3D_IS_BUILDING
  5. #include "Urho3D.h"
  6. #else
  7. #include <Urho3D/Urho3D.h>
  8. #endif
  9. namespace Urho3D
  10. {
  11. /// Operating system mutual exclusion primitive.
  12. class URHO3D_API Mutex
  13. {
  14. public:
  15. /// Construct.
  16. Mutex();
  17. /// Destruct.
  18. ~Mutex();
  19. /// Acquire the mutex. Block if already acquired.
  20. void Acquire();
  21. /// Try to acquire the mutex without locking. Return true if successful.
  22. bool TryAcquire();
  23. /// Release the mutex.
  24. void Release();
  25. private:
  26. /// Mutex handle.
  27. void* handle_;
  28. };
  29. /// Lock that automatically acquires and releases a mutex.
  30. class URHO3D_API MutexLock
  31. {
  32. public:
  33. /// Construct and acquire the mutex.
  34. explicit MutexLock(Mutex& mutex);
  35. /// Destruct. Release the mutex.
  36. ~MutexLock();
  37. /// Prevent copy construction.
  38. MutexLock(const MutexLock& rhs) = delete;
  39. /// Prevent assignment.
  40. MutexLock& operator =(const MutexLock& rhs) = delete;
  41. private:
  42. /// Mutex reference.
  43. Mutex& mutex_;
  44. };
  45. }