aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/mutex.h
blob: 4d11fc9c7a19e6c8210eaf7ee9f3fe281d25e66e (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
 
#include "guard.h"
#include "defaults.h"

#include <util/generic/ptr.h>
#include <util/generic/noncopyable.h>
 
class TFakeMutex: public TNonCopyable {
public:
    inline void Acquire() noexcept {
    }
 
    inline bool TryAcquire() noexcept {
        return true;
    }

    inline void Release() noexcept {
    }

    inline void lock() noexcept {
        Acquire();
    }

    inline bool try_lock() noexcept {
        return TryAcquire();
    }

    inline void unlock() noexcept {
        Release();
    }

    ~TFakeMutex() = default;
};
 
class TMutex {
public:
    TMutex();
    TMutex(TMutex&&);
    ~TMutex();

    void Acquire() noexcept;
    bool TryAcquire() noexcept;
    void Release() noexcept;

    inline void lock() noexcept {
        Acquire();
    }

    inline bool try_lock() noexcept {
        return TryAcquire();
    }

    inline void unlock() noexcept {
        Release();
    }

    //return opaque pointer to real handler
    void* Handle() const noexcept;

private:
    class TImpl;
    THolder<TImpl> Impl_;
};