tst-sem11.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. union
  24. {
  25. sem_t s;
  26. struct new_sem ns;
  27. } u;
  28. again:
  29. if (sem_init (&u.s, 0, 0) != 0)
  30. {
  31. puts ("sem_init failed");
  32. return 1;
  33. }
  34. #if __HAVE_64B_ATOMICS
  35. if ((u.ns.data >> SEM_NWAITERS_SHIFT) != 0)
  36. #else
  37. if (u.ns.nwaiters != 0)
  38. #endif
  39. {
  40. puts ("nwaiters not initialized");
  41. return 1;
  42. }
  43. if (pthread_create (&th, NULL, tf, &u.s) != 0)
  44. {
  45. puts ("pthread_create failed");
  46. return 1;
  47. }
  48. sleep (1);
  49. if (pthread_cancel (th) != 0)
  50. {
  51. puts ("pthread_cancel failed");
  52. return 1;
  53. }
  54. void *r;
  55. if (pthread_join (th, &r) != 0)
  56. {
  57. puts ("pthread_join failed");
  58. return 1;
  59. }
  60. if (r != PTHREAD_CANCELED && --tries > 0)
  61. {
  62. /* Maybe we get the scheduling right the next time. */
  63. sem_destroy (&u.s);
  64. goto again;
  65. }
  66. #if __HAVE_64B_ATOMICS
  67. if ((u.ns.data >> SEM_NWAITERS_SHIFT) != 0)
  68. #else
  69. if (u.ns.nwaiters != 0)
  70. #endif
  71. {
  72. puts ("nwaiters not reset");
  73. return 1;
  74. }
  75. return 0;
  76. }