]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/crimson/seastar_runner.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / test / crimson / seastar_runner.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #pragma once
5
6 #include <stdio.h>
7 #include <signal.h>
8 #include <thread>
9
10 #include <seastar/core/app-template.hh>
11 #include <seastar/core/future-util.hh>
12 #include <seastar/core/reactor.hh>
13 #include <seastar/core/alien.hh>
14 #include <seastar/core/thread.hh>
15
16 struct SeastarRunner {
17 seastar::app_template app;
18 seastar::file_desc begin_fd;
19 std::unique_ptr<seastar::readable_eventfd> on_end;
20
21 std::thread thread;
22
23 SeastarRunner() :
24 begin_fd{seastar::file_desc::eventfd(0, 0)} {}
25
26 ~SeastarRunner() {}
27
28 void init(int argc, char **argv)
29 {
30 thread = std::thread([argc, argv, this] { reactor(argc, argv); });
31 eventfd_t result = 0;
32 if (int r = ::eventfd_read(begin_fd.get(), &result); r < 0) {
33 std::cerr << "unable to eventfd_read():" << errno << std::endl;
34 throw std::runtime_error("Cannot start seastar");
35 }
36 }
37
38 void stop()
39 {
40 run([this] {
41 on_end->write_side().signal(1);
42 return seastar::now();
43 });
44 thread.join();
45 }
46
47 void reactor(int argc, char **argv)
48 {
49 app.run(argc, argv, [this] {
50 on_end.reset(new seastar::readable_eventfd);
51 return seastar::now().then([this] {
52 ::eventfd_write(begin_fd.get(), 1);
53 return seastar::now();
54 }).then([this] {
55 return on_end->wait().then([](size_t){});
56 }).handle_exception([](auto ep) {
57 std::cerr << "Error: " << ep << std::endl;
58 }).finally([this] {
59 on_end.reset();
60 });
61 });
62 }
63
64 template <typename Func>
65 void run(Func &&func) {
66 auto fut = seastar::alien::submit_to(0, std::forward<Func>(func));
67 fut.get();
68 }
69 };
70
71