aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/coroutine/engine/cont_poller.h
blob: e6b23149c33f4e7684ae6c66698aea88b706a299 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#pragma once
 
#include "poller.h" 
#include "sockmap.h"
 
#include <library/cpp/containers/intrusive_rb_tree/rb_tree.h>

#include <util/datetime/base.h>
#include <util/memory/pool.h>
#include <util/memory/smallobj.h> 
#include <util/network/init.h>
 
#include <cerrno>


class TCont; 
class TContExecutor; 
class TFdEvent;
 
namespace NCoro {

    class IPollEvent;
 
 
    struct TContPollEventCompare {
        template <class T>
        static inline bool Compare(const T& l, const T& r) noexcept {
            return l.DeadLine() < r.DeadLine() || (l.DeadLine() == r.DeadLine() && &l < &r);
        }
    };

 
    class TContPollEvent : public TRbTreeItem<TContPollEvent, TContPollEventCompare> {
    public:
        TContPollEvent(TCont* cont, TInstant deadLine) noexcept
            : Cont_(cont)
            , DeadLine_(deadLine)
        {}
 
        static bool Compare(const TContPollEvent& l, const TContPollEvent& r) noexcept {
            return l.DeadLine() < r.DeadLine() || (l.DeadLine() == r.DeadLine() && &l < &r);
        }
 
        int Status() const noexcept {
            return Status_;
        }
 
        void SetStatus(int status) noexcept {
            Status_ = status;
        }
 
        TCont* Cont() noexcept {
            return Cont_;
        }
 
        TInstant DeadLine() const noexcept {
            return DeadLine_;
        }
 
        void Wake(int status) noexcept {
            SetStatus(status);
            Wake();
        } 
 
    private:
        void Wake() noexcept;
 
    private:
        TCont* Cont_;
        TInstant DeadLine_;
        int Status_ = EINPROGRESS;
    }; 
 
 
    class IPollEvent: public TIntrusiveListItem<IPollEvent> {
    public:
        IPollEvent(SOCKET fd, ui16 what) noexcept
            : Fd_(fd)
            , What_(what)
        {}
 
        virtual ~IPollEvent() {}
 
        SOCKET Fd() const noexcept {
            return Fd_;
        }
 
        int What() const noexcept {
            return What_;
        }
 
        virtual void OnPollEvent(int status) noexcept = 0;
 
    private:
        SOCKET Fd_;
        ui16 What_;
    };
 
 
    template <class T>
    class TBigArray {
        struct TValue: public T, public TObjectFromPool<TValue> {
            TValue() {}
        };

    public:
        TBigArray()
            : Pool_(TMemoryPool::TExpGrow::Instance(), TDefaultAllocator::Instance())
        {}

        T* Get(size_t index) {
            TRef& ret = Lst_.Get(index);
            if (!ret) {
                ret = TRef(new (&Pool_) TValue());
            }
            return ret.Get();
        }

    private:
        using TRef = THolder<TValue>;
        typename TValue::TPool Pool_;
        TSocketMap<TRef> Lst_;
    };


    using TPollEventList = TIntrusiveList<IPollEvent>;
 
    class TContPoller {
    public:
        using TEvent = IPollerFace::TEvent;
        using TEvents = IPollerFace::TEvents;
 
        TContPoller()
            : P_(IPollerFace::Default())
        {
        }
 
        explicit TContPoller(THolder<IPollerFace> poller)
            : P_(std::move(poller))
        {}
 
        void Schedule(IPollEvent* event) {
            auto* lst = Lists_.Get(event->Fd());
            const ui16 oldFlags = Flags(*lst);
            lst->PushFront(event);
            ui16 newFlags = Flags(*lst);
 
            if (newFlags != oldFlags) {
                if (oldFlags) {
                    newFlags |= CONT_POLL_MODIFY;
                }

                P_->Set(lst, event->Fd(), newFlags);
            }
        } 
 
        void Remove(IPollEvent* event) noexcept {
            auto* lst = Lists_.Get(event->Fd());
            const ui16 oldFlags = Flags(*lst);
            event->Unlink();
            ui16 newFlags = Flags(*lst);
 
            if (newFlags != oldFlags) {
                if (newFlags) {
                    newFlags |= CONT_POLL_MODIFY;
                }

                P_->Set(lst, event->Fd(), newFlags);
            }
        } 
 
        void Wait(TEvents& events, TInstant deadLine) {
            events.clear();
            P_->Wait(events, deadLine);
        } 
 
        EContPoller PollEngine() const {
            return P_->PollEngine();
        }
    private:
        static ui16 Flags(TIntrusiveList<IPollEvent>& lst)  noexcept {
            ui16 ret = 0;
            for (auto&& item : lst) {
                ret |= item.What();
            } 
            return ret;
        } 
 
    private:
        TBigArray<TPollEventList> Lists_;
        THolder<IPollerFace> P_;
    }; 


    class TEventWaitQueue {
        using TIoWait = TRbTree<NCoro::TContPollEvent, NCoro::TContPollEventCompare>;

    public:
        void Register(NCoro::TContPollEvent* event);

        bool Empty() const noexcept {
            return IoWait_.Empty();
        }

        void Abort() noexcept;

        TInstant WakeTimedout(TInstant now) noexcept;

    private:
        TIoWait IoWait_;
    };
}
 
class TFdEvent final:
    public NCoro::TContPollEvent,
    public NCoro::IPollEvent
{
public: 
    TFdEvent(TCont* cont, SOCKET fd, ui16 what, TInstant deadLine) noexcept
        : TContPollEvent(cont, deadLine)
        , IPollEvent(fd, what)
    {}

    ~TFdEvent() {
        RemoveFromIOWait();
    } 
 
    void RemoveFromIOWait() noexcept;
 
    void OnPollEvent(int status) noexcept override {
        Wake(status);
    } 
}; 
 

class TTimerEvent: public NCoro::TContPollEvent {
public: 
    TTimerEvent(TCont* cont, TInstant deadLine) noexcept
        : TContPollEvent(cont, deadLine)
    {}
}; 

int ExecuteEvent(TFdEvent* event) noexcept;

int ExecuteEvent(TTimerEvent* event) noexcept;