aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/mutex.cpp
blob: 2850c6347b73df2c41c8329d00b3674c2cfea718 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "mutex.h"

#include <util/generic/yexception.h>
#include <errno.h>

#if defined(_win_)
    #include "winint.h"
#else
    #include <pthread.h>
#endif

class TMutex::TImpl {
public:
    inline TImpl() {
#if defined(_win_)
        InitializeCriticalSection(&Obj);
#else
        struct T {
            pthread_mutexattr_t Attr;

            inline T() {
                int result;

                memset(&Attr, 0, sizeof(Attr));
                result = pthread_mutexattr_init(&Attr);
                if (result != 0) {
                    ythrow yexception() << "mutexattr init failed(" << LastSystemErrorText(result) << ")";
                }

                result = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
                if (result != 0) {
                    ythrow yexception() << "mutexattr set type failed(" << LastSystemErrorText(result) << ")";
                }
            }

            inline ~T() {
                int result = pthread_mutexattr_destroy(&Attr);
                Y_ABORT_UNLESS(result == 0, "mutexattr destroy(%s)", LastSystemErrorText(result));
            }
        } pma;

        int result = pthread_mutex_init(&Obj, &pma.Attr);
        if (result != 0) {
            ythrow yexception() << "mutex init failed(" << LastSystemErrorText(result) << ")";
        }
#endif
    }

    inline ~TImpl() {
#if defined(_win_)
        DeleteCriticalSection(&Obj);
#else
        int result = pthread_mutex_destroy(&Obj);
        Y_ABORT_UNLESS(result == 0, "mutex destroy failure (%s)", LastSystemErrorText(result));
#endif
    }

    inline void Acquire() noexcept {
#if defined(_win_)
        EnterCriticalSection(&Obj);
#else
        int result = pthread_mutex_lock(&Obj);
        Y_ABORT_UNLESS(result == 0, "mutex lock failure (%s)", LastSystemErrorText(result));
#endif
    }

#if defined(_win_)
    static bool TryEnterCriticalSectionInt(CRITICAL_SECTION* obj) {
    #if (_WIN32_WINNT < 0x0400)
        if (-1L == ::InterlockedCompareExchange(&obj->LockCount, 0, -1)) {
            obj->OwningThread = (HANDLE)(DWORD_PTR)::GetCurrentThreadId();
            obj->RecursionCount = 1;

            return true;
        }

        if (obj->OwningThread == (HANDLE)(DWORD_PTR)::GetCurrentThreadId()) {
            ::InterlockedIncrement(&obj->LockCount);
            ++obj->RecursionCount;
            return true;
        }

        return false;
    #else  // _WIN32_WINNT < 0x0400
        return TryEnterCriticalSection(obj);
    #endif // _WIN32_WINNT < 0x0400
    }
#endif // _win_

    inline bool TryAcquire() noexcept {
#if defined(_win_)
        return TryEnterCriticalSectionInt(&Obj);
#else
        int result = pthread_mutex_trylock(&Obj);
        if (result == 0 || result == EBUSY) {
            return result == 0;
        }
        Y_FAIL("mutex trylock failure (%s)", LastSystemErrorText(result));
#endif
    }

    inline void Release() noexcept {
#if defined(_win_)
        LeaveCriticalSection(&Obj);
#else
        int result = pthread_mutex_unlock(&Obj);
        Y_ABORT_UNLESS(result == 0, "mutex unlock failure (%s)", LastSystemErrorText(result));
#endif
    }

    inline void* Handle() const noexcept {
        return (void*)&Obj;
    }

private:
#ifdef _win_
    CRITICAL_SECTION Obj;
#else
    pthread_mutex_t Obj;
#endif
};

TMutex::TMutex()
    : Impl_(new TImpl())
{
}

TMutex::TMutex(TMutex&&) noexcept = default;

TMutex::~TMutex() = default;

void TMutex::Acquire() noexcept {
    Impl_->Acquire();
}

bool TMutex::TryAcquire() noexcept {
    return Impl_->TryAcquire();
}

void TMutex::Release() noexcept {
    Impl_->Release();
}

void* TMutex::Handle() const noexcept {
    return Impl_->Handle();
}