~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/multi_thread/multi_thread.cc

  • Committer: Brian Aker
  • Date: 2010-09-11 01:35:47 UTC
  • mto: (1759.2.1 build)
  • mto: This revision was merged to the branch mainline in revision 1762.
  • Revision ID: brian@tangent.org-20100911013547-b04k7f1qddr3ml4t
Shuffle native functions over to hash such that we have a specific container
for them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#include "config.h"
17
17
#include <plugin/multi_thread/multi_thread.h>
18
18
#include "drizzled/pthread_globals.h"
 
19
#include <boost/program_options.hpp>
 
20
#include <drizzled/module/option_map.h>
 
21
#include <drizzled/errmsg_print.h>
19
22
 
 
23
namespace po= boost::program_options;
20
24
using namespace std;
21
25
using namespace drizzled;
22
26
 
26
30
/* Global's (TBR) */
27
31
static MultiThreadScheduler *scheduler= NULL;
28
32
 
 
33
namespace drizzled
 
34
{
 
35
  extern size_t my_thread_stack_size;
 
36
}
 
37
 
29
38
/**
30
39
 * Function to be run as a thread for each session.
31
40
 */
51
60
  if (thread_count >= max_threads)
52
61
    return true;
53
62
 
 
63
#ifdef __sun
 
64
  /*
 
65
   * Solaris will return zero for the stack size in a call to
 
66
   * pthread_attr_getstacksize() to indicate that the OS default stack
 
67
   * size is used. We need an actual value in my_thread_stack_size so that
 
68
   * check_stack_overrun() will work. The Solaris man page for the
 
69
   * pthread_attr_getstacksize() function says that 2M is used for 64-bit
 
70
   * processes. We'll explicitly set it here to make sure that is what
 
71
   * will be used.
 
72
   */
 
73
  if (my_thread_stack_size == 0)
 
74
    my_thread_stack_size= 2 * 1024 * 1024;
 
75
#endif
 
76
 
 
77
  /* Thread stack size of zero means just use the OS default */
 
78
  if (my_thread_stack_size != 0)
 
79
  {
 
80
    int err= pthread_attr_setstacksize(&attr, my_thread_stack_size);
 
81
 
 
82
    if (err != 0)
 
83
    {
 
84
      errmsg_printf(ERRMSG_LVL_ERROR,
 
85
                    _("Unable to set thread stack size to %" PRId64 "\n"),
 
86
                    static_cast<uint64_t>(my_thread_stack_size));
 
87
      return true;
 
88
    }
 
89
  }
 
90
  else
 
91
  {
 
92
    /* Get the thread stack size that the OS will use and make sure
 
93
       that we update our global variable. */
 
94
    int err= pthread_attr_getstacksize(&attr, &my_thread_stack_size);
 
95
 
 
96
    if (err != 0)
 
97
    {
 
98
      errmsg_printf(ERRMSG_LVL_ERROR, _("Unable to get thread stack size\n"));
 
99
      return true;
 
100
    }
 
101
  }
 
102
 
54
103
  thread_count.increment();
55
104
 
56
105
  if (pthread_create(&session->real_id, &attr, session_thread,
76
125
 
77
126
MultiThreadScheduler::~MultiThreadScheduler()
78
127
{
79
 
  (void) pthread_mutex_lock(&LOCK_thread_count);
 
128
  LOCK_thread_count.lock();
80
129
  while (thread_count)
81
130
  {
82
 
    pthread_cond_wait(&COND_thread_count, &LOCK_thread_count);
 
131
    pthread_cond_wait(COND_thread_count.native_handle(), LOCK_thread_count.native_handle());
83
132
  }
84
133
 
85
 
  (void) pthread_mutex_unlock(&LOCK_thread_count);
 
134
  LOCK_thread_count.unlock();
86
135
  (void) pthread_attr_destroy(&attr);
87
136
}
88
137
 
89
138
  
90
 
static int init(drizzled::plugin::Context &context)
 
139
static int init(drizzled::module::Context &context)
91
140
{
 
141
  
 
142
  const module::option_map &vm= context.getOptions();
 
143
  if (vm.count("max-threads"))
 
144
  {
 
145
    if (max_threads > 4096 || max_threads < 1)
 
146
    {
 
147
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for max-threads\n"));
 
148
      exit(-1);
 
149
    }
 
150
  }
 
151
 
92
152
  scheduler= new MultiThreadScheduler("multi_thread");
93
153
  context.add(scheduler);
94
154
 
100
160
                           N_("Maximum number of user threads available."),
101
161
                           NULL, NULL, 2048, 1, 4096, 0);
102
162
 
 
163
static void init_options(drizzled::module::option_context &context)
 
164
{
 
165
  context("max-threads",
 
166
          po::value<uint32_t>(&max_threads)->default_value(2048),
 
167
          N_("Maximum number of user threads available."));
 
168
}
 
169
 
103
170
static drizzle_sys_var* sys_variables[]= {
104
171
  DRIZZLE_SYSVAR(max_threads),
105
172
  NULL
115
182
  PLUGIN_LICENSE_GPL,
116
183
  init, /* Plugin Init */
117
184
  sys_variables,   /* system variables */
118
 
  NULL    /* config options */
 
185
  init_options    /* config options */
119
186
}
120
187
DRIZZLE_DECLARE_PLUGIN_END;