~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/multi_thread/multi_thread.cc

  • Committer: Monty Taylor
  • Date: 2008-08-16 21:06:22 UTC
  • Revision ID: monty@inaugust.com-20080816210622-zpnn13unyinqzn72
Updated po files.

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