aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/condvar.cpp
blob: 707db52c0ba756986d0a0ca5e1e6549107e77af2 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "event.h"
#include "mutex.h"
#include "yassert.h"
#include "condvar.h"
#include "datetime.h"
#include "spinlock.h"

#include <util/generic/ylimits.h>
#include <util/generic/intrlist.h>
#include <util/generic/yexception.h>

#include <cstdlib>

#if defined(_unix_)
    #include <sys/time.h>
    #include <pthread.h>
    #include <cerrno>
#endif

namespace {
    class TCondVarImpl {
        using TLock = TAdaptiveLock;

        struct TWaitEvent: public TIntrusiveListItem<TWaitEvent>, public TSystemEvent {
        };

        using TWaitEvents = TIntrusiveList<TWaitEvent>;

    public:
        inline ~TCondVarImpl() { 
            Y_ASSERT(Events_.Empty());
        }

        inline void Signal() noexcept { 
            with_lock (Lock_) {
                if (!Events_.Empty()) {
                    Events_.PopFront()->Signal();
                }
            }
        }

        inline void BroadCast() noexcept { 
            with_lock (Lock_) {
                //TODO
                while (!Events_.Empty()) {
                    Events_.PopFront()->Signal();
                }
            }
        }

        inline bool WaitD(TMutex& m, TInstant deadLine) noexcept { 
            TWaitEvent event;

            with_lock (Lock_) {
                Events_.PushBack(&event);
            }

            m.Release();

            const bool signalled = event.WaitD(deadLine);

            m.Acquire();

            with_lock (Lock_) {
                event.Unlink();
            }

            return signalled;
        }

    private:
        TWaitEvents Events_;
        TLock Lock_;
    };
}

#if defined(_win_)
class TCondVar::TImpl: public TCondVarImpl {
};
#else
class TCondVar::TImpl {
public:
    inline TImpl() {
        if (pthread_cond_init(&Cond_, nullptr)) {
            ythrow yexception() << "can not create condvar(" << LastSystemErrorText() << ")";
        }
    }

    inline ~TImpl() { 
        int ret = pthread_cond_destroy(&Cond_);
        Y_VERIFY(ret == 0, "pthread_cond_destroy failed: %s", LastSystemErrorText(ret));
    }

    inline void Signal() noexcept { 
        int ret = pthread_cond_signal(&Cond_);
        Y_VERIFY(ret == 0, "pthread_cond_signal failed: %s", LastSystemErrorText(ret));
    }

    inline bool WaitD(TMutex& lock, TInstant deadLine) noexcept { 
        if (deadLine == TInstant::Max()) {
            int ret = pthread_cond_wait(&Cond_, (pthread_mutex_t*)lock.Handle());
            Y_VERIFY(ret == 0, "pthread_cond_wait failed: %s", LastSystemErrorText(ret));
            return true;
        } else {
            struct timespec spec;

            Zero(spec);

            spec.tv_sec = deadLine.Seconds();
            spec.tv_nsec = deadLine.NanoSecondsOfSecond();

            int ret = pthread_cond_timedwait(&Cond_, (pthread_mutex_t*)lock.Handle(), &spec);

            Y_VERIFY(ret == 0 || ret == ETIMEDOUT, "pthread_cond_timedwait failed: %s", LastSystemErrorText(ret));

            return ret == 0;
        }
    }

    inline void BroadCast() noexcept { 
        int ret = pthread_cond_broadcast(&Cond_);
        Y_VERIFY(ret == 0, "pthread_cond_broadcast failed: %s", LastSystemErrorText(ret));
    }

private:
    pthread_cond_t Cond_;
};
#endif

TCondVar::TCondVar()
    : Impl_(new TImpl)
{
}

TCondVar::~TCondVar() = default;

void TCondVar::BroadCast() noexcept { 
    Impl_->BroadCast();
}

void TCondVar::Signal() noexcept { 
    Impl_->Signal();
}

bool TCondVar::WaitD(TMutex& mutex, TInstant deadLine) noexcept { 
    return Impl_->WaitD(mutex, deadLine);
}