aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/test/ut/count_down_latch.h
blob: c0f15256b7e5003469ee846567d3b2ad4141d799 (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 <library/cpp/deprecated/atomic/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);
    }
};