0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ReactorFactory.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #include <Common/Compat.h>
23 
24 #include "HandlerMap.h"
25 #include "ReactorFactory.h"
26 #include "ReactorRunner.h"
27 
28 #include <Common/Config.h>
29 #include <Common/SystemInfo.h>
30 
31 #include <cassert>
32 
33 extern "C" {
34 #include <signal.h>
35 }
36 
37 using namespace Hypertable;
38 using namespace std;
39 
40 std::vector<ReactorPtr> ReactorFactory::ms_reactors;
41 boost::thread_group ReactorFactory::ms_threads;
42 default_random_engine ReactorFactory::rng {1};
44 atomic<int> ReactorFactory::ms_next_reactor(0);
45 bool ReactorFactory::ms_epollet = true;
46 bool ReactorFactory::use_poll = false;
47 bool ReactorFactory::proxy_master = false;
49 
50 void ReactorFactory::initialize(uint16_t reactor_count) {
51  lock_guard<mutex> lock(ms_mutex);
52  if (!ms_reactors.empty())
53  return;
54  ReactorPtr reactor;
55  ReactorRunner rrunner;
56  ReactorRunner::handler_map = make_shared<HandlerMap>();
57  signal(SIGPIPE, SIG_IGN);
58  assert(reactor_count > 0);
59 
60 #if defined(__linux__)
61  if (System::os_info().version_major < 2 ||
66  ms_epollet = false;
67  if (System::os_info().version_major < 2 ||
69  use_poll = true;
70 #endif
71 
72  if (Config::properties->has("Comm.UsePoll") &&
73  Config::properties->get_bool("Comm.UsePoll"))
74  use_poll = true;
75 
76  ms_reactors.reserve(reactor_count+2);
77  for (uint16_t i=0; i<reactor_count+2; i++) {
78  reactor = make_shared<Reactor>();
79  ms_reactors.push_back(reactor);
80  rrunner.set_reactor(reactor);
81  ms_threads.create_thread(rrunner);
82  }
83 }
84 
87  for (size_t i=0; i<ms_reactors.size(); i++)
88  ms_reactors[i]->poll_loop_interrupt();
89  ms_threads.join_all();
90  ms_reactors.clear();
92 }
93 
95  ms_threads.join_all();
96 }
static std::mutex mutex
Definition: Logger.cc:43
static bool verbose
Verbose mode.
const int version_micro
Definition: Version.cc:33
PropertiesPtr properties
This singleton map stores all options.
Definition: Config.cc:47
static void initialize(uint16_t reactor_count)
Initializes I/O reactors.
Declarations for ReactorRunner.
static bool ms_epollet
Use "edge triggered" epoll.
static std::vector< ReactorPtr > ms_reactors
Vector of reactors (last position is timer reactor)
static std::default_random_engine rng
Pseudo random number generator.
STL namespace.
Thread functor class for reacting to I/O events.
Definition: ReactorRunner.h:51
bool has(const String &name)
Check existence of a configuration value.
Definition: Config.h:57
static std::atomic< int > ms_next_reactor
Atomic integer used for round-robin assignment of reactors.
static boost::thread_group ms_threads
Boost thread_group for managing reactor threads.
static HandlerMapPtr handler_map
Smart pointer to HandlerMap.
Definition: ReactorRunner.h:70
static void destroy()
This method shuts down the reactors.
static void join()
Joins with reactor threads.
static const OsInfo & os_info()
Retrieves updated Operating system information (see SystemInfo.h)
Definition: SystemInfo.cc:347
Compatibility Macros for C/C++.
std::shared_ptr< Reactor > ReactorPtr
Shared smart pointer to Reactor.
Definition: Reactor.h:295
Declarations for HandlerMap.
const int version_minor
Definition: Version.cc:32
Hypertable definitions
static bool shutdown
Flag indicating that reactor thread is being shut down.
Definition: ReactorRunner.h:63
const int version_major
Definition: Version.cc:31
Declarations for ReactorFactory.
Configuration settings.
System information and statistics based on libsigar.
static std::mutex ms_mutex
Mutex to serialize calls to initialize.
void set_reactor(ReactorPtr &reactor)
Assocates reactor state object with this ReactorRunner.
Definition: ReactorRunner.h:60
static bool proxy_master
Set to true if this process is acting as "Proxy Master".