1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/* Copyright (C) 2006 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "config.h"
#include <plugin/multi_thread/multi_thread.h>
#include "drizzled/pthread_globals.h"
#include <boost/program_options.hpp>
#include <drizzled/module/option_map.h>
#include <drizzled/errmsg_print.h>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
namespace po= boost::program_options;
using namespace std;
using namespace drizzled;
/* Configuration variables. */
static uint32_t max_threads;
/* Global's (TBR) */
static MultiThreadScheduler *scheduler= NULL;
namespace drizzled
{
extern size_t my_thread_stack_size;
}
void MultiThreadScheduler::runSession(drizzled::Session *session)
{
if (drizzled::internal::my_thread_init())
{
session->disconnect(drizzled::ER_OUT_OF_RESOURCES, true);
session->status_var.aborted_connects++;
killSessionNow(session);
}
boost::this_thread::at_thread_exit(&internal::my_thread_end);
session->thread_stack= (char*) &session;
session->run();
killSessionNow(session);
}
void MultiThreadScheduler::setStackSize()
{
pthread_attr_t attr;
(void) pthread_attr_init(&attr);
/* Get the thread stack size that the OS will use and make sure
that we update our global variable. */
int err= pthread_attr_getstacksize(&attr, &my_thread_stack_size);
pthread_attr_destroy(&attr);
if (err != 0)
{
errmsg_printf(ERRMSG_LVL_ERROR, _("Unable to get thread stack size\n"));
my_thread_stack_size= 524288; // At the time of the writing of this code, this was OSX's
}
if (my_thread_stack_size == 0)
{
my_thread_stack_size= 524288; // At the time of the writing of this code, this was OSX's
}
#ifdef __sun
/*
* Solaris will return zero for the stack size in a call to
* pthread_attr_getstacksize() to indicate that the OS default stack
* size is used. We need an actual value in my_thread_stack_size so that
* check_stack_overrun() will work. The Solaris man page for the
* pthread_attr_getstacksize() function says that 2M is used for 64-bit
* processes. We'll explicitly set it here to make sure that is what
* will be used.
*/
if (my_thread_stack_size == 0)
{
my_thread_stack_size= 2 * 1024 * 1024;
}
#endif
}
bool MultiThreadScheduler::addSession(Session *session)
{
if (thread_count >= max_threads)
return true;
thread_count.increment();
boost::thread new_thread(boost::bind(&MultiThreadScheduler::runSession, this, session));
if (not new_thread.joinable())
{
thread_count.decrement();
return true;
}
return false;
}
void MultiThreadScheduler::killSessionNow(Session *session)
{
/* Locks LOCK_thread_count and deletes session */
Session::unlink(session);
thread_count.decrement();
}
MultiThreadScheduler::~MultiThreadScheduler()
{
boost::mutex::scoped_lock scopedLock(LOCK_thread_count);
while (thread_count)
{
COND_thread_count.wait(scopedLock);
}
}
static int init(drizzled::module::Context &context)
{
const module::option_map &vm= context.getOptions();
if (vm.count("max-threads"))
{
if (max_threads > 4096 || max_threads < 1)
{
errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for max-threads\n"));
exit(-1);
}
}
scheduler= new MultiThreadScheduler("multi_thread");
context.add(scheduler);
return 0;
}
static DRIZZLE_SYSVAR_UINT(max_threads, max_threads,
PLUGIN_VAR_RQCMDARG,
N_("Maximum number of user threads available."),
NULL, NULL, 2048, 1, 4096, 0);
static void init_options(drizzled::module::option_context &context)
{
context("max-threads",
po::value<uint32_t>(&max_threads)->default_value(2048),
N_("Maximum number of user threads available."));
}
static drizzle_sys_var* sys_variables[]= {
DRIZZLE_SYSVAR(max_threads),
NULL
};
DRIZZLE_DECLARE_PLUGIN
{
DRIZZLE_VERSION_ID,
"multi_thread",
"0.1",
"Brian Aker",
"One Thread Per Session Scheduler",
PLUGIN_LICENSE_GPL,
init, /* Plugin Init */
sys_variables, /* system variables */
init_options /* config options */
}
DRIZZLE_DECLARE_PLUGIN_END;
|