~drizzle-trunk/drizzle/development

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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
16
#include "config.h"
1152.1.5 by Brian Aker
Remove Factory/make scheduler work like everything else.
17
#include <plugin/multi_thread/multi_thread.h>
1241.9.31 by Monty Taylor
Moved global pthread variables into their own header.
18
#include "drizzled/pthread_globals.h"
960.1.2 by Monty Taylor
Moved pthread_attrib into multi_thread class.
19
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
20
using namespace std;
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
21
using namespace drizzled;
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
22
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
23
/* Configuration variables. */
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
24
static uint32_t max_threads;
25
1152.1.5 by Brian Aker
Remove Factory/make scheduler work like everything else.
26
/* Global's (TBR) */
27
static MultiThreadScheduler *scheduler= NULL;
28
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
29
/**
30
 * Function to be run as a thread for each session.
31
 */
971.3.67 by Eric Day
Switched static funtion to anonymous namespace + extern C for Sun CC.
32
namespace
33
{
34
  extern "C" pthread_handler_t session_thread(void *arg);
35
}
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
36
971.3.67 by Eric Day
Switched static funtion to anonymous namespace + extern C for Sun CC.
37
namespace
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
38
{
971.3.67 by Eric Day
Switched static funtion to anonymous namespace + extern C for Sun CC.
39
  extern "C" pthread_handler_t session_thread(void *arg)
40
  {
41
    Session *session= static_cast<Session*>(arg);
1152.1.7 by Brian Aker
Fix for shadow issues.
42
    MultiThreadScheduler *sched= static_cast<MultiThreadScheduler*>(session->scheduler);
43
    sched->runSession(session);
971.3.67 by Eric Day
Switched static funtion to anonymous namespace + extern C for Sun CC.
44
    return NULL;
45
  }
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
46
}
47
1152.1.5 by Brian Aker
Remove Factory/make scheduler work like everything else.
48
49
bool MultiThreadScheduler::addSession(Session *session)
50
{
51
  if (thread_count >= max_threads)
52
    return true;
53
1260.1.2 by Monty Taylor
Replaced operator overloads with methods since we can't actually usefully follow the proper semantics for the operators.
54
  thread_count.increment();
1152.1.5 by Brian Aker
Remove Factory/make scheduler work like everything else.
55
56
  if (pthread_create(&session->real_id, &attr, session_thread,
57
                     static_cast<void*>(session)))
58
  {
1260.1.2 by Monty Taylor
Replaced operator overloads with methods since we can't actually usefully follow the proper semantics for the operators.
59
    thread_count.decrement();
1152.1.5 by Brian Aker
Remove Factory/make scheduler work like everything else.
60
    return true;
61
  }
62
63
  return false;
64
}
65
66
67
void MultiThreadScheduler::killSessionNow(Session *session)
68
{
69
  /* Locks LOCK_thread_count and deletes session */
1241.9.12 by Monty Taylor
Trims more out of server_includes.h.
70
  Session::unlink(session);
1260.1.2 by Monty Taylor
Replaced operator overloads with methods since we can't actually usefully follow the proper semantics for the operators.
71
  thread_count.decrement();
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
72
  internal::my_thread_end();
1152.1.5 by Brian Aker
Remove Factory/make scheduler work like everything else.
73
  pthread_exit(0);
74
  /* We should never reach this point. */
75
}
76
77
MultiThreadScheduler::~MultiThreadScheduler()
78
{
79
  (void) pthread_mutex_lock(&LOCK_thread_count);
80
  while (thread_count)
81
  {
82
    pthread_cond_wait(&COND_thread_count, &LOCK_thread_count);
83
  }
84
85
  (void) pthread_mutex_unlock(&LOCK_thread_count);
86
  (void) pthread_attr_destroy(&attr);
87
}
88
89
  
1110.1.5 by Monty Taylor
Renamed PluginRegistry to plugin::Registry.
90
static int init(drizzled::plugin::Registry &registry)
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
91
{
1152.1.5 by Brian Aker
Remove Factory/make scheduler work like everything else.
92
  scheduler= new MultiThreadScheduler("multi_thread");
93
  registry.add(scheduler);
94
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
95
  return 0;
96
}
97
1110.1.5 by Monty Taylor
Renamed PluginRegistry to plugin::Registry.
98
static int deinit(drizzled::plugin::Registry &registry)
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
99
{
1152.1.5 by Brian Aker
Remove Factory/make scheduler work like everything else.
100
  registry.remove(scheduler);
101
  delete scheduler;
102
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
103
  return 0;
104
}
105
106
static DRIZZLE_SYSVAR_UINT(max_threads, max_threads,
107
                           PLUGIN_VAR_RQCMDARG,
108
                           N_("Maximum number of user threads available."),
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
109
                           NULL, NULL, 2048, 1, 4096, 0);
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
110
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
111
static drizzle_sys_var* sys_variables[]= {
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
112
  DRIZZLE_SYSVAR(max_threads),
113
  NULL
114
};
115
1228.1.5 by Monty Taylor
Merged in some naming things.
116
DRIZZLE_DECLARE_PLUGIN
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
117
{
1241.10.2 by Monty Taylor
Added support for embedding the drizzle version number in the plugin file.
118
  DRIZZLE_VERSION_ID,
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
119
  "multi_thread",
120
  "0.1",
121
  "Brian Aker",
960.1.1 by Monty Taylor
First pass at scheduler plugin.
122
  "One Thread Per Session Scheduler",
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
123
  PLUGIN_LICENSE_GPL,
124
  init, /* Plugin Init */
125
  deinit, /* Plugin Deinit */
126
  NULL,   /* status variables */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
127
  sys_variables,   /* system variables */
868 by Brian Aker
Adding Multi-threaded Scheduler into the system.
128
  NULL    /* config options */
129
}
1228.1.5 by Monty Taylor
Merged in some naming things.
130
DRIZZLE_DECLARE_PLUGIN_END;