tst-sem11.c 1.4 KB

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