1
/* Copyright (C) 2006 MySQL AB
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.
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.
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 */
16
#include <drizzled/server_includes.h>
17
#include <drizzled/atomics.h>
18
#include <drizzled/gettext.h>
19
#include <drizzled/error.h>
20
#include <drizzled/plugin/scheduler.h>
21
#include <drizzled/connect.h>
22
#include <drizzled/sql_parse.h>
23
#include <drizzled/session.h>
24
#include <drizzled/connect.h>
29
static uint32_t max_threads;
31
class Multi_thread_scheduler: public Scheduler
33
tbb::atomic<uint32_t> thread_count;
34
pthread_attr_t multi_thread_attrib;
37
Multi_thread_scheduler(uint32_t threads): Scheduler(threads)
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);
46
struct sched_param tmp_sched_param;
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);
54
~Multi_thread_scheduler()
56
(void) pthread_mutex_lock(&LOCK_thread_count);
59
pthread_cond_wait(&COND_thread_count, &LOCK_thread_count);
61
(void) pthread_mutex_unlock(&LOCK_thread_count);
63
pthread_attr_destroy(&multi_thread_attrib);
66
virtual bool add_connection(Session *session)
72
if ((error= pthread_create(&session->real_id, &multi_thread_attrib, handle_one_connection, static_cast<void*>(session))))
80
End connection, in case when we are using 'no-threads'
83
virtual bool end_thread(Session *session, bool)
85
unlink_session(session); /* locks LOCK_thread_count and deletes session */
91
return true; // We should never reach this point
94
virtual uint32_t count(void)
100
static int init(void *p)
102
Multi_thread_scheduler** sched= static_cast<Multi_thread_scheduler **>(p);
104
*sched= new Multi_thread_scheduler(max_threads);
109
static int deinit(void *p)
112
Multi_thread_scheduler *sched= static_cast<Multi_thread_scheduler *>(p);
118
static DRIZZLE_SYSVAR_UINT(max_threads, max_threads,
120
N_("Maximum number of user threads available."),
121
NULL, NULL, 2048, 1, 4048, 0);
123
static struct st_mysql_sys_var* system_variables[]= {
124
DRIZZLE_SYSVAR(max_threads),
128
drizzle_declare_plugin(multi_thread)
130
DRIZZLE_SCHEDULING_PLUGIN,
134
"One Thread Per Session Scheduler",
136
init, /* Plugin Init */
137
deinit, /* Plugin Deinit */
138
NULL, /* status variables */
139
system_variables, /* system variables */
140
NULL /* config options */
142
drizzle_declare_plugin_end;