aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/coro/async_io.cpp
blob: 259850c0370c58d112b68913f2feaf809a8d9415 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "async_io.h"
#include "detached.h"
#include <vespa/vespalib/net/selector.h>
#include <vespa/vespalib/util/require.h>
#include <vespa/vespalib/util/time.h>
#include <vespa/config.h>

#include <thread>
#include <atomic>
#include <vector>
#include <map>
#include <set>

#ifdef VESPA_HAS_IO_URING
#include "io_uring_thread.hpp"
namespace {
bool can_use_io_uring() { return vespalib::coro::UringProbe::check_support(); }
vespalib::coro::AsyncIo::SP create_io_uring_thread() { return std::make_shared<vespalib::coro::IoUringThread>(); }
}
#else
namespace {
bool can_use_io_uring() { return false; }
vespalib::coro::AsyncIo::SP create_io_uring_thread() { abort(); }
}
#endif

namespace vespalib::coro {

namespace {

struct SelectorThread : AsyncIo {

    using BoolOp = WaitingFor<bool>;

    struct FdContext {
        int    _fd;
        bool   _epoll_read;
        bool   _epoll_write;
        BoolOp _reader;
        BoolOp _writer;
        FdContext(int fd_in)
          : _fd(fd_in),
            _epoll_read(false), _epoll_write(false),
            _reader(), _writer() {}
    };
    struct RunGuard;
    using ThreadId = std::atomic<std::thread::id>;

    std::map<int,FdContext> _state;
    std::set<int>           _check;
    Selector<FdContext>     _selector;
    std::thread             _thread;
    ThreadId                _thread_id;
    bool                    _check_queue;
    std::vector<BoolOp>     _todo;
    std::mutex              _lock;
    std::vector<BoolOp>     _queue;

    SelectorThread()
      : _state(),
        _check(),
        _selector(),
        _thread(),
        _thread_id(std::thread::id()),
        _check_queue(false),
        _todo(),
        _lock(),
        _queue()
    {
        static_assert(ThreadId::is_always_lock_free);
    }
    void start() override;
    void main();
    void init_shutdown() override;
    void fini_shutdown() override;
    ~SelectorThread();
    bool running() const noexcept {
        return (_thread_id.load(std::memory_order_relaxed) != std::thread::id());
    }
    bool stopped() const noexcept {
        return (_thread_id.load(std::memory_order_relaxed) == std::thread::id());
    }
    bool in_thread() const noexcept {
        return (std::this_thread::get_id() == _thread_id.load(std::memory_order_relaxed));
    }
    auto protect() { return std::lock_guard(_lock); }
    auto async_run() {
        return wait_for<bool>([this](auto wf)
                              {
                                  AsyncIo::SP wakeup_guard;
                                  {
                                      auto guard = protect();
                                      if (stopped()) {
                                          wf.set_value(false);
                                          return wf.mu();
                                      }
                                      if (_queue.empty()) {
                                          wakeup_guard = shared_from_this();
                                      }
                                      _queue.push_back(std::move(wf));
                                  }
                                  if (wakeup_guard) {
                                      _selector.wakeup();
                                  }
                                  return wf.nop();
                              });
    }
    auto readable(int fd) {
        return wait_for<bool>([fd, this](auto wf)
                              {
                                  if ((fd < 0) || stopped()) {
                                      wf.set_value(false);
                                      return wf.mu();
                                  }
                                  auto [pos, ignore] = _state.try_emplace(fd, fd);
                                  FdContext &state = pos->second;
                                  REQUIRE(!state._reader && "conflicting reads detected");
                                  state._reader = std::move(wf);
                                  _check.insert(state._fd);
                                  return wf.nop();
                              });
    }
    auto writable(int fd) {
        return wait_for<bool>([fd,this](auto wf)
                              {
                                  if ((fd < 0) || stopped()) {
                                      wf.set_value(false);
                                      return wf.mu();
                                  }
                                  auto [pos, ignore] = _state.try_emplace(fd, fd);
                                  FdContext &state = pos->second;
                                  REQUIRE(!state._writer && "conflicting writes detected");
                                  state._writer = std::move(wf);
                                  _check.insert(state._fd);
                                  return wf.nop();
                              });
    }
    void update_epoll_state() {
        for (int fd: _check) {
            auto pos = _state.find(fd);
            REQUIRE(pos != _state.end());
            FdContext &ctx = pos->second;
            const bool keep_entry = (ctx._reader || ctx._writer);
            const bool was_added = (ctx._epoll_read || ctx._epoll_write);
            if (keep_entry) {
                if (was_added) {
                    bool read_changed = ctx._epoll_read != bool(ctx._reader);
                    bool write_changed = ctx._epoll_write != bool(ctx._writer);
                    if (read_changed || write_changed) {
                        _selector.update(ctx._fd, ctx, bool(ctx._reader), bool(ctx._writer));
                    }
                } else {
                    _selector.add(ctx._fd, ctx, bool(ctx._reader), bool(ctx._writer));
                }
                ctx._epoll_read = bool(ctx._reader);
                ctx._epoll_write = bool(ctx._writer);
            } else {
                if (was_added) {
                    _selector.remove(ctx._fd);
                }
                _state.erase(pos);
            }
        }
        _check.clear();
    }
    void handle_wakeup() { _check_queue = true; }
    void handle_queue(bool result) {
        if (!_check_queue) {
            return;
        }
        _check_queue = false;
        {
            auto guard = protect();
            std::swap(_todo, _queue);
        }
        for (auto &&entry: _todo) {
            auto wf = std::move(entry);
            wf.set_value(result);
        }
        _todo.clear();
    }
    void handle_event(FdContext &ctx, bool read, bool write) {
        _check.insert(ctx._fd);
        if (read && ctx._reader) {
            auto reader = std::move(ctx._reader);
            reader.set_value(true);
        }
        if (write && ctx._writer) {
            auto writer = std::move(ctx._writer);
            writer.set_value(true);
        }
    }
    ImplTag get_impl_tag() override { return ImplTag::EPOLL; }
    Lazy<SocketHandle> accept(ServerSocket &server_socket) override {
        bool inside = in_thread() ? true : co_await async_run();
        if (inside) {
            bool can_read = co_await readable(server_socket.get_fd());
            if (can_read) {
                auto res = server_socket.accept();
                if (res.valid()) {
                    res.set_blocking(false);
                }
                co_return res;
            }
        }
        co_return SocketHandle(-ECANCELED);
    }
    Lazy<SocketHandle> connect(const SocketAddress &addr) override {
        bool inside = in_thread() ? true : co_await async_run();
        if (inside) {
            auto tweak = [](SocketHandle &handle){ return handle.set_blocking(false); };
            auto socket = addr.connect(tweak);
            bool can_write = co_await writable(socket.get());
            if (can_write) {
                co_return socket;
            }
        }
        co_return SocketHandle(-ECANCELED);
    }
    Lazy<ssize_t> read(SocketHandle &socket, char *buf, size_t len) override {
        bool inside = in_thread() ? true : co_await async_run();
        if (inside) {
            bool can_read = co_await readable(socket.get());
            if (can_read) {
                ssize_t res = socket.read(buf, len);
                co_return (res < 0) ? -errno : res;
            }
        }
        co_return -ECANCELED;
    }
    Lazy<ssize_t> write(SocketHandle &socket, const char *buf, size_t len) override {
        bool inside = in_thread() ? true : co_await async_run();
        if (inside) {
            bool can_write = co_await writable(socket.get());
            if (can_write) {
                ssize_t res = socket.write(buf, len);
                co_return (res < 0) ? -errno : res;
            }
        }
        co_return -ECANCELED;
    }
    Lazy<bool> schedule() override {
        co_return co_await async_run();
    }
    Detached async_shutdown() {
        bool inside = co_await async_run();
        REQUIRE(inside && "unable to initialize shutdown of internal thread");
        {
            auto guard = protect();
            _thread_id = std::thread::id();
        }
    }
};

void
SelectorThread::start()
{
    _thread = std::thread(&SelectorThread::main, this);
    _thread_id.wait(std::thread::id());
}

struct SelectorThread::RunGuard {
    SelectorThread &self;
    RunGuard(SelectorThread &self_in) noexcept : self(self_in) {
        self._thread_id = std::this_thread::get_id();
        self._thread_id.notify_all();
    }
    ~RunGuard() {
        REQUIRE(self.stopped());
        self._check.clear();
        for (auto &entry: self._state) {
            FdContext &ctx = entry.second;
            const bool was_added = (ctx._epoll_read || ctx._epoll_write);
            if (was_added) {
                self._selector.remove(ctx._fd);
            }
            if (ctx._reader) {
                auto reader = std::move(ctx._reader);
                reader.set_value(false);
            }
            if (ctx._writer) {
                auto writer = std::move(ctx._writer);
                writer.set_value(false);
            }
        }
        self._state.clear();
        REQUIRE(self._check.empty());
        self._check_queue = true;
        self.handle_queue(false);
    }
};

void
SelectorThread::main()
{
    RunGuard guard(*this);
    while (running()) {
        update_epoll_state();
        _selector.poll(1000);
        _selector.dispatch(*this);
        handle_queue(true);
    }
}

void
SelectorThread::init_shutdown()
{
    async_shutdown();
}

void
SelectorThread::fini_shutdown()
{
    _thread.join();
}

SelectorThread::~SelectorThread()
{
    REQUIRE(_state.empty());
    REQUIRE(_check.empty());
    REQUIRE(_todo.empty());
    REQUIRE(_queue.empty());
}

} // <unnamed>

AsyncIo::~AsyncIo() = default;
AsyncIo::AsyncIo() = default;

AsyncIo::Owner::Owner(std::shared_ptr<AsyncIo> async_io)
  : _async_io(std::move(async_io)),
    _init_shutdown_called(false),
    _fini_shutdown_called(false)
{
    _async_io->start();
}

void
AsyncIo::Owner::init_shutdown()
{
    if (!_init_shutdown_called) {
        if (_async_io) {
            _async_io->init_shutdown();
        }
        _init_shutdown_called = true;
    }
}

void
AsyncIo::Owner::fini_shutdown()
{
    if (!_fini_shutdown_called) {
        init_shutdown();
        if (_async_io) {
            _async_io->fini_shutdown();
        }
        _fini_shutdown_called = true;
    }
}

AsyncIo::Owner::~Owner()
{
    fini_shutdown();
}

AsyncIo::Owner
AsyncIo::create(ImplTag prefer_impl) {
    if (prefer_impl == ImplTag::URING && can_use_io_uring()) {
        return Owner(create_io_uring_thread());
    }
    return Owner(std::make_shared<SelectorThread>());
}

} // vespalib::coro