tst-timer3.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Test for bogus per-thread deletion of timers. */
  2. #include <stdio.h>
  3. #include <error.h>
  4. #include <time.h>
  5. #include <signal.h>
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include <sys/time.h>
  9. #include <sys/resource.h>
  10. #include <unistd.h>
  11. #if _POSIX_THREADS
  12. # include <pthread.h>
  13. /* Creating timers in another thread should work too. */
  14. static void *
  15. do_timer_create (void *arg)
  16. {
  17. struct sigevent *const sigev = arg;
  18. timer_t *const timerId = sigev->sigev_value.sival_ptr;
  19. if (timer_create (CLOCK_REALTIME, sigev, timerId) < 0)
  20. {
  21. printf ("timer_create: %m\n");
  22. return NULL;
  23. }
  24. return timerId;
  25. }
  26. static int
  27. do_test (void)
  28. {
  29. int i, res;
  30. timer_t timerId;
  31. struct itimerspec itval;
  32. struct sigevent sigev;
  33. itval.it_interval.tv_sec = 2;
  34. itval.it_interval.tv_nsec = 0;
  35. itval.it_value.tv_sec = 2;
  36. itval.it_value.tv_nsec = 0;
  37. sigev.sigev_notify = SIGEV_SIGNAL;
  38. sigev.sigev_signo = SIGALRM;
  39. sigev.sigev_value.sival_ptr = (void *) &timerId;
  40. for (i = 0; i < 100; i++)
  41. {
  42. printf ("cnt = %d\n", i);
  43. pthread_t thr;
  44. res = pthread_create (&thr, NULL, &do_timer_create, &sigev);
  45. if (res)
  46. {
  47. printf ("pthread_create: %s\n", strerror (res));
  48. continue;
  49. }
  50. void *val;
  51. res = pthread_join (thr, &val);
  52. if (res)
  53. {
  54. printf ("pthread_join: %s\n", strerror (res));
  55. continue;
  56. }
  57. if (val == NULL)
  58. continue;
  59. res = timer_settime (timerId, 0, &itval, NULL);
  60. if (res < 0)
  61. printf ("timer_settime: %m\n");
  62. res = timer_delete (timerId);
  63. if (res < 0)
  64. printf ("timer_delete: %m\n");
  65. }
  66. return 0;
  67. }
  68. # define TEST_FUNCTION do_test ()
  69. #else
  70. # define TEST_FUNCTION 0
  71. #endif
  72. #include "../test-skeleton.c"