~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/signal_handler/signal_handler.cc

  • Committer: lbieber
  • Date: 2010-09-16 00:35:01 UTC
  • mfrom: (1755.2.14 refactor)
  • mto: This revision was merged to the branch mainline in revision 1767.
  • Revision ID: lbieber@orisndriz08-20100916003501-vqcujpfmpgg9lfyr
Merge Brian - more refactoring and adding option for optional schedulers and making them --plugin-add only

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
#include "drizzled/drizzled.h"
29
29
 
 
30
#include <boost/thread/thread.hpp>
 
31
 
30
32
#include <sys/stat.h>
31
33
#include <fcntl.h>
32
34
 
33
35
 
34
36
static bool kill_in_progress= false;
35
 
extern "C" pthread_handler_t signal_hand(void *);
 
37
void signal_hand(void);
36
38
 
37
39
namespace drizzled
38
40
{
107
109
 
108
110
 
109
111
/** This threads handles all signals and alarms. */
110
 
pthread_handler_t signal_hand(void *)
 
112
void signal_hand()
111
113
{
112
114
  sigset_t set;
113
115
  int sig;
151
153
  for (;;)
152
154
  {
153
155
    int error;                                  // Used when debugging
 
156
 
154
157
    if (shutdown_in_progress && !abort_loop)
155
158
    {
156
159
      sig= SIGTERM;
157
160
      error=0;
158
161
    }
159
162
    else
 
163
    {
160
164
      while ((error= sigwait(&set,&sig)) == EINTR) ;
 
165
    }
 
166
 
161
167
    if (cleanup_done)
162
168
    {
163
169
      internal::my_thread_end();
164
170
      signal_thread_in_use= false;
165
171
 
166
 
      return NULL;
 
172
      return;
167
173
    }
168
174
    switch (sig) {
169
175
    case SIGTERM:
194
200
{
195
201
  SignalHandler(const SignalHandler &);
196
202
  SignalHandler& operator=(const SignalHandler &);
 
203
  boost::thread thread;
 
204
 
197
205
public:
198
 
  SignalHandler()
199
 
    : drizzled::plugin::Daemon("Signal Handler")
 
206
  SignalHandler() :
 
207
    drizzled::plugin::Daemon("Signal Handler")
200
208
  {
201
 
    int error;
202
 
    pthread_attr_t thr_attr;
203
 
    size_t my_thread_stack_size= 65536;
204
 
 
205
 
    (void) pthread_attr_init(&thr_attr);
206
 
    pthread_attr_setscope(&thr_attr, PTHREAD_SCOPE_SYSTEM);
207
 
    (void) pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED);
208
 
    {
209
 
      struct sched_param tmp_sched_param;
210
 
 
211
 
      memset(&tmp_sched_param, 0, sizeof(tmp_sched_param));
212
 
      tmp_sched_param.sched_priority= INTERRUPT_PRIOR;
213
 
      (void)pthread_attr_setschedparam(&thr_attr, &tmp_sched_param);
214
 
    }
215
 
#if defined(__ia64__) || defined(__ia64)
216
 
    /*
217
 
      Peculiar things with ia64 platforms - it seems we only have half the
218
 
      stack size in reality, so we have to double it here
219
 
    */
220
 
    pthread_attr_setstacksize(&thr_attr, my_thread_stack_size*2);
221
 
# else
222
 
    pthread_attr_setstacksize(&thr_attr, my_thread_stack_size);
223
 
#endif
224
 
 
225
209
    // @todo fix spurious wakeup issue
226
 
    (void) LOCK_thread_count.lock();
227
 
    if ((error= pthread_create(&signal_thread, &thr_attr, signal_hand, 0)))
228
 
    {
229
 
      errmsg_printf(ERRMSG_LVL_ERROR,
230
 
                    _("Can't create interrupt-thread (error %d, errno: %d)"),
231
 
                    error,errno);
232
 
      exit(1);
233
 
    }
234
 
    pthread_cond_wait(COND_thread_count.native_handle(), LOCK_thread_count.native_handle());
235
 
    LOCK_thread_count.unlock();
236
 
 
237
 
    (void) pthread_attr_destroy(&thr_attr);
 
210
    boost::mutex::scoped_lock scopedLock(LOCK_thread_count);
 
211
    thread= boost::thread(signal_hand);
 
212
    signal_thread= thread.native_handle();
 
213
    COND_thread_count.wait(scopedLock);
238
214
  }
239
215
 
240
216
  /**
247
223
      Wait up to 100000 micro-seconds for signal thread to die. We use this mainly to
248
224
      avoid getting warnings that internal::my_thread_end has not been called
249
225
    */
250
 
    for (uint32_t i= 0 ; i < 100 && signal_thread_in_use; i++)
251
 
    {
252
 
      if (pthread_kill(signal_thread, SIGTERM) != ESRCH)
253
 
        break;
254
 
 
255
 
      struct timespec tm;
256
 
      tm.tv_sec= 0;
257
 
      tm.tv_nsec= 100000;
258
 
 
259
 
      nanosleep(&tm, NULL);                             // Give it time to die
260
 
    }
261
 
 
 
226
    pthread_kill(signal_thread, SIGTERM);
 
227
    thread.join();
262
228
  }
263
229
};
264
230
 
265
231
static int init(drizzled::module::Context& context)
266
232
{
267
 
  SignalHandler *handler= new SignalHandler;
268
 
  context.add(handler);
 
233
  context.add(new SignalHandler);
 
234
 
269
235
  return 0;
270
236
}
271
237