lock.hpp 841 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. //shared functionality used for pObject on all platforms
  3. struct Lock {
  4. struct Handle {
  5. Handle(const Lock* self) : self(self) {
  6. if(self) ++self->locks;
  7. }
  8. ~Handle() {
  9. release();
  10. }
  11. auto release() -> bool {
  12. if(self) {
  13. --self->locks;
  14. self = nullptr;
  15. return true;
  16. }
  17. return false;
  18. }
  19. const Lock* self = nullptr;
  20. };
  21. auto acquired() const -> bool {
  22. return locks || Application::state().quit;
  23. }
  24. auto acquire() const -> Handle {
  25. return {this};
  26. }
  27. //deprecated C-style manual functions
  28. //prefer RAII acquire() functionality instead in newly written code
  29. auto locked() const -> bool {
  30. return acquired();
  31. }
  32. auto lock() -> void {
  33. ++locks;
  34. }
  35. auto unlock() -> void {
  36. --locks;
  37. }
  38. mutable int locks = 0;
  39. };