aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/coroutine/engine/mutex.h
blob: 93e9119503b66445f5742e3e7f2619974e85b40c (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
#pragma once

#include "impl.h"
#include "events.h"

class TContMutex {
public:
    TContMutex() noexcept
        : Token_(true)
    {
    }

    ~TContMutex() {
        Y_ASSERT(Token_);
    }

    int LockD(TCont* current, TInstant deadline) {
        while (!Token_) {
            const int ret = WaitQueue_.WaitD(current, deadline);

            if (ret != EWAKEDUP) {
                return ret;
            }
        }

        Token_ = false;

        return 0;
    }

    int LockT(TCont* current, TDuration timeout) {
        return LockD(current, timeout.ToDeadLine());
    }

    int LockI(TCont* current) {
        return LockD(current, TInstant::Max());
    }

    void UnLock() noexcept {
        Y_ASSERT(!Token_);

        Token_ = true;
        WaitQueue_.Signal();
    }

private:
    TContWaitQueue WaitQueue_;
    bool Token_;
};