13
13
along with this program; if not, write to the Free Software
14
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>
17
#include <plugin/multi_thread/multi_thread.h>
18
#include "drizzled/pthread_globals.h"
27
20
using namespace std;
21
using namespace drizzled;
23
/* Configuration variables. */
29
24
static uint32_t max_threads;
31
class Multi_thread_scheduler: public Scheduler
33
drizzled::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
class MultiThreadFactory : public SchedulerFactory
103
MultiThreadFactory() : SchedulerFactory("multi_thread") {}
104
~MultiThreadFactory() { if (scheduler != NULL) delete scheduler; }
105
Scheduler *operator() ()
107
if (scheduler == NULL)
108
scheduler= new Multi_thread_scheduler(max_threads);
112
static MultiThreadFactory *factory= NULL;
114
static int init(PluginRegistry ®istry)
116
factory= new MultiThreadFactory();
117
registry.add(factory);
121
static int deinit(PluginRegistry ®istry)
125
registry.remove(factory);
27
static MultiThreadScheduler *scheduler= NULL;
30
* Function to be run as a thread for each session.
34
extern "C" pthread_handler_t session_thread(void *arg);
39
extern "C" pthread_handler_t session_thread(void *arg)
41
Session *session= static_cast<Session*>(arg);
42
MultiThreadScheduler *sched= static_cast<MultiThreadScheduler*>(session->scheduler);
43
sched->runSession(session);
49
bool MultiThreadScheduler::addSession(Session *session)
51
if (thread_count >= max_threads)
54
thread_count.increment();
56
if (pthread_create(&session->real_id, &attr, session_thread,
57
static_cast<void*>(session)))
59
thread_count.decrement();
67
void MultiThreadScheduler::killSessionNow(Session *session)
69
/* Locks LOCK_thread_count and deletes session */
70
Session::unlink(session);
71
thread_count.decrement();
72
internal::my_thread_end();
74
/* We should never reach this point. */
77
MultiThreadScheduler::~MultiThreadScheduler()
79
(void) pthread_mutex_lock(&LOCK_thread_count);
82
pthread_cond_wait(&COND_thread_count, &LOCK_thread_count);
85
(void) pthread_mutex_unlock(&LOCK_thread_count);
86
(void) pthread_attr_destroy(&attr);
90
static int init(drizzled::plugin::Context &context)
92
scheduler= new MultiThreadScheduler("multi_thread");
93
context.add(scheduler);
131
98
static DRIZZLE_SYSVAR_UINT(max_threads, max_threads,
132
99
PLUGIN_VAR_RQCMDARG,
133
100
N_("Maximum number of user threads available."),
134
NULL, NULL, 2048, 1, 4048, 0);
101
NULL, NULL, 2048, 1, 4096, 0);
136
static struct st_mysql_sys_var* system_variables[]= {
103
static drizzle_sys_var* sys_variables[]= {
137
104
DRIZZLE_SYSVAR(max_threads),
141
drizzle_declare_plugin(multi_thread)
108
DRIZZLE_DECLARE_PLUGIN
146
114
"One Thread Per Session Scheduler",
147
115
PLUGIN_LICENSE_GPL,
148
116
init, /* Plugin Init */
149
deinit, /* Plugin Deinit */
150
NULL, /* status variables */
151
system_variables, /* system variables */
117
sys_variables, /* system variables */
152
118
NULL /* config options */
154
drizzle_declare_plugin_end;
120
DRIZZLE_DECLARE_PLUGIN_END;