~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/thr_alarm.c

  • Committer: Jay Pipes
  • Date: 2008-07-17 20:11:46 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080717201146-s2flcwwq0q89ac1y
Phase 3 removal of DBUG in mysys

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
/* To avoid problems with alarms in debug code, we disable DBUG here */
17
 
#define FORCE_DBUG_OFF
18
16
#include <my_global.h>
19
17
 
20
18
#if !defined(DONT_USE_THR_ALARM)
67
65
void init_thr_alarm(uint max_alarms)
68
66
{
69
67
  sigset_t s;
70
 
  DBUG_ENTER("init_thr_alarm");
71
68
  alarm_aborted=0;
72
69
  next_alarm_expire_time= ~ (time_t) 0;
73
70
  init_queue(&alarm_queue,max_alarms+1,offsetof(ALARM,expire_time),0,
111
108
  my_sigset(THR_SERVER_ALARM, process_alarm);
112
109
  pthread_sigmask(SIG_UNBLOCK, &s, NULL);
113
110
#endif /* USE_ALARM_THREAD */
114
 
  DBUG_VOID_RETURN;
 
111
  return;
115
112
}
116
113
 
117
114
 
155
152
#endif
156
153
  bool reschedule;
157
154
  struct st_my_thread_var *current_my_thread_var= my_thread_var;
158
 
  DBUG_ENTER("thr_alarm");
159
 
  DBUG_PRINT("enter",("thread: %s  sec: %d",my_thread_name(),sec));
160
155
 
161
156
  now= my_time(0);
162
157
#ifndef USE_ONE_SIGNAL_HAND
165
160
  pthread_mutex_lock(&LOCK_alarm);        /* Lock from threads & alarms */
166
161
  if (alarm_aborted > 0)
167
162
  {                                     /* No signal thread */
168
 
    DBUG_PRINT("info", ("alarm aborted"));
169
163
    *alrm= 0;                                   /* No alarm */
170
164
    pthread_mutex_unlock(&LOCK_alarm);
171
165
#ifndef USE_ONE_SIGNAL_HAND
172
166
    pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
173
167
#endif
174
 
    DBUG_RETURN(1);
 
168
    return(1);
175
169
  }
176
170
  if (alarm_aborted < 0)
177
171
    sec= 1;                                     /* Abort mode */
180
174
  {
181
175
    if (alarm_queue.elements == alarm_queue.max_elements)
182
176
    {
183
 
      DBUG_PRINT("info", ("alarm queue full"));
184
177
      fprintf(stderr,"Warning: thr_alarm queue is full\n");
185
178
      *alrm= 0;                                 /* No alarm */
186
179
      pthread_mutex_unlock(&LOCK_alarm);
187
180
#ifndef USE_ONE_SIGNAL_HAND
188
181
      pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
189
182
#endif
190
 
      DBUG_RETURN(1);
 
183
      return(1);
191
184
    }
192
185
    max_used_alarms=alarm_queue.elements+1;
193
186
  }
196
189
  {
197
190
    if (!(alarm_data=(ALARM*) my_malloc(sizeof(ALARM),MYF(MY_WME))))
198
191
    {
199
 
      DBUG_PRINT("info", ("failed my_malloc()"));
200
192
      *alrm= 0;                                 /* No alarm */
201
193
      pthread_mutex_unlock(&LOCK_alarm);
202
194
#ifndef USE_ONE_SIGNAL_HAND
203
195
      pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
204
196
#endif
205
 
      DBUG_RETURN(1);
 
197
      return(1);
206
198
    }
207
199
    alarm_data->malloced=1;
208
200
  }
217
209
  /* Reschedule alarm if the current one has more than sec left */
218
210
  if (reschedule)
219
211
  {
220
 
    DBUG_PRINT("info", ("reschedule"));
221
212
    if (pthread_equal(pthread_self(),alarm_thread))
222
213
    {
223
214
      alarm(sec);                               /* purecov: inspected */
231
222
  pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
232
223
#endif
233
224
  (*alrm)= &alarm_data->alarmed;
234
 
  DBUG_RETURN(0);
 
225
  return(0);
235
226
}
236
227
 
237
228
 
246
237
  sigset_t old_mask;
247
238
#endif
248
239
  uint i, found=0;
249
 
  DBUG_ENTER("thr_end_alarm");
250
240
 
251
241
#ifndef USE_ONE_SIGNAL_HAND
252
242
  pthread_sigmask(SIG_BLOCK,&full_signal_set,&old_mask);
262
252
      if (alarm_data->malloced)
263
253
        my_free((uchar*) alarm_data,MYF(0));
264
254
      found++;
265
 
#ifdef DBUG_OFF
266
255
      break;
267
 
#endif
268
256
    }
269
257
  }
270
 
  DBUG_ASSERT(!*alarmed || found == 1);
 
258
  assert(!*alarmed || found == 1);
271
259
  if (!found)
272
260
  {
273
261
    if (*alarmed)
274
262
      fprintf(stderr,"Warning: Didn't find alarm 0x%lx in queue of %d alarms\n",
275
263
              (long) *alarmed, alarm_queue.elements);
276
 
    DBUG_PRINT("warning",("Didn't find alarm 0x%lx in queue\n",
277
 
                          (long) *alarmed));
278
264
  }
279
265
  pthread_mutex_unlock(&LOCK_alarm);
280
266
#ifndef USE_ONE_SIGNAL_HAND
281
267
  pthread_sigmask(SIG_SETMASK,&old_mask,NULL);
282
268
#endif
283
 
  DBUG_VOID_RETURN;
 
269
  return;
284
270
}
285
271
 
286
272
/*
294
280
sig_handler process_alarm(int sig __attribute__((unused)))
295
281
{
296
282
  sigset_t old_mask;
297
 
/*
298
 
  This must be first as we can't call DBUG inside an alarm for a normal thread
299
 
*/
300
283
 
301
284
  if (thd_lib_detected == THD_LIB_LT &&
302
285
      !pthread_equal(pthread_self(),alarm_thread))
310
293
    return;
311
294
  }
312
295
 
313
 
  /*
314
 
    We have to do do the handling of the alarm in a sub function,
315
 
    because otherwise we would get problems with two threads calling
316
 
    DBUG_... functions at the same time (as two threads may call
317
 
    process_alarm() at the same time
318
 
  */
319
 
 
320
296
#ifndef USE_ALARM_THREAD
321
297
  pthread_sigmask(SIG_SETMASK,&full_signal_set,&old_mask);
322
298
  pthread_mutex_lock(&LOCK_alarm);
336
312
static sig_handler process_alarm_part2(int sig __attribute__((unused)))
337
313
{
338
314
  ALARM *alarm_data;
339
 
  DBUG_ENTER("process_alarm");
340
 
  DBUG_PRINT("info",("sig: %d  active alarms: %d",sig,alarm_queue.elements));
341
315
 
342
316
#if defined(MAIN)
343
317
  printf("process_alarm\n"); fflush(stdout);
374
348
      while ((alarm_data=(ALARM*) queue_top(&alarm_queue))->expire_time <= now)
375
349
      {
376
350
        alarm_data->alarmed=1;                  /* Info to thread */
377
 
        DBUG_PRINT("info",("sending signal to waiting thread"));
378
351
        if (pthread_equal(alarm_data->thread,alarm_thread) ||
379
352
            pthread_kill(alarm_data->thread, thr_client_alarm))
380
353
        {
407
380
    */
408
381
    next_alarm_expire_time= ~(time_t) 0;
409
382
  }
410
 
  DBUG_VOID_RETURN;
 
383
  return;
411
384
}
412
385
 
413
386
 
428
401
 
429
402
void end_thr_alarm(bool free_structures)
430
403
{
431
 
  DBUG_ENTER("end_thr_alarm");
432
404
  if (alarm_aborted != 1)                       /* If memory not freed */
433
405
  {
434
406
    pthread_mutex_lock(&LOCK_alarm);
435
 
    DBUG_PRINT("info",("Resheduling %d waiting alarms",alarm_queue.elements));
436
407
    alarm_aborted= -1;                          /* mark aborted */
437
408
    if (alarm_queue.elements || (alarm_thread_running && free_structures))
438
409
    {
445
416
    {
446
417
      struct timespec abstime;
447
418
 
448
 
      DBUG_ASSERT(!alarm_queue.elements);
 
419
      assert(!alarm_queue.elements);
449
420
 
450
421
      /* Wait until alarm thread dies */
451
422
      set_timespec(abstime, 10);                /* Wait up to 10 seconds */
467
438
    else
468
439
      pthread_mutex_unlock(&LOCK_alarm);
469
440
  }
470
 
  DBUG_VOID_RETURN;
 
441
  return;
471
442
}
472
443
 
473
444
 
743
714
#endif
744
715
  printf("server alarm: %d  thread alarm: %d\n",
745
716
         THR_SERVER_ALARM, thr_client_alarm);
746
 
  DBUG_PRINT("info",("Starting signal and alarm handling thread"));
747
717
  for(;;)
748
718
  {
749
719
    while ((error=my_sigwait(&set,&sig)) == EINTR)
792
762
  ALARM_INFO alarm_info;
793
763
  MY_INIT(argv[0]);
794
764
 
795
 
  if (argc > 1 && argv[1][0] == '-' && argv[1][1] == '#')
796
 
  {
797
 
    DBUG_PUSH(argv[1]+2);
798
 
  }
799
765
  pthread_mutex_init(&LOCK_thread_count,MY_MUTEX_INIT_FAST);
800
766
  pthread_cond_init(&COND_thread_count,NULL);
801
767
 
828
794
  pthread_create(&tid,&thr_attr,signal_hand,NULL);
829
795
  VOID(pthread_cond_wait(&COND_thread_count,&LOCK_thread_count));
830
796
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
831
 
  DBUG_PRINT("info",("signal thread created"));
832
797
 
833
798
  thr_setconcurrency(3);
834
799
  pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_PROCESS);