aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/actor/what_thread_does_guard.h
blob: f104e9e1731805a6fba6f539a72edbee746c6891 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#pragma once

#include "what_thread_does.h"

template <class T>
class TWhatThreadDoesAcquireGuard: public TNonCopyable {
public:
    inline TWhatThreadDoesAcquireGuard(const T& t, const char* acquire) noexcept {
        Init(&t, acquire);
    }

    inline TWhatThreadDoesAcquireGuard(const T* t, const char* acquire) noexcept {
        Init(t, acquire);
    }

    inline ~TWhatThreadDoesAcquireGuard() {
        Release();
    }

    inline void Release() noexcept {
        if (WasAcquired()) {
            const_cast<T*>(T_)->Release();
            T_ = nullptr;
        }
    }

    inline bool WasAcquired() const noexcept {
        return T_ != nullptr;
    }

private:
    inline void Init(const T* t, const char* acquire) noexcept {
        T_ = const_cast<T*>(t);
        TWhatThreadDoesPushPop pp(acquire);
        T_->Acquire();
    }

private:
    T* T_;
};