578
553
#endif /* USE_ALARM_THREAD */
580
555
#endif /* THREAD */
583
/****************************************************************************
584
Handling of test case (when compiled with -DMAIN)
585
***************************************************************************/
588
#if !defined(DONT_USE_THR_ALARM)
590
static pthread_cond_t COND_thread_count;
591
static pthread_mutex_t LOCK_thread_count;
592
static uint32_t thread_count;
595
typedef int * fd_set_ptr;
597
typedef fd_set * fd_set_ptr;
600
static void *test_thread(void *arg)
602
int i,param=*((int*) arg),wait_time,retry;
604
thr_alarm_t got_alarm;
608
printf("Thread %d (%s) started\n",param,my_thread_name()); fflush(stdout);
609
for (i=1 ; i <= 10 ; i++)
611
wait_time=param ? 11-i : i;
612
start_time= my_time(0);
613
if (thr_alarm(&got_alarm,wait_time,0))
615
printf("Thread: %s Alarms aborted\n",my_thread_name());
620
printf("Thread: %s Simulation of no alarm needed\n",my_thread_name());
625
for (retry=0 ; !thr_got_alarm(&got_alarm) && retry < 10 ; retry++)
627
printf("Thread: %s Waiting %d sec\n",my_thread_name(),wait_time);
628
select(0,(fd_set_ptr) &fd,0,0,0);
630
if (!thr_got_alarm(&got_alarm))
632
printf("Thread: %s didn't get an alarm. Aborting!\n",
637
{ /* Simulate alarm-miss */
639
uint32_t max_connection=fileno(stdin);
641
FD_SET(max_connection,&readFDs);
645
printf("Thread: %s Simulating alarm miss\n",my_thread_name());
647
if (select(max_connection+1, (fd_set_ptr) &readFDs,0,0,0) < 0)
650
break; /* Got new interrupt */
651
printf("Got errno: %d from select. Retrying..\n",errno);
654
printf("Warning: Interrupt of select() doesn't set errno!\n");
658
else /* This shouldn't happen */
660
if (!FD_ISSET(max_connection,&readFDs))
662
printf("Select interrupted, but errno not set\n");
668
getchar(); /* Somebody was playing */
673
printf("Thread: %s Slept for %d (%d) sec\n",my_thread_name(),
674
(int) (my_time(0)-start_time), wait_time); fflush(stdout);
675
thr_end_alarm(&got_alarm);
678
pthread_mutex_lock(&LOCK_thread_count);
680
pthread_cond_signal(&COND_thread_count); /* Tell main we are ready */
681
pthread_mutex_unlock(&LOCK_thread_count);
682
free((unsigned char*) arg);
686
#ifdef USE_ONE_SIGNAL_HAND
687
static RETSIGTYPE print_signal_warning(int sig)
689
printf("Warning: Got signal %d from thread %s\n",sig,my_thread_name());
691
#ifndef HAVE_BSD_SIGNALS
692
my_sigset(sig,print_signal_warning); /* int. thread system calls */
695
alarm(2); /* reschedule alarm */
697
#endif /* USE_ONE_SIGNAL_HAND */
700
static void *signal_hand(void *arg __attribute__((unused)))
703
int sig,error,err_count=0;;
706
pthread_detach_this_thread();
707
init_thr_alarm(10); /* Setup alarm handler */
708
pthread_mutex_lock(&LOCK_thread_count); /* Required by bsdi */
709
pthread_cond_signal(&COND_thread_count); /* Tell main we are ready */
710
pthread_mutex_unlock(&LOCK_thread_count);
712
sigemptyset(&set); /* Catch all signals */
713
sigaddset(&set,SIGINT);
714
sigaddset(&set,SIGQUIT);
715
sigaddset(&set,SIGTERM);
716
sigaddset(&set,SIGHUP);
718
sigaddset(&set,SIGTSTP);
720
#ifdef USE_ONE_SIGNAL_HAND
721
sigaddset(&set,THR_SERVER_ALARM); /* For alarms */
722
puts("Starting signal and alarm handling thread");
724
puts("Starting signal handling thread");
726
printf("server alarm: %d thread alarm: %d\n",
727
THR_SERVER_ALARM, thr_client_alarm);
730
while ((error=my_sigwait(&set,&sig)) == EINTR)
731
printf("sigwait restarted\n");
734
fprintf(stderr,"Got error %d from sigwait\n",error);
736
exit(1); /* Too many errors in test */
739
#ifdef USE_ONE_SIGNAL_HAND
740
if (sig != THR_SERVER_ALARM)
742
printf("Main thread: Got signal %d\n",sig);
748
printf("Aborting nicely\n");
753
printf("Aborting\n");
755
return 0; /* Keep some compilers happy */
757
#ifdef USE_ONE_SIGNAL_HAND
758
case THR_SERVER_ALARM:
767
int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
770
pthread_attr_t thr_attr;
773
ALARM_INFO alarm_info;
776
pthread_mutex_init(&LOCK_thread_count,MY_MUTEX_INIT_FAST);
777
pthread_cond_init(&COND_thread_count,NULL);
779
/* Start a alarm handling thread */
781
sigaddset(&set,SIGINT);
782
sigaddset(&set,SIGQUIT);
783
sigaddset(&set,SIGTERM);
784
sigaddset(&set,SIGHUP);
785
signal(SIGTERM,SIG_DFL); /* If it's blocked by parent */
787
sigaddset(&set,SIGTSTP);
789
sigaddset(&set,THR_SERVER_ALARM);
790
sigdelset(&set, thr_client_alarm);
791
(void) pthread_sigmask(SIG_SETMASK,&set,NULL);
794
sigaddset(&set, thr_client_alarm);
795
pthread_sigmask(SIG_UNBLOCK, &set, (sigset_t*) 0);
798
pthread_attr_init(&thr_attr);
799
pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_PROCESS);
800
pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED);
801
pthread_attr_setstacksize(&thr_attr,65536L);
803
/* Start signal thread and wait for it to start */
804
pthread_mutex_lock(&LOCK_thread_count);
805
pthread_create(&tid,&thr_attr,signal_hand,NULL);
806
pthread_cond_wait(&COND_thread_count,&LOCK_thread_count);
807
pthread_mutex_unlock(&LOCK_thread_count);
809
thr_setconcurrency(3);
810
pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_PROCESS);
811
printf("Main thread: %s\n",my_thread_name());
812
for (i=0 ; i < 2 ; i++)
814
param=(int*) malloc(sizeof(int));
816
pthread_mutex_lock(&LOCK_thread_count);
817
if ((error=pthread_create(&tid,&thr_attr,test_thread,(void*) param)))
819
printf("Can't create thread %d, error: %d\n",i,error);
823
pthread_mutex_unlock(&LOCK_thread_count);
826
pthread_attr_destroy(&thr_attr);
827
pthread_mutex_lock(&LOCK_thread_count);
828
thr_alarm_info(&alarm_info);
829
printf("Main_thread: Alarms: %u max_alarms: %u next_alarm_time: %lu\n",
830
alarm_info.active_alarms, alarm_info.max_used_alarms,
831
alarm_info.next_alarm_time);
834
pthread_cond_wait(&COND_thread_count,&LOCK_thread_count);
835
if (thread_count == 1)
837
printf("Calling end_thr_alarm. This should cancel the last thread\n");
841
pthread_mutex_unlock(&LOCK_thread_count);
842
thr_alarm_info(&alarm_info);
844
printf("Main_thread: Alarms: %u max_alarms: %u next_alarm_time: %lu\n",
845
alarm_info.active_alarms, alarm_info.max_used_alarms,
846
alarm_info.next_alarm_time);
847
printf("Test succeeded\n");
851
#else /* DONT_USE_THR_ALARM */
853
int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
855
printf("thr_alarm disabled with DONT_USE_THR_ALARM\n");
859
#endif /* DONT_USE_THR_ALARM */