~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-08-11 20:22:59 UTC
  • mfrom: (1093.1.59 captain)
  • Revision ID: brian@gaz-20090811202259-5a92huu2yqmzdx1u
MErge Jay

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#include <drizzled/gettext.h>
19
19
#include <drizzled/error.h>
20
20
#include <drizzled/plugin/scheduler.h>
21
 
#include <drizzled/connect.h>
22
21
#include <drizzled/sql_parse.h>
23
22
#include <drizzled/session.h>
24
 
#include <drizzled/connect.h>
25
23
#include <string>
26
24
 
27
25
using namespace std;
 
26
using namespace drizzled;
28
27
 
 
28
/* Configuration variables. */
29
29
static uint32_t max_threads;
30
30
 
31
 
class Multi_thread_scheduler: public Scheduler
32
 
{
 
31
/**
 
32
 * Function to be run as a thread for each session.
 
33
 */
 
34
namespace
 
35
{
 
36
  extern "C" pthread_handler_t session_thread(void *arg);
 
37
}
 
38
 
 
39
class MultiThreadScheduler: public plugin::Scheduler
 
40
{
 
41
private:
33
42
  drizzled::atomic<uint32_t> thread_count;
34
 
  pthread_attr_t multi_thread_attrib;
 
43
  pthread_attr_t attr;
35
44
 
36
45
public:
37
 
  Multi_thread_scheduler(uint32_t threads): Scheduler(threads)
 
46
  MultiThreadScheduler(): Scheduler()
38
47
  {
 
48
    struct sched_param tmp_sched_param;
 
49
 
 
50
    memset(&tmp_sched_param, 0, sizeof(struct sched_param));
 
51
 
 
52
    /* Setup attribute parameter for session threads. */
 
53
    (void) pthread_attr_init(&attr);
 
54
    (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 
55
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
 
56
 
 
57
    tmp_sched_param.sched_priority= WAIT_PRIOR;
 
58
    (void) pthread_attr_setschedparam(&attr, &tmp_sched_param);
 
59
 
39
60
    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
61
  }
53
62
 
54
 
  ~Multi_thread_scheduler()
 
63
  ~MultiThreadScheduler()
55
64
  {
56
65
    (void) pthread_mutex_lock(&LOCK_thread_count);
57
66
    while (thread_count)
58
67
    {
59
68
      pthread_cond_wait(&COND_thread_count, &LOCK_thread_count);
60
69
    }
 
70
 
61
71
    (void) pthread_mutex_unlock(&LOCK_thread_count);
62
 
    
63
 
    pthread_attr_destroy(&multi_thread_attrib);
 
72
    (void) pthread_attr_destroy(&attr);
64
73
  }
65
74
 
66
 
  virtual bool add_connection(Session *session)
 
75
  virtual bool addSession(Session *session)
67
76
  {
68
 
    int error;
69
 
  
 
77
    if (thread_count >= max_threads)
 
78
      return true;
 
79
 
70
80
    thread_count++;
71
81
  
72
 
    if ((error= pthread_create(&session->real_id, &multi_thread_attrib, handle_one_connection, static_cast<void*>(session))))
 
82
    if (pthread_create(&session->real_id, &attr, session_thread,
 
83
                       static_cast<void*>(session)))
 
84
    {
 
85
      thread_count--;
73
86
      return true;
 
87
    }
74
88
  
75
89
    return false;
76
90
  }
77
91
  
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 */
 
92
  void runSession(Session *session)
 
93
  {
 
94
    if (my_thread_init())
 
95
    {
 
96
      session->disconnect(ER_OUT_OF_RESOURCES, true);
 
97
      statistic_increment(aborted_connects, &LOCK_status);
 
98
      killSessionNow(session);
 
99
    }
 
100
 
 
101
    session->thread_stack= (char*) &session;
 
102
    session->run();
 
103
    killSessionNow(session);
 
104
  }
 
105
 
 
106
  void killSessionNow(Session *session)
 
107
  {
 
108
    /* Locks LOCK_thread_count and deletes session */
 
109
    unlink_session(session);
86
110
    thread_count--;
87
 
  
88
111
    my_thread_end();
89
112
    pthread_exit(0);
90
 
  
91
 
    return true; // We should never reach this point
 
113
    /* We should never reach this point. */
92
114
  }
93
 
  
94
 
  virtual uint32_t count(void)
 
115
};
 
116
 
 
117
namespace
 
118
{
 
119
  extern "C" pthread_handler_t session_thread(void *arg)
95
120
  {
96
 
    return thread_count;
 
121
    Session *session= static_cast<Session*>(arg);
 
122
    MultiThreadScheduler *scheduler= static_cast<MultiThreadScheduler*>(session->scheduler);
 
123
    scheduler->runSession(session);
 
124
    return NULL;
97
125
  }
98
 
};
 
126
}
99
127
 
100
 
class MultiThreadFactory : public SchedulerFactory
 
128
class MultiThreadFactory : public plugin::SchedulerFactory
101
129
{
102
130
public:
103
131
  MultiThreadFactory() : SchedulerFactory("multi_thread")
105
133
    addAlias("multi-thread");
106
134
  }
107
135
 
108
 
  ~MultiThreadFactory() { if (scheduler != NULL) delete scheduler; }
109
 
  Scheduler *operator() ()
 
136
  ~MultiThreadFactory()
 
137
  {
 
138
    if (scheduler != NULL)
 
139
      delete scheduler;
 
140
  }
 
141
 
 
142
  plugin::Scheduler *operator() ()
110
143
  {
111
144
    if (scheduler == NULL)
112
 
      scheduler= new Multi_thread_scheduler(max_threads);
 
145
      scheduler= new MultiThreadScheduler();
113
146
    return scheduler;
114
147
  }
115
148
};
 
149
 
116
150
static MultiThreadFactory *factory= NULL;
117
151
 
118
 
static int init(PluginRegistry &registry)
 
152
static int init(drizzled::plugin::Registry &registry)
119
153
{
120
154
  factory= new MultiThreadFactory();
121
155
  registry.add(factory);
122
156
  return 0;
123
157
}
124
158
 
125
 
static int deinit(PluginRegistry &registry)
 
159
static int deinit(drizzled::plugin::Registry &registry)
126
160
{
127
161
  if (factory)
128
162
  {
135
169
static DRIZZLE_SYSVAR_UINT(max_threads, max_threads,
136
170
                           PLUGIN_VAR_RQCMDARG,
137
171
                           N_("Maximum number of user threads available."),
138
 
                           NULL, NULL, 2048, 1, 4048, 0);
 
172
                           NULL, NULL, 2048, 1, 4096, 0);
139
173
 
140
174
static struct st_mysql_sys_var* system_variables[]= {
141
175
  DRIZZLE_SYSVAR(max_threads),