2 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef ANBOX_RUNTIME_H_
19 #define ANBOX_RUNTIME_H_
21 #include <boost/asio.hpp>
22 #include <boost/version.hpp>
28 #include "anbox/do_not_copy_or_move.h"
32 // We bundle our "global" runtime dependencies here, specifically
33 // a dispatcher to decouple multiple in-process providers from one
34 // another , forcing execution to a well known set of threads.
35 class Runtime : public DoNotCopyOrMove,
36 public std::enable_shared_from_this<Runtime> {
38 // Our default concurrency setup.
39 static constexpr const std::uint32_t worker_threads = 8;
41 // create returns a Runtime instance with pool_size worker threads
42 // executing the underlying service.
43 static std::shared_ptr<Runtime> create(
44 std::uint32_t pool_size = worker_threads);
46 // Tears down the runtime, stopping all worker threads.
47 ~Runtime() noexcept(true);
49 // start executes the underlying io_service on a thread pool with
50 // the size configured at creation time.
53 // stop cleanly shuts down a Runtime instance.
56 // to_dispatcher_functional returns a function for integration
57 // with components that expect a dispatcher for operation.
58 std::function<void(std::function<void()>)> to_dispatcher_functional();
60 // service returns the underlying boost::asio::io_service that is executed
62 boost::asio::io_service& service();
65 // Runtime constructs a new instance, firing up pool_size
67 Runtime(std::uint32_t pool_size);
69 std::uint32_t pool_size_;
70 boost::asio::io_service service_;
71 boost::asio::io_service::strand strand_;
72 boost::asio::io_service::work keep_alive_;
73 std::vector<std::thread> workers_;