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/sql_parse.h>
22
#include <drizzled/session.h>
16
#include <plugin/multi_thread/multi_thread.h>
25
18
using namespace std;
26
19
using namespace drizzled;
36
32
extern "C" pthread_handler_t session_thread(void *arg);
39
class MultiThreadScheduler: public plugin::Scheduler
42
drizzled::atomic<uint32_t> thread_count;
46
MultiThreadScheduler(): Scheduler()
48
struct sched_param tmp_sched_param;
50
memset(&tmp_sched_param, 0, sizeof(struct sched_param));
52
/* Setup attribute parameter for session threads. */
53
(void) pthread_attr_init(&attr);
54
(void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
55
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
57
tmp_sched_param.sched_priority= WAIT_PRIOR;
58
(void) pthread_attr_setschedparam(&attr, &tmp_sched_param);
63
~MultiThreadScheduler()
65
(void) pthread_mutex_lock(&LOCK_thread_count);
68
pthread_cond_wait(&COND_thread_count, &LOCK_thread_count);
71
(void) pthread_mutex_unlock(&LOCK_thread_count);
72
(void) pthread_attr_destroy(&attr);
75
virtual bool addSession(Session *session)
77
if (thread_count >= max_threads)
82
if (pthread_create(&session->real_id, &attr, session_thread,
83
static_cast<void*>(session)))
92
void runSession(Session *session)
96
session->disconnect(ER_OUT_OF_RESOURCES, true);
97
statistic_increment(aborted_connects, &LOCK_status);
98
killSessionNow(session);
101
session->thread_stack= (char*) &session;
103
killSessionNow(session);
106
void killSessionNow(Session *session)
108
/* Locks LOCK_thread_count and deletes session */
109
unlink_session(session);
113
/* We should never reach this point. */
119
37
extern "C" pthread_handler_t session_thread(void *arg)
121
39
Session *session= static_cast<Session*>(arg);
122
MultiThreadScheduler *scheduler= static_cast<MultiThreadScheduler*>(session->scheduler);
123
scheduler->runSession(session);
40
MultiThreadScheduler *sched= static_cast<MultiThreadScheduler*>(session->scheduler);
41
sched->runSession(session);
128
class MultiThreadFactory : public plugin::SchedulerFactory
131
MultiThreadFactory() : SchedulerFactory("multi_thread")
133
addAlias("multi-thread");
136
~MultiThreadFactory()
138
if (scheduler != NULL)
142
plugin::Scheduler *operator() ()
144
if (scheduler == NULL)
145
scheduler= new MultiThreadScheduler();
150
static MultiThreadFactory *factory= NULL;
47
bool MultiThreadScheduler::addSession(Session *session)
49
if (thread_count >= max_threads)
54
if (pthread_create(&session->real_id, &attr, session_thread,
55
static_cast<void*>(session)))
65
void MultiThreadScheduler::killSessionNow(Session *session)
67
/* Locks LOCK_thread_count and deletes session */
68
unlink_session(session);
72
/* We should never reach this point. */
75
MultiThreadScheduler::~MultiThreadScheduler()
77
(void) pthread_mutex_lock(&LOCK_thread_count);
80
pthread_cond_wait(&COND_thread_count, &LOCK_thread_count);
83
(void) pthread_mutex_unlock(&LOCK_thread_count);
84
(void) pthread_attr_destroy(&attr);
152
88
static int init(drizzled::plugin::Registry ®istry)
154
factory= new MultiThreadFactory();
155
registry.add(factory);
90
scheduler= new MultiThreadScheduler("multi_thread");
91
registry.add(scheduler);
159
96
static int deinit(drizzled::plugin::Registry ®istry)
163
registry.remove(factory);
98
registry.remove(scheduler);