18
18
#include <drizzled/gettext.h>
19
19
#include <drizzled/error.h>
20
20
#include <drizzled/plugin/scheduler.h>
21
#include <drizzled/connect.h>
22
21
#include <drizzled/sql_parse.h>
23
22
#include <drizzled/session.h>
24
#include <drizzled/connect.h>
27
25
using namespace std;
26
using namespace drizzled;
28
/* Configuration variables. */
29
29
static uint32_t max_threads;
31
class Multi_thread_scheduler: public Scheduler
32
* Function to be run as a thread for each session.
34
static pthread_handler_t session_thread(void *arg);
36
class MultiThreadScheduler: public plugin::Scheduler
33
39
drizzled::atomic<uint32_t> thread_count;
34
pthread_attr_t multi_thread_attrib;
37
Multi_thread_scheduler(uint32_t threads): Scheduler(threads)
43
MultiThreadScheduler(): Scheduler()
45
struct sched_param tmp_sched_param;
47
/* Setup attribute parameter for session threads. */
48
(void) pthread_attr_init(&attr);
49
(void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
50
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
51
memset(&tmp_sched_param, 0, sizeof(tmp_sched_param));
52
tmp_sched_param.sched_priority= WAIT_PRIOR;
53
(void) pthread_attr_setschedparam(&attr, &tmp_sched_param);
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()
58
~MultiThreadScheduler()
56
60
(void) pthread_mutex_lock(&LOCK_thread_count);
57
61
while (thread_count)
59
63
pthread_cond_wait(&COND_thread_count, &LOCK_thread_count);
61
66
(void) pthread_mutex_unlock(&LOCK_thread_count);
63
pthread_attr_destroy(&multi_thread_attrib);
67
(void) pthread_attr_destroy(&attr);
66
virtual bool add_connection(Session *session)
70
virtual bool addSession(Session *session)
72
if (thread_count >= max_threads)
72
if ((error= pthread_create(&session->real_id, &multi_thread_attrib, handle_one_connection, static_cast<void*>(session))))
77
if (pthread_create(&session->real_id, &attr, session_thread,
78
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 */
87
void runSession(Session *session)
91
session->disconnect(ER_OUT_OF_RESOURCES, true);
92
statistic_increment(aborted_connects, &LOCK_status);
93
killSessionNow(session);
96
session->thread_stack= (char*) &session;
98
killSessionNow(session);
101
void killSessionNow(Session *session)
103
/* Locks LOCK_thread_count and deletes session */
104
unlink_session(session);
91
return true; // We should never reach this point
94
virtual uint32_t count(void)
108
/* We should never reach this point. */
100
class MultiThreadFactory : public SchedulerFactory
112
static pthread_handler_t session_thread(void *arg)
114
Session *session= static_cast<Session*>(arg);
115
MultiThreadScheduler *scheduler= static_cast<MultiThreadScheduler*>(session->scheduler);
116
scheduler->runSession(session);
120
class MultiThreadFactory : public plugin::SchedulerFactory
103
123
MultiThreadFactory() : SchedulerFactory("multi_thread")