~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/single_thread/single_thread.cc

Merged in latest plugin-slot-reorg.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
16
#include <drizzled/server_includes.h>
 
17
#include <drizzled/gettext.h>
 
18
#include <drizzled/error.h>
 
19
#include <drizzled/plugin/scheduler.h>
 
20
#include <drizzled/sql_parse.h>
 
21
#include <drizzled/session.h>
 
22
#include <string>
 
23
 
 
24
using namespace std;
 
25
using namespace drizzled;
 
26
 
 
27
/**
 
28
 * Simple scheduler that uses the main thread to handle the request. This
 
29
 * should only be used for debugging.
 
30
 */
 
31
class SingleThreadScheduler : public plugin::Scheduler
 
32
{
 
33
public:
 
34
  SingleThreadScheduler() : Scheduler() {}
 
35
 
 
36
  /* When we enter this function, LOCK_thread_count is held! */
 
37
  virtual bool addSession(Session *session)
 
38
  {
 
39
    if (my_thread_init())
 
40
    {
 
41
      session->disconnect(ER_OUT_OF_RESOURCES, true);
 
42
      statistic_increment(aborted_connects, &LOCK_status);
 
43
      return true;
 
44
    }
 
45
 
 
46
    /*
 
47
      This is not the real thread start beginning, but there is not an easy
 
48
      way to find it.
 
49
    */
 
50
    session->thread_stack= (char *)&session;
 
51
 
 
52
    session->run();
 
53
    killSessionNow(session);
 
54
    return false;
 
55
  }
 
56
 
 
57
  virtual void killSessionNow(Session *session)
 
58
  {
 
59
    unlink_session(session);
 
60
    my_thread_end();
 
61
  }
 
62
};
 
63
 
 
64
 
 
65
class SingleThreadFactory : public plugin::SchedulerFactory
 
66
{
 
67
public:
 
68
  SingleThreadFactory() : SchedulerFactory("single_thread") {}
 
69
  ~SingleThreadFactory() { if (scheduler != NULL) delete scheduler; }
 
70
  plugin::Scheduler *operator() ()
 
71
  {
 
72
    if (scheduler == NULL)
 
73
      scheduler= new SingleThreadScheduler();
 
74
    return scheduler;
 
75
  }
 
76
};
 
77
 
 
78
SingleThreadFactory *factory= NULL;
 
79
 
 
80
static int init(drizzled::plugin::Registry &registry)
 
81
{
 
82
  factory= new SingleThreadFactory();
 
83
  registry.scheduler.add(factory);
 
84
  return 0;
 
85
}
 
86
 
 
87
static int deinit(drizzled::plugin::Registry &registry)
 
88
{
 
89
  if (factory)
 
90
  {
 
91
    registry.scheduler.remove(factory);
 
92
    delete factory;
 
93
  }
 
94
  return 0;
 
95
}
 
96
 
 
97
static struct st_mysql_sys_var* system_variables[]= {
 
98
  NULL
 
99
};
 
100
 
 
101
drizzle_declare_plugin(single_thread)
 
102
{
 
103
  "single_thread",
 
104
  "0.1",
 
105
  "Brian Aker",
 
106
  "Single Thread Scheduler",
 
107
  PLUGIN_LICENSE_GPL,
 
108
  init, /* Plugin Init */
 
109
  deinit, /* Plugin Deinit */
 
110
  NULL,   /* status variables */
 
111
  system_variables,   /* system variables */
 
112
  NULL    /* config options */
 
113
}
 
114
drizzle_declare_plugin_end;