~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/multi_thread/multi_thread.cc

Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.

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