~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/multi_thread/multi_thread.cc

  • Committer: Monty Taylor
  • Date: 2009-03-25 21:06:47 UTC
  • mto: This revision was merged to the branch mainline in revision 964.
  • Revision ID: mordred@inaugust.com-20090325210647-7j1tm98gvct3jxsu
Removed legacy_db_type.

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
  tbb::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
static int init(void *p)
 
101
{
 
102
  Multi_thread_scheduler** sched= static_cast<Multi_thread_scheduler **>(p);
 
103
 
 
104
  *sched= new Multi_thread_scheduler(max_threads);
 
105
 
 
106
  return 0;
 
107
}
 
108
 
 
109
static int deinit(void *p)
 
110
{
 
111
 
 
112
  Multi_thread_scheduler *sched= static_cast<Multi_thread_scheduler *>(p);
 
113
  delete sched;
 
114
 
 
115
  return 0;
 
116
}
 
117
 
 
118
static DRIZZLE_SYSVAR_UINT(max_threads, max_threads,
 
119
                           PLUGIN_VAR_RQCMDARG,
 
120
                           N_("Maximum number of user threads available."),
 
121
                           NULL, NULL, 2048, 1, 4048, 0);
 
122
 
 
123
static struct st_mysql_sys_var* system_variables[]= {
 
124
  DRIZZLE_SYSVAR(max_threads),
 
125
  NULL
 
126
};
 
127
 
 
128
drizzle_declare_plugin(multi_thread)
 
129
{
 
130
  DRIZZLE_SCHEDULING_PLUGIN,
 
131
  "multi_thread",
 
132
  "0.1",
 
133
  "Brian Aker",
 
134
  "One Thread Per Session Scheduler",
 
135
  PLUGIN_LICENSE_GPL,
 
136
  init, /* Plugin Init */
 
137
  deinit, /* Plugin Deinit */
 
138
  NULL,   /* status variables */
 
139
  system_variables,   /* system variables */
 
140
  NULL    /* config options */
 
141
}
 
142
drizzle_declare_plugin_end;