tst-sem11.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <semaphore.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <internaltypes.h>
  6. #ifndef SEM_WAIT
  7. # define SEM_WAIT(s) sem_wait (s)
  8. #endif
  9. static void *
  10. tf (void *arg)
  11. {
  12. #ifdef PREPARE
  13. PREPARE
  14. #endif
  15. SEM_WAIT (arg);
  16. return NULL;
  17. }
  18. int
  19. main (void)
  20. {
  21. int tries = 5;
  22. pthread_t th;
  23. sem_t s;
  24. again:
  25. if (sem_init (&s, 0, 0) != 0)
  26. {
  27. puts ("sem_init failed");
  28. return 1;
  29. }
  30. struct new_sem *is = (struct new_sem *) &s;
  31. if (is->nwaiters != 0)
  32. {
  33. puts ("nwaiters not initialized");
  34. return 1;
  35. }
  36. if (pthread_create (&th, NULL, tf, &s) != 0)
  37. {
  38. puts ("pthread_create failed");
  39. return 1;
  40. }
  41. sleep (1);
  42. if (pthread_cancel (th) != 0)
  43. {
  44. puts ("pthread_cancel failed");
  45. return 1;
  46. }
  47. void *r;
  48. if (pthread_join (th, &r) != 0)
  49. {
  50. puts ("pthread_join failed");
  51. return 1;
  52. }
  53. if (r != PTHREAD_CANCELED && --tries > 0)
  54. {
  55. /* Maybe we get the scheduling right the next time. */
  56. sem_destroy (&s);
  57. goto again;
  58. }
  59. if (is->nwaiters != 0)
  60. {
  61. puts ("nwaiters not reset");
  62. return 1;
  63. }
  64. return 0;
  65. }