aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/cc_semaphore.h
blob: 0df8a3d664e2e2db331e7a15128c856fcee3575f (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
#pragma once

#include "latch.h"

template <typename TThis>
class TComplexConditionSemaphore {
private:
    TLatch Latch;

public:
    void Updated() {
        if (GetThis()->TryWait()) {
            Latch.Unlock();
        }
    }

    void Wait() {
        while (!GetThis()->TryWait()) {
            Latch.Lock();
            if (GetThis()->TryWait()) {
                Latch.Unlock();
                return;
            }
            Latch.Wait();
        }
    }

    bool IsLocked() {
        return Latch.IsLocked();
    }

private:
    TThis* GetThis() {
        return static_cast<TThis*>(this);
    }
};