aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/event.h
blob: cab2fc478ab01757fd247607571179912e83bcb1 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#pragma once

#include <util/generic/ptr.h>
#include <util/datetime/base.h>

struct TEventResetType {
    enum ResetMode {
        rAuto,   // the state will be nonsignaled after Wait() returns
        rManual, // we need call Reset() to set the state to nonsignaled.
    };
};

/**
 * DEPRECATED!
 *
 * Use TAutoEvent, TManualEvent for the direct replacement.
 * Use TManualEvent to prevent SEGFAULT (http://nga.at.yandex-team.ru/5772).
 */
class TSystemEvent: public TEventResetType {
public:
    TSystemEvent(ResetMode rmode = rManual);
    TSystemEvent(const TSystemEvent& other) noexcept;
    TSystemEvent& operator=(const TSystemEvent& other) noexcept;

    ~TSystemEvent();

    void Reset() noexcept;
    void Signal() noexcept;

    /*
     * return true if signaled, false if timed out.
     */
    bool WaitD(TInstant deadLine) noexcept;

    /*
     * return true if signaled, false if timed out.
     */
    inline bool WaitT(TDuration timeOut) noexcept {
        return WaitD(timeOut.ToDeadLine());
    }

    /*
     * wait infinite time
     */
    inline void WaitI() noexcept {
        WaitD(TInstant::Max());
    }

    //return true if signaled, false if timed out.
    inline bool Wait(ui32 timer) noexcept {
        return WaitT(TDuration::MilliSeconds(timer));
    }

    inline bool Wait() noexcept {
        WaitI();

        return true;
    }

private:
    class TEvImpl;
    TIntrusivePtr<TEvImpl> EvImpl_;
};

class TAutoEvent: public TSystemEvent {
public:
    TAutoEvent()
        : TSystemEvent(TSystemEvent::rAuto)
    {
    }

private:
    void Reset() noexcept;
};

/**
 * Prevents from a "shortcut problem" (see http://nga.at.yandex-team.ru/5772): if Wait will be called after Signaled
 * flag set to true in Signal method but before CondVar.BroadCast - Wait will shortcut (without actual wait on condvar).
 * If Wait thread will destruct event - Signal thread will do broadcast on a destructed CondVar.
 */
class TManualEvent {
public:
    TManualEvent()
        : Ev(TEventResetType::rManual)
    {
    }

    void Reset() noexcept {
        TSystemEvent{Ev}.Reset();
    }

    void Signal() noexcept {
        TSystemEvent{Ev}.Signal();
    }

    /** return true if signaled, false if timed out. */
    bool WaitD(TInstant deadLine) noexcept {
        return TSystemEvent{Ev}.WaitD(deadLine);
    }

    /** return true if signaled, false if timed out. */
    inline bool WaitT(TDuration timeOut) noexcept {
        return TSystemEvent{Ev}.WaitT(timeOut);
    }

    /** Wait infinite time */
    inline void WaitI() noexcept {
        TSystemEvent{Ev}.WaitI();
    }

    /** return true if signaled, false if timed out. */
    inline bool Wait(ui32 timer) noexcept {
        return TSystemEvent{Ev}.Wait(timer);
    }

    inline bool Wait() noexcept {
        return TSystemEvent{Ev}.Wait();
    }

private:
    TSystemEvent Ev;
};