TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / runtime.h
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
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.
7  *
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.
12  *
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/>.
15  *
16  */
17
18 #ifndef ANBOX_RUNTIME_H_
19 #define ANBOX_RUNTIME_H_
20
21 #include <boost/asio.hpp>
22 #include <boost/version.hpp>
23
24 #include <memory.h>
25 #include <functional>
26 #include <thread>
27
28 #include "anbox/do_not_copy_or_move.h"
29
30 namespace anbox {
31
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> {
37  public:
38   // Our default concurrency setup.
39   static constexpr const std::uint32_t worker_threads = 8;
40
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);
45
46   // Tears down the runtime, stopping all worker threads.
47   ~Runtime() noexcept(true);
48
49   // start executes the underlying io_service on a thread pool with
50   // the size configured at creation time.
51   void start();
52
53   // stop cleanly shuts down a Runtime instance.
54   void stop();
55
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();
59
60   // service returns the underlying boost::asio::io_service that is executed
61   // by the Runtime.
62   boost::asio::io_service& service();
63
64  private:
65   // Runtime constructs a new instance, firing up pool_size
66   // worker threads.
67   Runtime(std::uint32_t pool_size);
68
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_;
74 };
75
76 }  // namespace anbox
77
78 #endif