20 #include <drizzled/pthread_globals.h>
22 #include <drizzled/errmsg_print.h>
23 #include <drizzled/session.h>
24 #include <drizzled/session/cache.h>
25 #include <drizzled/abort_exception.h>
26 #include <drizzled/transaction_services.h>
27 #include <drizzled/gettext.h>
28 #include <drizzled/plugin.h>
29 #include <drizzled/statistics_variables.h>
31 #include <boost/thread.hpp>
32 #include <boost/bind.hpp>
33 #include <boost/program_options.hpp>
35 #include "multi_thread.h"
37 namespace po= boost::program_options;
39 using namespace drizzled;
47 extern size_t my_thread_stack_size;
50 namespace multi_thread {
52 void MultiThreadScheduler::runSession(drizzled::session_id_t
id)
55 boost::this_thread::disable_interruption disable_by_default;
57 Session::shared_ptr session(session::Cache::find(
id));
64 std::cerr << _(
"Session killed before thread could execute") << endl;
67 session->pushInterrupt(&disable_by_default);
68 drizzled::internal::my_thread_init();
69 session->thread_stack= (
char*) &stack_dummy;
72 killSessionNow(session);
76 cout << _(
"Drizzle has receieved an abort event.") << endl;
77 cout << _(
"In Function: ") << *::boost::get_error_info<boost::throw_function>(ex) << endl;
78 cout << _(
"In File: ") << *::boost::get_error_info<boost::throw_file>(ex) << endl;
79 cout << _(
"On Line: ") << *::boost::get_error_info<boost::throw_line>(ex) << endl;
81 TransactionServices::sendShutdownEvent(*session.get());
85 while (not session.unique()) {}
88 void MultiThreadScheduler::setStackSize()
92 (void) pthread_attr_init(&attr);
96 int err= pthread_attr_getstacksize(&attr, &my_thread_stack_size);
97 pthread_attr_destroy(&attr);
101 errmsg_printf(error::ERROR, _(
"Unable to get thread stack size"));
102 my_thread_stack_size= 524288;
105 if (my_thread_stack_size == 0)
107 my_thread_stack_size= 524288;
119 if (my_thread_stack_size == 0)
121 my_thread_stack_size= 2 * 1024 * 1024;
126 bool MultiThreadScheduler::addSession(
const Session::shared_ptr& session)
128 if (thread_count >= max_threads)
131 thread_count.increment();
134 session->getThread().reset(
new boost::thread((boost::bind(&MultiThreadScheduler::runSession,
this, session->getSessionId()))));
136 catch (std::exception&)
138 thread_count.decrement();
142 if (not session->getThread())
144 thread_count.decrement();
148 if (not session->getThread()->joinable())
150 thread_count.decrement();
158 void MultiThreadScheduler::killSession(
Session *session)
160 thread_ptr thread(session->getThread());
168 void MultiThreadScheduler::killSessionNow(
const Session::shared_ptr& session)
170 killSession(session.get());
172 session->disconnect();
175 Session::unlink(session);
176 thread_count.decrement();
179 MultiThreadScheduler::~MultiThreadScheduler()
181 boost::mutex::scoped_lock scopedLock(drizzled::session::Cache::mutex());
184 COND_thread_count.wait(scopedLock);
201 context(
"max-threads",
202 po::value<max_threads_constraint>(&max_threads)->default_value(2048),
203 _(
"Maximum number of user threads available."));
206 DRIZZLE_DECLARE_PLUGIN
212 N_(
"Multi-thread scheduler"),
218 DRIZZLE_DECLARE_PLUGIN_END;
An Proxy Wrapper around boost::program_options::variables_map.