aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/cc_semaphore.h
blob: 17d3b3bf323f18f1417098b3c7af4ea88e15f07f (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); 
    } 
};