~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/multi_thread/multi_thread.cc

  • Committer: Stewart Smith
  • Date: 2008-07-13 08:00:16 UTC
  • mto: (210.1.1 drizzle)
  • mto: This revision was merged to the branch mainline in revision 211.
  • Revision ID: stewart@flamingspork.com-20080713080016-8qtjv9ypt42azzr6
CRC32() as UDF

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/atomics.h>
18
 
#include <drizzled/gettext.h>
19
 
#include <drizzled/error.h>
20
 
#include <drizzled/plugin/scheduler.h>
21
 
#include <drizzled/connect.h>
22
 
#include <drizzled/sql_parse.h>
23
 
#include <drizzled/session.h>
24
 
#include <drizzled/connect.h>
25
 
#include <string>
26
 
 
27
 
using namespace std;
28
 
 
29
 
static uint32_t max_threads;
30
 
 
31
 
class Multi_thread_scheduler: public Scheduler
32
 
{
33
 
  drizzled::atomic<uint32_t> thread_count;
34
 
  pthread_attr_t multi_thread_attrib;
35
 
 
36
 
public:
37
 
  Multi_thread_scheduler(uint32_t threads): Scheduler(threads)
38
 
  {
39
 
    thread_count= 0;
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);
45
 
    {
46
 
      struct sched_param tmp_sched_param;
47
 
  
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);
51
 
    }
52
 
  }
53
 
 
54
 
  ~Multi_thread_scheduler()
55
 
  {
56
 
    (void) pthread_mutex_lock(&LOCK_thread_count);
57
 
    while (thread_count)
58
 
    {
59
 
      pthread_cond_wait(&COND_thread_count, &LOCK_thread_count);
60
 
    }
61
 
    (void) pthread_mutex_unlock(&LOCK_thread_count);
62
 
    
63
 
    pthread_attr_destroy(&multi_thread_attrib);
64
 
  }
65
 
 
66
 
  virtual bool add_connection(Session *session)
67
 
  {
68
 
    int error;
69
 
  
70
 
    thread_count++;
71
 
  
72
 
    if ((error= pthread_create(&session->real_id, &multi_thread_attrib, handle_one_connection, static_cast<void*>(session))))
73
 
      return true;
74
 
  
75
 
    return false;
76
 
  }
77
 
  
78
 
  
79
 
  /*
80
 
    End connection, in case when we are using 'no-threads'
81
 
  */
82
 
  
83
 
  virtual bool end_thread(Session *session, bool)
84
 
  {
85
 
    unlink_session(session);   /* locks LOCK_thread_count and deletes session */
86
 
    thread_count--;
87
 
  
88
 
    my_thread_end();
89
 
    pthread_exit(0);
90
 
  
91
 
    return true; // We should never reach this point
92
 
  }
93
 
  
94
 
  virtual uint32_t count(void)
95
 
  {
96
 
    return thread_count;
97
 
  }
98
 
};
99
 
 
100
 
class MultiThreadFactory : public SchedulerFactory
101
 
{
102
 
public:
103
 
  MultiThreadFactory() : SchedulerFactory("multi_thread") {}
104
 
  ~MultiThreadFactory() { if (scheduler != NULL) delete scheduler; }
105
 
  Scheduler *operator() ()
106
 
  {
107
 
    if (scheduler == NULL)
108
 
      scheduler= new Multi_thread_scheduler(max_threads);
109
 
    return scheduler;
110
 
  }
111
 
};
112
 
static MultiThreadFactory *factory= NULL;
113
 
 
114
 
static int init(PluginRegistry &registry)
115
 
{
116
 
  factory= new MultiThreadFactory();
117
 
  registry.add(factory);
118
 
  return 0;
119
 
}
120
 
 
121
 
static int deinit(PluginRegistry &registry)
122
 
{
123
 
  if (factory)
124
 
  {
125
 
    registry.remove(factory);
126
 
    delete factory;
127
 
  }
128
 
  return 0;
129
 
}
130
 
 
131
 
static DRIZZLE_SYSVAR_UINT(max_threads, max_threads,
132
 
                           PLUGIN_VAR_RQCMDARG,
133
 
                           N_("Maximum number of user threads available."),
134
 
                           NULL, NULL, 2048, 1, 4048, 0);
135
 
 
136
 
static struct st_mysql_sys_var* system_variables[]= {
137
 
  DRIZZLE_SYSVAR(max_threads),
138
 
  NULL
139
 
};
140
 
 
141
 
drizzle_declare_plugin(multi_thread)
142
 
{
143
 
  "multi_thread",
144
 
  "0.1",
145
 
  "Brian Aker",
146
 
  "One Thread Per Session Scheduler",
147
 
  PLUGIN_LICENSE_GPL,
148
 
  init, /* Plugin Init */
149
 
  deinit, /* Plugin Deinit */
150
 
  NULL,   /* status variables */
151
 
  system_variables,   /* system variables */
152
 
  NULL    /* config options */
153
 
}
154
 
drizzle_declare_plugin_end;