~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/signal_handler/signal_handler.cc

  • Committer: Eric Day
  • Date: 2010-03-25 19:28:37 UTC
  • mfrom: (1405 staging)
  • mto: This revision was merged to the branch mainline in revision 1409.
  • Revision ID: eday@oddments.org-20100325192837-4exmacbrywjovsqp
Merged trunk, rsolved conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
#include "drizzled/pthread_globals.h"
23
23
#include "drizzled/internal/my_pthread.h"
24
24
#include "drizzled/internal/my_sys.h"
 
25
#include "drizzled/plugin/daemon.h"
25
26
 
26
27
#include <sys/stat.h>
27
28
#include <fcntl.h>
199
200
  }
200
201
}
201
202
 
202
 
 
203
 
static int init(drizzled::plugin::Registry&)
 
203
class SignalHandler :
 
204
  public drizzled::plugin::Daemon
204
205
{
205
 
  int error;
206
 
  pthread_attr_t thr_attr;
207
 
  size_t my_thread_stack_size= 65536;
208
 
 
209
 
  (void) pthread_attr_init(&thr_attr);
210
 
  pthread_attr_setscope(&thr_attr, PTHREAD_SCOPE_SYSTEM);
211
 
  (void) pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED);
 
206
  SignalHandler(const SignalHandler &);
 
207
  SignalHandler& operator=(const SignalHandler &);
 
208
public:
 
209
  SignalHandler()
 
210
    : drizzled::plugin::Daemon("Signal Handler")
212
211
  {
213
 
    struct sched_param tmp_sched_param;
214
 
 
215
 
    memset(&tmp_sched_param, 0, sizeof(tmp_sched_param));
216
 
    tmp_sched_param.sched_priority= INTERRUPT_PRIOR;
217
 
    (void)pthread_attr_setschedparam(&thr_attr, &tmp_sched_param);
218
 
  }
 
212
    int error;
 
213
    pthread_attr_t thr_attr;
 
214
    size_t my_thread_stack_size= 65536;
 
215
 
 
216
    (void) pthread_attr_init(&thr_attr);
 
217
    pthread_attr_setscope(&thr_attr, PTHREAD_SCOPE_SYSTEM);
 
218
    (void) pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED);
 
219
    {
 
220
      struct sched_param tmp_sched_param;
 
221
 
 
222
      memset(&tmp_sched_param, 0, sizeof(tmp_sched_param));
 
223
      tmp_sched_param.sched_priority= INTERRUPT_PRIOR;
 
224
      (void)pthread_attr_setschedparam(&thr_attr, &tmp_sched_param);
 
225
    }
219
226
#if defined(__ia64__) || defined(__ia64)
220
 
  /*
221
 
    Peculiar things with ia64 platforms - it seems we only have half the
222
 
    stack size in reality, so we have to double it here
223
 
  */
224
 
  pthread_attr_setstacksize(&thr_attr, my_thread_stack_size*2);
 
227
    /*
 
228
      Peculiar things with ia64 platforms - it seems we only have half the
 
229
      stack size in reality, so we have to double it here
 
230
    */
 
231
    pthread_attr_setstacksize(&thr_attr, my_thread_stack_size*2);
225
232
# else
226
 
  pthread_attr_setstacksize(&thr_attr, my_thread_stack_size);
 
233
    pthread_attr_setstacksize(&thr_attr, my_thread_stack_size);
227
234
#endif
228
235
 
229
 
  (void) pthread_mutex_lock(&LOCK_thread_count);
230
 
  if ((error=pthread_create(&signal_thread, &thr_attr, signal_hand, 0)))
231
 
  {
232
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("Can't create interrupt-thread (error %d, errno: %d)"),
 
236
    (void) pthread_mutex_lock(&LOCK_thread_count);
 
237
    if ((error=pthread_create(&signal_thread, &thr_attr, signal_hand, 0)))
 
238
    {
 
239
      errmsg_printf(ERRMSG_LVL_ERROR,
 
240
                    _("Can't create interrupt-thread (error %d, errno: %d)"),
233
241
                    error,errno);
234
 
    exit(1);
 
242
      exit(1);
 
243
    }
 
244
    (void) pthread_cond_wait(&COND_thread_count,&LOCK_thread_count);
 
245
    pthread_mutex_unlock(&LOCK_thread_count);
 
246
 
 
247
    (void) pthread_attr_destroy(&thr_attr);
235
248
  }
236
 
  (void) pthread_cond_wait(&COND_thread_count,&LOCK_thread_count);
237
 
  pthread_mutex_unlock(&LOCK_thread_count);
238
 
 
239
 
  (void) pthread_attr_destroy(&thr_attr);
240
 
 
241
 
  return 0;
242
 
}
243
 
 
244
 
/**
245
 
  This is mainly needed when running with purify, but it's still nice to
246
 
  know that all child threads have died when drizzled exits.
247
 
*/
248
 
static int deinit(drizzled::plugin::Registry&)
249
 
{
250
 
  uint32_t i;
251
 
  /*
252
 
    Wait up to 10 seconds for signal thread to die. We use this mainly to
253
 
    avoid getting warnings that internal::my_thread_end has not been called
 
249
 
 
250
  /**
 
251
    This is mainly needed when running with purify, but it's still nice to
 
252
    know that all child threads have died when drizzled exits.
254
253
  */
255
 
  for (i= 0 ; i < 100 && signal_thread_in_use; i++)
 
254
  ~SignalHandler()
256
255
  {
257
 
    if (pthread_kill(signal_thread, SIGTERM) != ESRCH)
258
 
      break;
259
 
    usleep(100);                                // Give it time to die
 
256
    uint32_t i;
 
257
    /*
 
258
      Wait up to 10 seconds for signal thread to die. We use this mainly to
 
259
      avoid getting warnings that internal::my_thread_end has not been called
 
260
    */
 
261
    for (i= 0 ; i < 100 && signal_thread_in_use; i++)
 
262
    {
 
263
      if (pthread_kill(signal_thread, SIGTERM) != ESRCH)
 
264
        break;
 
265
      usleep(100);                              // Give it time to die
 
266
    }
 
267
 
260
268
  }
 
269
};
261
270
 
 
271
static int init(drizzled::plugin::Context& context)
 
272
{
 
273
  SignalHandler *handler= new SignalHandler;
 
274
  context.add(handler);
262
275
  return 0;
263
276
}
264
277
 
 
278
 
265
279
static drizzle_sys_var* system_variables[]= {
266
280
  NULL
267
281
};
275
289
  "Default Signal Handler",
276
290
  PLUGIN_LICENSE_GPL,
277
291
  init, /* Plugin Init */
278
 
  deinit, /* Plugin Deinit */
279
292
  system_variables,   /* system variables */
280
293
  NULL    /* config options */
281
294
}