aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/test/ut/count_down_latch.h
blob: 5117db573166ac20805b7820f08b618622d9fe78 (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
#pragma once

#include <util/system/atomic.h>
#include <util/system/event.h>

class TCountDownLatch {
private:
    TAtomic Current;
    TSystemEvent EventObject;

public:
    TCountDownLatch(unsigned initial)
        : Current(initial)
    {
    }

    void CountDown() {
        if (AtomicDecrement(Current) == 0) {
            EventObject.Signal();
        }
    }

    void Await() {
        EventObject.Wait();
    }

    bool Await(TDuration timeout) {
        return EventObject.WaitT(timeout);
    }
};