aboutsummaryrefslogtreecommitdiffstats
path: root/include/http_server.h
diff options
context:
space:
mode:
authoripknHama <ipknhama@gmail.com>2015-02-20 13:44:46 +0900
committeripknHama <ipknhama@gmail.com>2015-02-20 13:44:46 +0900
commit48811ce4a47200567796730d7467526683f265d7 (patch)
treef54890849a58160c39fc67073316c1349463d4f6 /include/http_server.h
parent5507e98ce25f8468e37652b58a3512f40869af99 (diff)
downloadcrow-48811ce4a47200567796730d7467526683f265d7.tar.gz
crow-48811ce4a47200567796730d7467526683f265d7.zip
remove thread_local variables
* move thread_local variables forget_cached_date_str, timer_queue into each threads local stack
Diffstat (limited to 'include/http_server.h')
-rw-r--r--include/http_server.h54
1 files changed, 49 insertions, 5 deletions
diff --git a/include/http_server.h b/include/http_server.h
index bd35ba1..e7d9f4f 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -10,7 +10,6 @@
#include <memory>
#include "http_connection.h"
-#include "datetime.h"
#include "logging.h"
#include "dumb_timer_queue.h"
@@ -40,13 +39,46 @@ namespace crow
for(int i = 0; i < concurrency_; i++)
io_service_pool_.emplace_back(new boost::asio::io_service());
+ get_cached_date_str_pool_.resize(concurrency_);
+ timer_queue_pool_.resize(concurrency_);
std::vector<std::future<void>> v;
for(uint16_t i = 0; i < concurrency_; i ++)
v.push_back(
std::async(std::launch::async, [this, i]{
+
+ // thread local date string get function
+ auto last = std::chrono::steady_clock::now();
+
+ std::string date_str;
+ auto update_date_str = [&]
+ {
+ auto last_time_t = time(0);
+ tm my_tm;
+
+#ifdef _MSC_VER
+ gmtime_s(&my_tm, &last_time_t);
+#else
+ gmtime_r(&last_time_t, &my_tm);
+#endif
+ date_str.resize(100);
+ size_t date_str_sz = strftime(&date_str[0], 99, "%a, %d %b %Y %H:%M:%S GMT", &my_tm);
+ date_str.resize(date_str_sz);
+ };
+ update_date_str();
+ get_cached_date_str_pool_[i] = [&]()->std::string
+ {
+ if (std::chrono::steady_clock::now() - last >= std::chrono::seconds(1))
+ {
+ last = std::chrono::steady_clock::now();
+ update_date_str();
+ }
+ return date_str;
+ };
+
// initializing timer queue
- auto& timer_queue = detail::dumb_timer_queue::get_current_dumb_timer_queue();
+ detail::dumb_timer_queue timer_queue;
+ timer_queue_pool_[i] = &timer_queue;
timer_queue.set_io_service(*io_service_pool_[i]);
boost::asio::deadline_timer timer(*io_service_pool_[i]);
@@ -71,12 +103,18 @@ namespace crow
stop();
});
+ for (int i = 0; i < concurrency_; i++)
+ {
+ while (timer_queue_pool_[i] == nullptr)
+ std::this_thread::yield();
+ }
+
do_accept();
- v.push_back(std::async(std::launch::async, [this]{
+ std::thread([this]{
io_service_.run();
CROW_LOG_INFO << "Exiting.";
- }));
+ }).join();
}
void stop()
@@ -98,7 +136,11 @@ namespace crow
void do_accept()
{
- auto p = new Connection<Handler, Middlewares...>(pick_io_service(), handler_, server_name_, middlewares_);
+ asio::io_service& is = pick_io_service();
+ auto p = new Connection<Handler, Middlewares...>(
+ is, handler_, server_name_, middlewares_,
+ get_cached_date_str_pool_[roundrobin_index_], *timer_queue_pool_[roundrobin_index_]
+ );
acceptor_.async_accept(p->socket(),
[this, p](boost::system::error_code ec)
{
@@ -113,6 +155,8 @@ namespace crow
private:
asio::io_service io_service_;
std::vector<std::unique_ptr<asio::io_service>> io_service_pool_;
+ std::vector<detail::dumb_timer_queue*> timer_queue_pool_;
+ std::vector<std::function<std::string()>> get_cached_date_str_pool_;
tcp::acceptor acceptor_;
boost::asio::signal_set signals_;