~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/multi_thread/multi_thread.cc

  • Committer: Brian Aker
  • Date: 2009-10-01 22:56:26 UTC
  • mto: (1154.1.1 staging)
  • mto: This revision was merged to the branch mainline in revision 1155.
  • Revision ID: brian@gaz-20091001225626-sb1pdykpxlnkheaj
Remove Factory/make scheduler work like everything else.

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 <plugin/multi_thread/multi_thread.h>
 
17
 
 
18
using namespace std;
 
19
using namespace drizzled;
 
20
 
 
21
/* Configuration variables. */
 
22
static uint32_t max_threads;
 
23
 
 
24
/* Global's (TBR) */
 
25
static MultiThreadScheduler *scheduler= NULL;
 
26
 
 
27
/**
 
28
 * Function to be run as a thread for each session.
 
29
 */
 
30
namespace
 
31
{
 
32
  extern "C" pthread_handler_t session_thread(void *arg);
 
33
}
 
34
 
 
35
namespace
 
36
{
 
37
  extern "C" pthread_handler_t session_thread(void *arg)
 
38
  {
 
39
    Session *session= static_cast<Session*>(arg);
 
40
    MultiThreadScheduler *scheduler= static_cast<MultiThreadScheduler*>(session->scheduler);
 
41
    scheduler->runSession(session);
 
42
    return NULL;
 
43
  }
 
44
}
 
45
 
 
46
 
 
47
bool MultiThreadScheduler::addSession(Session *session)
 
48
{
 
49
  if (thread_count >= max_threads)
 
50
    return true;
 
51
 
 
52
  thread_count++;
 
53
 
 
54
  if (pthread_create(&session->real_id, &attr, session_thread,
 
55
                     static_cast<void*>(session)))
 
56
  {
 
57
    thread_count--;
 
58
    return true;
 
59
  }
 
60
 
 
61
  return false;
 
62
}
 
63
 
 
64
 
 
65
void MultiThreadScheduler::killSessionNow(Session *session)
 
66
{
 
67
  /* Locks LOCK_thread_count and deletes session */
 
68
  unlink_session(session);
 
69
  thread_count--;
 
70
  my_thread_end();
 
71
  pthread_exit(0);
 
72
  /* We should never reach this point. */
 
73
}
 
74
 
 
75
MultiThreadScheduler::~MultiThreadScheduler()
 
76
{
 
77
  (void) pthread_mutex_lock(&LOCK_thread_count);
 
78
  while (thread_count)
 
79
  {
 
80
    pthread_cond_wait(&COND_thread_count, &LOCK_thread_count);
 
81
  }
 
82
 
 
83
  (void) pthread_mutex_unlock(&LOCK_thread_count);
 
84
  (void) pthread_attr_destroy(&attr);
 
85
}
 
86
 
 
87
  
 
88
static int init(drizzled::plugin::Registry &registry)
 
89
{
 
90
  scheduler= new MultiThreadScheduler("multi_thread");
 
91
  registry.add(scheduler);
 
92
 
 
93
  return 0;
 
94
}
 
95
 
 
96
static int deinit(drizzled::plugin::Registry &registry)
 
97
{
 
98
  registry.remove(scheduler);
 
99
  delete scheduler;
 
100
 
 
101
  return 0;
 
102
}
 
103
 
 
104
static DRIZZLE_SYSVAR_UINT(max_threads, max_threads,
 
105
                           PLUGIN_VAR_RQCMDARG,
 
106
                           N_("Maximum number of user threads available."),
 
107
                           NULL, NULL, 2048, 1, 4096, 0);
 
108
 
 
109
static struct st_mysql_sys_var* system_variables[]= {
 
110
  DRIZZLE_SYSVAR(max_threads),
 
111
  NULL
 
112
};
 
113
 
 
114
drizzle_declare_plugin(multi_thread)
 
115
{
 
116
  "multi_thread",
 
117
  "0.1",
 
118
  "Brian Aker",
 
119
  "One Thread Per Session Scheduler",
 
120
  PLUGIN_LICENSE_GPL,
 
121
  init, /* Plugin Init */
 
122
  deinit, /* Plugin Deinit */
 
123
  NULL,   /* status variables */
 
124
  system_variables,   /* system variables */
 
125
  NULL    /* config options */
 
126
}
 
127
drizzle_declare_plugin_end;