868
by Brian Aker
Adding Multi-threaded Scheduler into the system. |
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 <drizzled/server_includes.h> |
|
942.2.5
by Brian Aker
Fix, again, the thread lock issues for unlink_session |
17 |
#include <drizzled/atomics.h> |
868
by Brian Aker
Adding Multi-threaded Scheduler into the system. |
18 |
#include <drizzled/gettext.h> |
19 |
#include <drizzled/error.h> |
|
960.2.22
by Monty Taylor
Renamed a bunch of plugin files. |
20 |
#include <drizzled/plugin/scheduler.h> |
868
by Brian Aker
Adding Multi-threaded Scheduler into the system. |
21 |
#include <drizzled/connect.h> |
22 |
#include <drizzled/sql_parse.h> |
|
23 |
#include <drizzled/session.h> |
|
24 |
#include <drizzled/connect.h> |
|
25 |
#include <string> |
|
960.1.2
by Monty Taylor
Moved pthread_attrib into multi_thread class. |
26 |
|
868
by Brian Aker
Adding Multi-threaded Scheduler into the system. |
27 |
using namespace std; |
28 |
||
29 |
static uint32_t max_threads; |
|
30 |
||
960.1.1
by Monty Taylor
First pass at scheduler plugin. |
31 |
class Multi_thread_scheduler: public Scheduler |
32 |
{
|
|
968.2.3
by Monty Taylor
Replacd use of tbb:: namespace with drizzled:: |
33 |
drizzled::atomic<uint32_t> thread_count; |
960.1.2
by Monty Taylor
Moved pthread_attrib into multi_thread class. |
34 |
pthread_attr_t multi_thread_attrib; |
960.1.1
by Monty Taylor
First pass at scheduler plugin. |
35 |
|
36 |
public: |
|
960.1.2
by Monty Taylor
Moved pthread_attrib into multi_thread class. |
37 |
Multi_thread_scheduler(uint32_t threads): Scheduler(threads) |
38 |
{
|
|
960.1.3
by Monty Taylor
Initialize thread_count to zero. |
39 |
thread_count= 0; |
960.1.2
by Monty Taylor
Moved pthread_attrib into multi_thread class. |
40 |
/* Parameter for threads created for connections */
|
41 |
(void) pthread_attr_init(&multi_thread_attrib); |
|
42 |
(void) pthread_attr_setdetachstate(&multi_thread_attrib, |
|
43 |
PTHREAD_CREATE_DETACHED); |
|
44 |
pthread_attr_setscope(&multi_thread_attrib, PTHREAD_SCOPE_SYSTEM); |
|
45 |
{
|
|
46 |
struct sched_param tmp_sched_param; |
|
47 |
||
48 |
memset(&tmp_sched_param, 0, sizeof(tmp_sched_param)); |
|
49 |
tmp_sched_param.sched_priority= WAIT_PRIOR; |
|
50 |
(void)pthread_attr_setschedparam(&multi_thread_attrib, &tmp_sched_param); |
|
51 |
}
|
|
52 |
}
|
|
53 |
||
54 |
~Multi_thread_scheduler() |
|
55 |
{
|
|
56 |
(void) pthread_mutex_lock(&LOCK_thread_count); |
|
57 |
while (thread_count) |
|
58 |
{
|
|
59 |
pthread_cond_wait(&COND_thread_count, &LOCK_thread_count); |
|
60 |
}
|
|
61 |
(void) pthread_mutex_unlock(&LOCK_thread_count); |
|
62 |
||
63 |
pthread_attr_destroy(&multi_thread_attrib); |
|
64 |
}
|
|
960.1.1
by Monty Taylor
First pass at scheduler plugin. |
65 |
|
66 |
virtual bool add_connection(Session *session) |
|
67 |
{
|
|
68 |
int error; |
|
69 |
||
70 |
thread_count++; |
|
71 |
||
72 |
if ((error= pthread_create(&session->real_id, &multi_thread_attrib, handle_one_connection, static_cast<void*>(session)))) |
|
73 |
return true; |
|
74 |
||
75 |
return false; |
|
76 |
}
|
|
77 |
||
78 |
||
79 |
/*
|
|
80 |
End connection, in case when we are using 'no-threads'
|
|
81 |
*/
|
|
82 |
||
83 |
virtual bool end_thread(Session *session, bool) |
|
84 |
{
|
|
85 |
unlink_session(session); /* locks LOCK_thread_count and deletes session */ |
|
86 |
thread_count--; |
|
87 |
||
88 |
my_thread_end(); |
|
89 |
pthread_exit(0); |
|
90 |
||
91 |
return true; // We should never reach this point |
|
92 |
}
|
|
93 |
||
94 |
virtual uint32_t count(void) |
|
95 |
{
|
|
96 |
return thread_count; |
|
97 |
}
|
|
98 |
};
|
|
929.1.1
by Brian Aker
Push thread count out to the scheduler. |
99 |
|
971.1.46
by Monty Taylor
Made plugin registration go through Plugin_registry. |
100 |
class MultiThreadFactory : public SchedulerFactory |
101 |
{
|
|
102 |
public: |
|
994.2.2
by Monty Taylor
Store a Registry of SchedulerFactories and set one of them at startup for better error messages earlier. |
103 |
MultiThreadFactory() : SchedulerFactory("multi_thread") |
104 |
{
|
|
105 |
addAlias("multi-thread"); |
|
106 |
}
|
|
107 |
||
971.1.46
by Monty Taylor
Made plugin registration go through Plugin_registry. |
108 |
~MultiThreadFactory() { if (scheduler != NULL) delete scheduler; } |
109 |
Scheduler *operator() () |
|
110 |
{
|
|
111 |
if (scheduler == NULL) |
|
112 |
scheduler= new Multi_thread_scheduler(max_threads); |
|
113 |
return scheduler; |
|
114 |
}
|
|
115 |
};
|
|
971.1.51
by Monty Taylor
New-style plugin registration now works. |
116 |
static MultiThreadFactory *factory= NULL; |
971.1.46
by Monty Taylor
Made plugin registration go through Plugin_registry. |
117 |
|
971.1.52
by Monty Taylor
Did the finalizers. Renamed plugin_registry. |
118 |
static int init(PluginRegistry ®istry) |
868
by Brian Aker
Adding Multi-threaded Scheduler into the system. |
119 |
{
|
971.1.51
by Monty Taylor
New-style plugin registration now works. |
120 |
factory= new MultiThreadFactory(); |
971.1.52
by Monty Taylor
Did the finalizers. Renamed plugin_registry. |
121 |
registry.add(factory); |
868
by Brian Aker
Adding Multi-threaded Scheduler into the system. |
122 |
return 0; |
123 |
}
|
|
124 |
||
971.1.52
by Monty Taylor
Did the finalizers. Renamed plugin_registry. |
125 |
static int deinit(PluginRegistry ®istry) |
868
by Brian Aker
Adding Multi-threaded Scheduler into the system. |
126 |
{
|
971.1.51
by Monty Taylor
New-style plugin registration now works. |
127 |
if (factory) |
971.1.52
by Monty Taylor
Did the finalizers. Renamed plugin_registry. |
128 |
{
|
129 |
registry.remove(factory); |
|
971.1.51
by Monty Taylor
New-style plugin registration now works. |
130 |
delete factory; |
971.1.52
by Monty Taylor
Did the finalizers. Renamed plugin_registry. |
131 |
}
|
868
by Brian Aker
Adding Multi-threaded Scheduler into the system. |
132 |
return 0; |
133 |
}
|
|
134 |
||
135 |
static DRIZZLE_SYSVAR_UINT(max_threads, max_threads, |
|
136 |
PLUGIN_VAR_RQCMDARG, |
|
137 |
N_("Maximum number of user threads available."), |
|
138 |
NULL, NULL, 2048, 1, 4048, 0); |
|
139 |
||
140 |
static struct st_mysql_sys_var* system_variables[]= { |
|
141 |
DRIZZLE_SYSVAR(max_threads), |
|
142 |
NULL
|
|
143 |
};
|
|
144 |
||
145 |
drizzle_declare_plugin(multi_thread) |
|
146 |
{
|
|
147 |
"multi_thread", |
|
148 |
"0.1", |
|
149 |
"Brian Aker", |
|
960.1.1
by Monty Taylor
First pass at scheduler plugin. |
150 |
"One Thread Per Session Scheduler", |
868
by Brian Aker
Adding Multi-threaded Scheduler into the system. |
151 |
PLUGIN_LICENSE_GPL, |
152 |
init, /* Plugin Init */ |
|
153 |
deinit, /* Plugin Deinit */ |
|
154 |
NULL, /* status variables */ |
|
155 |
system_variables, /* system variables */ |
|
156 |
NULL /* config options */ |
|
157 |
}
|
|
158 |
drizzle_declare_plugin_end; |