what_thread_does_guard.h 871 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #pragma once
  2. #include "what_thread_does.h"
  3. template <class T>
  4. class TWhatThreadDoesAcquireGuard: public TNonCopyable {
  5. public:
  6. inline TWhatThreadDoesAcquireGuard(const T& t, const char* acquire) noexcept {
  7. Init(&t, acquire);
  8. }
  9. inline TWhatThreadDoesAcquireGuard(const T* t, const char* acquire) noexcept {
  10. Init(t, acquire);
  11. }
  12. inline ~TWhatThreadDoesAcquireGuard() {
  13. Release();
  14. }
  15. inline void Release() noexcept {
  16. if (WasAcquired()) {
  17. const_cast<T*>(T_)->Release();
  18. T_ = nullptr;
  19. }
  20. }
  21. inline bool WasAcquired() const noexcept {
  22. return T_ != nullptr;
  23. }
  24. private:
  25. inline void Init(const T* t, const char* acquire) noexcept {
  26. T_ = const_cast<T*>(t);
  27. TWhatThreadDoesPushPop pp(acquire);
  28. T_->Acquire();
  29. }
  30. private:
  31. T* T_;
  32. };