staticlib_pion  2.0.2
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
scheduler.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015, alex at staticlibs.net
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // ---------------------------------------------------------------------
18 // pion: a Boost C++ framework for building lightweight HTTP interfaces
19 // ---------------------------------------------------------------------
20 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
21 //
22 // Distributed under the Boost Software License, Version 1.0.
23 // See http://www.boost.org/LICENSE_1_0.txt
24 //
25 
26 #ifndef STATICLIB_PION_SCHEDULER_HPP
27 #define STATICLIB_PION_SCHEDULER_HPP
28 
29 #include <cstdint>
30 #include <chrono>
31 #include <condition_variable>
32 #include <functional>
33 #include <memory>
34 #include <mutex>
35 #include <thread>
36 #include <vector>
37 
38 #include "asio.hpp"
39 
40 #include "staticlib/config.hpp"
41 
42 namespace staticlib {
43 namespace pion {
44 
48 class scheduler {
52  std::mutex mutex;
53 
57  std::condition_variable no_more_active_users;
58 
62  std::condition_variable scheduler_has_stopped;
63 
67  uint32_t num_threads;
68 
72  uint32_t active_users;
73 
77  bool running;
78 
82  std::vector<std::unique_ptr<std::thread>> thread_pool;
83 
87  asio::io_service asio_service;
88 
92  asio::steady_timer timer;
93 
97  std::function<void() /* noexcept */> thread_stop_hook;
98 
99 public:
100 
104  scheduler(uint32_t number_of_threads) :
105  num_threads(number_of_threads),
106  active_users(0),
107  running(false),
108  asio_service(),
109  timer(asio_service),
110  thread_stop_hook([]() STATICLIB_NOEXCEPT {}) { }
111 
115  scheduler(const scheduler&) = delete;
116 
120  scheduler& operator=(const scheduler&) = delete;
121 
126  shutdown();
127  }
128 
132  void startup();
133 
137  void shutdown();
138 
142  void join();
143 
149  void add_active_user();
150 
154  void remove_active_user();
155 
161  bool is_running() const {
162  return running;
163  }
164 
170  asio::io_service& get_io_service() {
171  return asio_service;
172  }
173 
179  void post(std::function<void()> work_func) {
180  get_io_service().post(work_func);
181  }
182 
189  void keep_running(asio::io_service& my_service, asio::steady_timer& my_timer);
190 
196  void process_service_work(asio::io_service& service);
197 
204  void set_thread_stop_hook(std::function<void() /* noexcept */> hook) {
205  std::lock_guard<std::mutex> scheduler_lock{mutex};
206  this->thread_stop_hook = hook;
207  }
208 
209 private:
210 
214  void stop_threads();
215 
216 };
217 
218 } // namespace
219 }
220 
221 #endif // STATICLIB_PION_SCHEDULER_HPP