21#include <drizzled/pthread_globals.h>
23#include <drizzled/errmsg_print.h>
24#include <drizzled/session.h>
25#include <drizzled/session/cache.h>
26#include <drizzled/abort_exception.h>
27#include <drizzled/transaction_services.h>
28#include <drizzled/gettext.h>
29#include <drizzled/plugin.h>
30#include <drizzled/statistics_variables.h>
32#include <boost/thread.hpp>
33#include <boost/bind/bind.hpp>
34#include <boost/program_options.hpp>
36#include "multi_thread.h"
38namespace po= boost::program_options;
44static max_threads_constraint max_threads;
48 extern size_t my_thread_stack_size;
51namespace multi_thread {
53void MultiThreadScheduler::runSession(drizzled::session_id_t
id)
56 boost::this_thread::disable_interruption disable_by_default;
58 Session::shared_ptr session(session::Cache::find(
id));
65 std::cerr << _(
"Session killed before thread could execute") << endl;
68 session->pushInterrupt(&disable_by_default);
69 drizzled::internal::my_thread_init();
70 session->thread_stack= (
char*) &stack_dummy;
75 catch (abort_exception& ex)
77 cout << _(
"Drizzle has receieved an abort event.") << endl;
78 cout << _(
"In Function: ") << *::boost::get_error_info<boost::throw_function>(ex) << endl;
79 cout << _(
"In File: ") << *::boost::get_error_info<boost::throw_file>(ex) << endl;
80 cout << _(
"On Line: ") << *::boost::get_error_info<boost::throw_line>(ex) << endl;
86 while (not session.unique()) {}
89void MultiThreadScheduler::setStackSize()
93 (void) pthread_attr_init(&attr);
97 int err= pthread_attr_getstacksize(&attr, &my_thread_stack_size);
98 pthread_attr_destroy(&attr);
100 if ((err != 0) || (my_thread_stack_size == 0))
102 errmsg_printf(error::ERROR, _(
"Unable to get thread stack size"));
109 if (thread_count >= max_threads)
112 thread_count.increment();
115 session->getThread().reset(
new boost::thread((boost::bind(&MultiThreadScheduler::runSession,
this, session->getSessionId()))));
117 catch (std::exception&)
119 thread_count.decrement();
123 if (not session->getThread())
125 thread_count.decrement();
129 if (not session->getThread()->joinable())
131 thread_count.decrement();
141 thread_ptr thread(session->getThread());
153 session->disconnect();
156 Session::unlink(session);
157 thread_count.decrement();
160MultiThreadScheduler::~MultiThreadScheduler()
162 boost::mutex::scoped_lock scopedLock(drizzled::session::Cache::mutex());
165 COND_thread_count.wait(scopedLock);
182 context(
"max-threads",
183 po::value<max_threads_constraint>(&max_threads)->default_value(2048),
184 _(
"Maximum number of user threads available."));
187DRIZZLE_DECLARE_PLUGIN
193 N_(
"Multi-thread scheduler"),
199DRIZZLE_DECLARE_PLUGIN_END;
static bool sendShutdownEvent(Session &)
void killSession(drizzled::Session *)
bool addSession(const drizzled::Session::shared_ptr &)
void killSessionNow(const drizzled::Session::shared_ptr &)
TODO: Rename this file - func.h is stupid.
An Proxy Wrapper around boost::program_options::variables_map.