aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/condvar.h
blob: b18e48432b2ac9c7f841e63eaf3b8bcf70e85468 (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
#pragma once

#include "mutex.h"

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

#include <utility>

class TCondVar {
public:
    TCondVar();
    ~TCondVar(); 

    void BroadCast() noexcept; 
    void Signal() noexcept; 

    /*
     * returns false if failed by timeout
     */
    bool WaitD(TMutex& m, TInstant deadline) noexcept;

    template <typename P>
    inline bool WaitD(TMutex& m, TInstant deadline, P pred) noexcept {
        while (!pred()) {
            if (!WaitD(m, deadline)) {
                return pred();
            }
        }
        return true;
    }

    /*
     * returns false if failed by timeout
     */
    inline bool WaitT(TMutex& m, TDuration timeout) noexcept {
        return WaitD(m, timeout.ToDeadLine());
    }

    template <typename P>
    inline bool WaitT(TMutex& m, TDuration timeout, P pred) noexcept {
        return WaitD(m, timeout.ToDeadLine(), std::move(pred));
    }

    /*
     * infinite wait
     */
    inline void WaitI(TMutex& m) noexcept { 
        WaitD(m, TInstant::Max());
    }

    template <typename P>
    inline void WaitI(TMutex& m, P pred) noexcept {
        WaitD(m, TInstant::Max(), std::move(pred));
    }

    //deprecated
    inline void Wait(TMutex& m) noexcept { 
        WaitI(m);
    }

    template <typename P>
    inline void Wait(TMutex& m, P pred) noexcept {
        WaitI(m, std::move(pred));
    }

private:
    class TImpl;
    THolder<TImpl> Impl_;
};