aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/neh/wfmo.h
blob: 8e2cd6052a1cfc982ddc2539647e3d1209602a9f (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
#pragma once

#include "lfqueue.h"

#include <library/cpp/threading/atomic/bool.h>

#include <util/generic/vector.h>
#include <util/generic/scope.h>
#include <library/cpp/deprecated/atomic/atomic.h>
#include <library/cpp/deprecated/atomic/atomic_ops.h>
#include <util/system/event.h>
#include <util/system/spinlock.h>

namespace NNeh {
    template <class T>
    class TBlockedQueue: public TLockFreeQueue<T>, public TSystemEvent {
    public:
        inline TBlockedQueue() noexcept
            : TSystemEvent(TSystemEvent::rAuto)
        {
        }

        inline void Notify(T t) noexcept {
            this->Enqueue(t);
            Signal();
        }
    };

    class TWaitQueue: public TThrRefBase {
    public:
        class TWaitHandle {
        public:

            ~TWaitHandle() {
                SwapWaitQueue(nullptr);
            }

            void Signal() noexcept {
                Signalled_ = true;

                if (TIntrusivePtr<TWaitQueue> q = SwapWaitQueue(nullptr)) {
                    q->Notify(this);
                }
            }

            void Register(TIntrusivePtr<TWaitQueue>& waitQueue) noexcept {
                if (Signalled_) {
                    waitQueue->Notify(this);
                    SwapWaitQueue(nullptr);
                    return;
                }

                waitQueue->Ref();
                SwapWaitQueue(waitQueue.Get());

                if (Signalled_) {
                    if (TIntrusivePtr<TWaitQueue> q = SwapWaitQueue(nullptr)) {
                        q->Notify(this);
                    }
                }
            }

            bool Signalled() const {
                return Signalled_;
            }

            void ResetState() {
                Signalled_ = false;
                SwapWaitQueue(nullptr);
            }
        private:
            TIntrusivePtr<TWaitQueue> SwapWaitQueue(TWaitQueue* newQueue) noexcept {
                return TIntrusivePtr<TWaitQueue>(AtomicSwap(&WaitQueue_, newQueue), TIntrusivePtr<TWaitQueue>::TNoIncrement());
            }
        private:
            NAtomic::TBool Signalled_ = false;
            TWaitQueue* WaitQueue_ = nullptr;
        };

        inline bool Wait(const TInstant& deadLine) noexcept {
            return Q_.WaitD(deadLine);
        }

        inline void Notify(TWaitHandle* wq) noexcept {
            Q_.Notify(wq);
        }

        inline bool Dequeue(TWaitHandle** wq) noexcept {
            return Q_.Dequeue(wq);
        }

    private:
        TBlockedQueue<TWaitHandle*> Q_;
    };

    typedef TWaitQueue::TWaitHandle TWaitHandle;

    template <class T>
    static inline void WaitForMultipleObj(TWaitQueue& hndl, const TInstant& deadLine, T& func) {
        do {
            TWaitHandle* ret = nullptr;

            if (hndl.Dequeue(&ret)) {
                do {
                    func(ret);
                } while (hndl.Dequeue(&ret));

                return;
            }
        } while (hndl.Wait(deadLine));
    }

    struct TSignalled {
        inline TSignalled()
            : Signalled(false)
        {
        }

        inline void operator()(const TWaitHandle*) noexcept {
            Signalled = true;
        }

        bool Signalled;
    };

    static inline bool WaitForOne(TWaitHandle& wh, const TInstant& deadLine) {
        TSignalled func;
        auto hndl = MakeIntrusive<TWaitQueue>();
        wh.Register(hndl);

        WaitForMultipleObj(*hndl, deadLine, func);

        return func.Signalled;
    }
}