19
19
#include <boost/program_options.hpp>
20
20
#include <drizzled/module/option_map.h>
21
21
#include <drizzled/errmsg_print.h>
22
#include "drizzled/session.h"
23
#include "drizzled/session/cache.h"
25
23
#include <boost/thread.hpp>
26
24
#include <boost/bind.hpp>
30
28
using namespace drizzled;
32
30
/* Configuration variables. */
33
typedef constrained_check<uint32_t, 4096, 1> max_threads_constraint;
34
static max_threads_constraint max_threads;
31
static uint32_t max_threads;
34
static MultiThreadScheduler *scheduler= NULL;
38
38
extern size_t my_thread_stack_size;
41
namespace multi_thread {
43
void MultiThreadScheduler::runSession(drizzled::session_id_t id)
41
void MultiThreadScheduler::runSession(drizzled::Session *session)
46
boost::this_thread::disable_interruption disable_by_default;
47
Session::shared_ptr session(session::Cache::singleton().find(id));
51
std::cerr << "Session killed before thread could execute\n";
54
session->pushInterrupt(&disable_by_default);
56
43
if (drizzled::internal::my_thread_init())
58
45
session->disconnect(drizzled::ER_OUT_OF_RESOURCES, true);
62
49
boost::this_thread::at_thread_exit(&internal::my_thread_end);
64
session->thread_stack= (char*) &stack_dummy;
51
session->thread_stack= (char*) &session;
66
53
killSessionNow(session);
67
// @todo remove hard spin by disconnection the session first from the
69
while (not session.unique()) {}
72
56
void MultiThreadScheduler::setStackSize()
110
bool MultiThreadScheduler::addSession(Session::shared_ptr &session)
94
bool MultiThreadScheduler::addSession(Session *session)
112
96
if (thread_count >= max_threads)
115
99
thread_count.increment();
117
session->getThread().reset(new boost::thread((boost::bind(&MultiThreadScheduler::runSession, this, session->getSessionId()))));
119
if (not session->getThread())
121
thread_count.decrement();
125
if (not session->getThread()->joinable())
101
boost::thread new_thread(boost::bind(&MultiThreadScheduler::runSession, this, session));
103
if (not new_thread.joinable())
127
105
thread_count.decrement();
135
void MultiThreadScheduler::killSession(Session *session)
137
boost_thread_shared_ptr thread(session->getThread());
145
void MultiThreadScheduler::killSessionNow(Session::shared_ptr &session)
147
killSession(session.get());
113
void MultiThreadScheduler::killSessionNow(Session *session)
148
115
/* Locks LOCK_thread_count and deletes session */
149
116
Session::unlink(session);
150
117
thread_count.decrement();
153
120
MultiThreadScheduler::~MultiThreadScheduler()
155
boost::mutex::scoped_lock scopedLock(drizzled::session::Cache::singleton().mutex());
122
boost::mutex::scoped_lock scopedLock(LOCK_thread_count);
156
123
while (thread_count)
158
125
COND_thread_count.wait(scopedLock);
162
} // multi_thread namespace
165
130
static int init(drizzled::module::Context &context)
168
context.add(new multi_thread::MultiThreadScheduler("multi_thread"));
133
const module::option_map &vm= context.getOptions();
134
if (vm.count("max-threads"))
136
if (max_threads > 4096 || max_threads < 1)
138
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for max-threads\n"));
143
scheduler= new MultiThreadScheduler("multi_thread");
144
context.add(scheduler);
149
static DRIZZLE_SYSVAR_UINT(max_threads, max_threads,
151
N_("Maximum number of user threads available."),
152
NULL, NULL, 2048, 1, 4096, 0);
173
154
static void init_options(drizzled::module::option_context &context)
175
156
context("max-threads",
176
po::value<max_threads_constraint>(&max_threads)->default_value(2048),
157
po::value<uint32_t>(&max_threads)->default_value(2048),
177
158
N_("Maximum number of user threads available."));
161
static drizzle_sys_var* sys_variables[]= {
162
DRIZZLE_SYSVAR(max_threads),
180
166
DRIZZLE_DECLARE_PLUGIN
182
168
DRIZZLE_VERSION_ID,
186
172
"One Thread Per Session Scheduler",
187
173
PLUGIN_LICENSE_GPL,
188
174
init, /* Plugin Init */
189
NULL, /* system variables */
175
sys_variables, /* system variables */
190
176
init_options /* config options */
192
178
DRIZZLE_DECLARE_PLUGIN_END;