~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/thr_alarm.cc

  • Committer: Toru Maesaka
  • Date: 2008-12-17 07:16:37 UTC
  • mto: (685.1.40 devel) (713.1.5 devel)
  • mto: This revision was merged to the branch mainline in revision 713.
  • Revision ID: dev@torum.net-20081217071637-7j9040w7lpms77r2
Removed my_time() and added error checking

Show diffs side-by-side

added added

removed removed

Lines of Context:
164
164
  bool reschedule;
165
165
  struct st_my_thread_var *current_my_thread_var= my_thread_var;
166
166
 
167
 
  now= my_time(0);
 
167
  if((now= time(0)) == (time_t)-1)
 
168
  {
 
169
    fprintf(stderr, "%s: Warning: time() call failed\n", my_progname);
 
170
    return 1;
 
171
  }
 
172
 
168
173
#ifndef USE_ONE_SIGNAL_HAND
169
174
  pthread_sigmask(SIG_BLOCK,&full_signal_set,&old_mask);
170
175
#endif
328
333
      uint32_t i;
329
334
      for (i=0 ; i < alarm_queue.elements ;)
330
335
      {
331
 
        alarm_data=(ALARM*) queue_element(&alarm_queue,i);
332
 
        alarm_data->alarmed=1;                  /* Info to thread */
333
 
        if (pthread_equal(alarm_data->thread,alarm_thread) ||
334
 
            pthread_kill(alarm_data->thread, thr_client_alarm))
335
 
        {
336
 
          queue_remove(&alarm_queue,i);         /* No thread. Remove alarm */
337
 
        }
338
 
        else
339
 
          i++;                                  /* Signal next thread */
 
336
        alarm_data=(ALARM*) queue_element(&alarm_queue,i);
 
337
        alarm_data->alarmed=1;                  /* Info to thread */
 
338
        if (pthread_equal(alarm_data->thread,alarm_thread) ||
 
339
            pthread_kill(alarm_data->thread, thr_client_alarm))
 
340
        {
 
341
          queue_remove(&alarm_queue,i);         /* No thread. Remove alarm */
 
342
        }
 
343
        else
 
344
          i++;                                  /* Signal next thread */
340
345
      }
341
346
#ifndef USE_ALARM_THREAD
342
347
      if (alarm_queue.elements)
343
 
        alarm(1);                               /* Signal soon again */
 
348
        alarm(1);                               /* Signal soon again */
344
349
#endif
345
350
    }
346
351
    else
347
352
    {
348
 
      uint32_t now=(uint32_t) my_time(0);
349
 
      uint32_t next=now+10-(now%10);
 
353
      uint32_t now= time(0);
 
354
      uint32_t next= now+10-(now%10);
350
355
      while ((alarm_data=(ALARM*) queue_top(&alarm_queue))->expire_time <= now)
351
356
      {
352
 
        alarm_data->alarmed=1;                  /* Info to thread */
353
 
        if (pthread_equal(alarm_data->thread,alarm_thread) ||
354
 
            pthread_kill(alarm_data->thread, thr_client_alarm))
355
 
        {
356
 
          queue_remove(&alarm_queue,0);         /* No thread. Remove alarm */
357
 
          if (!alarm_queue.elements)
358
 
            break;
359
 
        }
360
 
        else
361
 
        {
362
 
          alarm_data->expire_time=next;
363
 
          queue_replaced(&alarm_queue);
364
 
        }
 
357
        alarm_data->alarmed=1;                  /* Info to thread */
 
358
        if (pthread_equal(alarm_data->thread,alarm_thread) ||
 
359
            pthread_kill(alarm_data->thread, thr_client_alarm))
 
360
        {
 
361
          queue_remove(&alarm_queue,0);         /* No thread. Remove alarm */
 
362
          if (!alarm_queue.elements)
 
363
            break;
 
364
        }
 
365
        else
 
366
        {
 
367
          alarm_data->expire_time=next;
 
368
          queue_replaced(&alarm_queue);
 
369
        }
365
370
      }
366
371
#ifndef USE_ALARM_THREAD
367
372
      if (alarm_queue.elements)
368
373
      {
369
 
        alarm((uint) (alarm_data->expire_time-now));
 
374
        alarm((uint) (alarm_data->expire_time-now));
370
375
        next_alarm_expire_time= alarm_data->expire_time;
371
376
      }
372
377
#endif
473
478
  info->max_used_alarms= max_used_alarms;
474
479
  if ((info->active_alarms=  alarm_queue.elements))
475
480
  {
476
 
    uint32_t now=(uint32_t) my_time(0);
 
481
    uint32_t now= (uint32_t)time(0);
477
482
    long time_diff;
478
483
    ALARM *alarm_data= (ALARM*) queue_top(&alarm_queue);
479
484
    time_diff= (long) (alarm_data->expire_time - now);
501
506
#define tv_nsec ts_nsec
502
507
#endif
503
508
 
504
 
/* set up a alarm thread with uses 'sleep' to sleep between alarms */
 
509
/* 
 
510
   Set up a alarm thread which uses 'sleep' to sleep between alarms
 
511
 
 
512
  RETURNS
 
513
    NULL on time() failure
 
514
*/
505
515
 
506
516
#ifdef USE_ALARM_THREAD
507
517
static void *alarm_handler(void *arg __attribute__((unused)))
515
525
  {
516
526
    if (alarm_queue.elements)
517
527
    {
518
 
      uint32_t sleep_time,now= my_time(0);
 
528
      uint32_t sleep_time, now;
 
529
 
 
530
      if ((now= time(0)) == (time_t)-1)
 
531
      {
 
532
        pthread_mutex_unlock(&LOCK_alarm);
 
533
        return NULL;
 
534
      }
 
535
 
519
536
      if (alarm_aborted)
520
 
        sleep_time=now+1;
 
537
        sleep_time=now+1;
521
538
      else
522
 
        sleep_time= ((ALARM*) queue_top(&alarm_queue))->expire_time;
 
539
        sleep_time= ((ALARM*) queue_top(&alarm_queue))->expire_time;
523
540
      if (sleep_time > now)
524
541
      {
525
 
        abstime.tv_sec=sleep_time;
526
 
        abstime.tv_nsec=0;
 
542
        abstime.tv_sec=sleep_time;
 
543
        abstime.tv_nsec=0;
527
544
        next_alarm_expire_time= sleep_time;
528
 
        if ((error=pthread_cond_timedwait(&COND_alarm,&LOCK_alarm,&abstime)) &&
529
 
            error != ETIME && error != ETIMEDOUT)
530
 
        {
 
545
        if ((error=pthread_cond_timedwait(&COND_alarm,&LOCK_alarm,&abstime)) &&
 
546
            error != ETIME && error != ETIMEDOUT)
 
547
        {
531
548
          assert(1);
532
 
        }
 
549
        }
533
550
      }
534
551
    }
535
552
    else if (alarm_aborted == -1)