~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/multi_thread/multi_thread.cc

  • Committer: Stewart Smith
  • Date: 2010-11-10 08:10:53 UTC
  • mto: (1819.7.44 update-innobase)
  • mto: This revision was merged to the branch mainline in revision 1926.
  • Revision ID: stewart@flamingspork.com-20101110081053-fz4depckopv167tg
add tset for bug673380: CREATE TABLE AS can create a table with a column type of NULL

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
#include <boost/program_options.hpp>
20
20
#include <drizzled/module/option_map.h>
21
21
#include <drizzled/errmsg_print.h>
22
 
#include "drizzled/session.h"
23
 
#include "drizzled/session_list.h"
24
22
 
25
23
#include <boost/thread.hpp>
26
24
#include <boost/bind.hpp>
30
28
using namespace drizzled;
31
29
 
32
30
/* Configuration variables. */
33
 
typedef constrained_check<uint32_t, 4096, 1> max_threads_constraint;
34
 
static max_threads_constraint max_threads;
 
31
static uint32_t max_threads;
 
32
 
 
33
/* Global's (TBR) */
 
34
static MultiThreadScheduler *scheduler= NULL;
35
35
 
36
36
namespace drizzled
37
37
{
38
38
  extern size_t my_thread_stack_size;
39
39
}
40
40
 
41
 
void MultiThreadScheduler::runSession(drizzled::session_id_t id)
 
41
void MultiThreadScheduler::runSession(drizzled::Session *session)
42
42
{
43
 
  char stack_dummy;
44
 
  Session::shared_ptr session(session::Cache::singleton().find(id));
45
 
 
46
 
  if (not session)
47
 
  {
48
 
    std::cerr << "Session killed before thread could execute\n";
49
 
    return;
50
 
  }
51
 
 
52
43
  if (drizzled::internal::my_thread_init())
53
44
  {
54
45
    session->disconnect(drizzled::ER_OUT_OF_RESOURCES, true);
57
48
  }
58
49
  boost::this_thread::at_thread_exit(&internal::my_thread_end);
59
50
 
60
 
  session->thread_stack= (char*) &stack_dummy;
 
51
  session->thread_stack= (char*) &session;
61
52
  session->run();
62
53
  killSessionNow(session);
63
 
  // @todo remove hard spin by disconnection the session first from the
64
 
  // thread.
65
 
  while (not session.unique()) {}
66
54
}
67
55
 
68
56
void MultiThreadScheduler::setStackSize()
103
91
#endif
104
92
}
105
93
 
106
 
bool MultiThreadScheduler::addSession(Session::shared_ptr &session)
 
94
bool MultiThreadScheduler::addSession(Session *session)
107
95
{
108
96
  if (thread_count >= max_threads)
109
97
    return true;
110
98
 
111
99
  thread_count.increment();
112
100
 
113
 
  boost::thread new_thread(boost::bind(&MultiThreadScheduler::runSession, this, session->getSessionId()));
 
101
  boost::thread new_thread(boost::bind(&MultiThreadScheduler::runSession, this, session));
114
102
 
115
103
  if (not new_thread.joinable())
116
104
  {
122
110
}
123
111
 
124
112
 
125
 
void MultiThreadScheduler::killSessionNow(Session::shared_ptr &session)
 
113
void MultiThreadScheduler::killSessionNow(Session *session)
126
114
{
127
115
  /* Locks LOCK_thread_count and deletes session */
128
116
  Session::unlink(session);
131
119
 
132
120
MultiThreadScheduler::~MultiThreadScheduler()
133
121
{
134
 
  boost::mutex::scoped_lock scopedLock(drizzled::session::Cache::singleton().mutex());
 
122
  boost::mutex::scoped_lock scopedLock(LOCK_thread_count);
135
123
  while (thread_count)
136
124
  {
137
125
    COND_thread_count.wait(scopedLock);
142
130
static int init(drizzled::module::Context &context)
143
131
{
144
132
  
145
 
  context.add(new MultiThreadScheduler("multi_thread"));
 
133
  const module::option_map &vm= context.getOptions();
 
134
  if (vm.count("max-threads"))
 
135
  {
 
136
    if (max_threads > 4096 || max_threads < 1)
 
137
    {
 
138
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for max-threads\n"));
 
139
      return 1;
 
140
    }
 
141
  }
 
142
 
 
143
  scheduler= new MultiThreadScheduler("multi_thread");
 
144
  context.add(scheduler);
146
145
 
147
146
  return 0;
148
147
}
149
148
 
 
149
static DRIZZLE_SYSVAR_UINT(max_threads, max_threads,
 
150
                           PLUGIN_VAR_RQCMDARG,
 
151
                           N_("Maximum number of user threads available."),
 
152
                           NULL, NULL, 2048, 1, 4096, 0);
 
153
 
150
154
static void init_options(drizzled::module::option_context &context)
151
155
{
152
156
  context("max-threads",
153
 
          po::value<max_threads_constraint>(&max_threads)->default_value(2048),
 
157
          po::value<uint32_t>(&max_threads)->default_value(2048),
154
158
          N_("Maximum number of user threads available."));
155
159
}
156
160
 
 
161
static drizzle_sys_var* sys_variables[]= {
 
162
  DRIZZLE_SYSVAR(max_threads),
 
163
  NULL
 
164
};
 
165
 
157
166
DRIZZLE_DECLARE_PLUGIN
158
167
{
159
168
  DRIZZLE_VERSION_ID,
163
172
  "One Thread Per Session Scheduler",
164
173
  PLUGIN_LICENSE_GPL,
165
174
  init, /* Plugin Init */
166
 
  NULL,   /* system variables */
 
175
  sys_variables,   /* system variables */
167
176
  init_options    /* config options */
168
177
}
169
178
DRIZZLE_DECLARE_PLUGIN_END;