Back to index

glibc  2.9
tst-timer3.c
Go to the documentation of this file.
00001 /* Test for bogus per-thread deletion of timers.  */
00002 
00003 #include <stdio.h>
00004 #include <error.h>
00005 #include <time.h>
00006 #include <signal.h>
00007 #include <stdint.h>
00008 #include <string.h>
00009 #include <sys/time.h>
00010 #include <sys/resource.h>
00011 #include <unistd.h>
00012 #if _POSIX_THREADS
00013 # include <pthread.h>
00014 
00015 
00016 /* Creating timers in another thread should work too.  */
00017 static void *
00018 do_timer_create (void *arg)
00019 {
00020   struct sigevent *const sigev = arg;
00021   timer_t *const timerId = sigev->sigev_value.sival_ptr;
00022   if (timer_create (CLOCK_REALTIME, sigev, timerId) < 0)
00023     {
00024       printf ("timer_create: %m\n");
00025       return NULL;
00026     }
00027   return timerId;
00028 }
00029 
00030 
00031 static int
00032 do_test (void)
00033 {
00034   int i, res;
00035   timer_t timerId;
00036   struct itimerspec itval;
00037   struct sigevent sigev;
00038 
00039   itval.it_interval.tv_sec = 2;
00040   itval.it_interval.tv_nsec = 0;
00041   itval.it_value.tv_sec = 2;
00042   itval.it_value.tv_nsec = 0;
00043 
00044   sigev.sigev_notify = SIGEV_SIGNAL;
00045   sigev.sigev_signo = SIGALRM;
00046   sigev.sigev_value.sival_ptr = (void *) &timerId;
00047 
00048   for (i = 0; i < 100; i++)
00049     {
00050       printf ("cnt = %d\n", i);
00051 
00052       pthread_t thr;
00053       res = pthread_create (&thr, NULL, &do_timer_create, &sigev);
00054       if (res)
00055        {
00056          printf ("pthread_create: %s\n", strerror (res));
00057          continue;
00058        }
00059       void *val;
00060       res = pthread_join (thr, &val);
00061       if (res)
00062        {
00063          printf ("pthread_join: %s\n", strerror (res));
00064          continue;
00065        }
00066       if (val == NULL)
00067        continue;
00068 
00069       res = timer_settime (timerId, 0, &itval, NULL);
00070       if (res < 0)
00071        printf ("timer_settime: %m\n");
00072 
00073       res = timer_delete (timerId);
00074       if (res < 0)
00075        printf ("timer_delete: %m\n");
00076     }
00077 
00078   return 0;
00079 }
00080 
00081 # define TEST_FUNCTION do_test ()
00082 #else
00083 # define TEST_FUNCTION 0
00084 #endif
00085 
00086 #include "../test-skeleton.c"