aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/actor/what_thread_does_guard.h
blob: 4fb0abef6dad50a503db7bb4b5d605b97dd342d5 (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_; 
};