drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
multi_thread.cc
1/* Copyright (C) 2006 MySQL AB
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16#include <config.h>
17
18#include <cstdlib>
19#include <iostream>
20
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>
31
32#include <boost/thread.hpp>
33#include <boost/bind/bind.hpp>
34#include <boost/program_options.hpp>
35
36#include "multi_thread.h"
37
38namespace po= boost::program_options;
39using namespace std;
40using namespace drizzled;
41
42/* Configuration variables. */
43typedef constrained_check<uint32_t, 4096, 1> max_threads_constraint;
44static max_threads_constraint max_threads;
45
46namespace drizzled
47{
48 extern size_t my_thread_stack_size;
49}
50
51namespace multi_thread {
52
53void MultiThreadScheduler::runSession(drizzled::session_id_t id)
54{
55 char stack_dummy;
56 boost::this_thread::disable_interruption disable_by_default;
57
58 Session::shared_ptr session(session::Cache::find(id));
59
60 try
61 {
62
63 if (not session)
64 {
65 std::cerr << _("Session killed before thread could execute") << endl;
66 return;
67 }
68 session->pushInterrupt(&disable_by_default);
69 drizzled::internal::my_thread_init();
70 session->thread_stack= (char*) &stack_dummy;
71 session->run();
72
73 killSessionNow(session);
74 }
75 catch (abort_exception& ex)
76 {
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;
81
83 }
84 // @todo remove hard spin by disconnection the session first from the
85 // thread.
86 while (not session.unique()) {}
87}
88
89void MultiThreadScheduler::setStackSize()
90{
91 pthread_attr_t attr;
92
93 (void) pthread_attr_init(&attr);
94
95 /* Get the thread stack size that the OS will use and make sure
96 that we update our global variable. */
97 int err= pthread_attr_getstacksize(&attr, &my_thread_stack_size);
98 pthread_attr_destroy(&attr);
99
100 if ((err != 0) || (my_thread_stack_size == 0))
101 {
102 errmsg_printf(error::ERROR, _("Unable to get thread stack size"));
103 abort();
104 }
105}
106
107bool MultiThreadScheduler::addSession(const Session::shared_ptr& session)
108{
109 if (thread_count >= max_threads)
110 return true;
111
112 thread_count.increment();
113 try
114 {
115 session->getThread().reset(new boost::thread((boost::bind(&MultiThreadScheduler::runSession, this, session->getSessionId()))));
116 }
117 catch (std::exception&)
118 {
119 thread_count.decrement();
120 return true;
121 }
122
123 if (not session->getThread())
124 {
125 thread_count.decrement();
126 return true;
127 }
128
129 if (not session->getThread()->joinable())
130 {
131 thread_count.decrement();
132 return true;
133 }
134
135 return false;
136}
137
138
140{
141 thread_ptr thread(session->getThread());
142
143 if (thread)
144 {
145 thread->interrupt();
146 }
147}
148
149void MultiThreadScheduler::killSessionNow(const Session::shared_ptr& session)
150{
151 killSession(session.get());
152
153 session->disconnect();
154
155 /* Locks LOCK_thread_count and deletes session */
156 Session::unlink(session);
157 thread_count.decrement();
158}
159
160MultiThreadScheduler::~MultiThreadScheduler()
161{
162 boost::mutex::scoped_lock scopedLock(drizzled::session::Cache::mutex());
163 while (thread_count)
164 {
165 COND_thread_count.wait(scopedLock);
166 }
167}
168
169} // multi_thread namespace
170
171
172static int init(drizzled::module::Context &context)
173{
174
175 context.add(new multi_thread::MultiThreadScheduler("multi_thread"));
176
177 return 0;
178}
179
180static void init_options(drizzled::module::option_context &context)
181{
182 context("max-threads",
183 po::value<max_threads_constraint>(&max_threads)->default_value(2048),
184 _("Maximum number of user threads available."));
185}
186
187DRIZZLE_DECLARE_PLUGIN
188{
189 DRIZZLE_VERSION_ID,
190 "multi_thread",
191 "0.1",
192 "Brian Aker",
193 N_("Multi-thread scheduler"),
194 PLUGIN_LICENSE_GPL,
195 init,
196 NULL,
197 init_options
198}
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.
STL namespace.
An Proxy Wrapper around boost::program_options::variables_map.