~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/internal/my_thr_init.cc

  • Committer: Brian Aker
  • Date: 2010-09-21 09:57:03 UTC
  • Revision ID: brian@tangent.org-20100921095703-622iopd89890rsky
This modifies our thread system to be based on boost, and it fixes a
critical bug where stack size was not being calculated correctly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
# endif
40
40
#endif
41
41
 
 
42
#include <boost/thread/thread.hpp>
 
43
#include <boost/thread/mutex.hpp>
 
44
#include <boost/thread/tss.hpp>
 
45
 
42
46
namespace drizzled
43
47
{
44
48
namespace internal
45
49
{
46
50
 
47
 
pthread_key_t THR_KEY_mysys;
 
51
boost::thread_specific_ptr<st_my_thread_var> THR_KEY_mysys;
48
52
boost::mutex THR_LOCK_threads;
49
53
pthread_cond_t  THR_COND_threads;
50
54
uint32_t THR_thread_count= 0;
63
67
 
64
68
bool my_thread_global_init(void)
65
69
{
66
 
  int pth_ret;
67
 
 
68
 
  if ((pth_ret= pthread_key_create(&THR_KEY_mysys, NULL)) != 0)
69
 
  {
70
 
    fprintf(stderr,"Can't initialize threads: error %d\n", pth_ret);
71
 
    return 1;
72
 
  }
73
 
 
74
70
  pthread_cond_init(&THR_COND_threads, NULL);
75
71
  if (my_thread_init())
76
72
  {
110
106
    }
111
107
  }
112
108
 
113
 
  pthread_key_delete(THR_KEY_mysys);
114
109
  if (all_threads_killed)
115
110
  {
116
111
    pthread_cond_destroy(&THR_COND_threads);
136
131
  st_my_thread_var *tmp= NULL;
137
132
 
138
133
  // We should mever see my_thread_init()  called twice
139
 
  if (pthread_getspecific(THR_KEY_mysys))
 
134
  if (THR_KEY_mysys.get())
140
135
    return 0;
141
136
 
142
 
  tmp= static_cast<st_my_thread_var *>(calloc(1, sizeof(*tmp)));
 
137
  tmp= new st_my_thread_var;
143
138
  if (tmp == NULL)
144
139
  {
145
140
    return 1;
146
141
  }
147
 
  pthread_setspecific(THR_KEY_mysys,tmp);
148
 
  tmp->pthread_self= pthread_self();
149
 
  pthread_mutex_init(&tmp->mutex,MY_MUTEX_INIT_FAST);
150
 
  pthread_cond_init(&tmp->suspend, NULL);
151
 
  tmp->init= 1;
 
142
  THR_KEY_mysys.reset(tmp);
152
143
 
153
144
  boost::mutex::scoped_lock scopedLock(THR_LOCK_threads);
154
145
  tmp->id= ++thread_id;
172
163
 
173
164
void my_thread_end(void)
174
165
{
175
 
  st_my_thread_var *tmp=
176
 
    static_cast<st_my_thread_var *>(pthread_getspecific(THR_KEY_mysys));
 
166
  st_my_thread_var *tmp= THR_KEY_mysys.get();
177
167
 
178
 
  if (tmp && tmp->init)
 
168
  if (tmp)
179
169
  {
180
 
#if !defined(__bsdi__) && !defined(__OpenBSD__)
181
 
 /* bsdi and openbsd 3.5 dumps core here */
182
 
    pthread_cond_destroy(&tmp->suspend);
183
 
#endif
184
 
    pthread_mutex_destroy(&tmp->mutex);
185
 
    free(tmp);
 
170
    delete tmp;
 
171
    THR_KEY_mysys.release();
186
172
 
187
173
    /*
188
174
      Decrement counter for number of running threads. We are using this
199
185
 
200
186
struct st_my_thread_var *_my_thread_var(void)
201
187
{
202
 
  struct st_my_thread_var *tmp= (struct st_my_thread_var*)pthread_getspecific(THR_KEY_mysys);
203
 
  return tmp;
 
188
  return THR_KEY_mysys.get();
204
189
}
205
190
 
206
191
} /* namespace internal */