tst-cond23.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Copyright (C) 2008 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>, 2008.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0
  22. static int
  23. check (pthread_condattr_t *condattr, int pshared, clockid_t cl)
  24. {
  25. clockid_t cl2;
  26. if (pthread_condattr_getclock (condattr, &cl2) != 0)
  27. {
  28. puts ("condattr_getclock failed");
  29. return 1;
  30. }
  31. if (cl != cl2)
  32. {
  33. printf ("condattr_getclock returned wrong value: %d, expected %d\n",
  34. (int) cl2, (int) cl);
  35. return 1;
  36. }
  37. int p;
  38. if (pthread_condattr_getpshared (condattr, &p) != 0)
  39. {
  40. puts ("condattr_getpshared failed");
  41. return 1;
  42. }
  43. else if (p != pshared)
  44. {
  45. printf ("condattr_getpshared returned wrong value: %d, expected %d\n",
  46. p, pshared);
  47. return 1;
  48. }
  49. return 0;
  50. }
  51. static int
  52. run_test (clockid_t cl)
  53. {
  54. pthread_condattr_t condattr;
  55. printf ("clock = %d\n", (int) cl);
  56. if (pthread_condattr_init (&condattr) != 0)
  57. {
  58. puts ("condattr_init failed");
  59. return 1;
  60. }
  61. if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME))
  62. return 1;
  63. if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0)
  64. {
  65. puts ("1st condattr_setpshared failed");
  66. return 1;
  67. }
  68. if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME))
  69. return 1;
  70. if (pthread_condattr_setclock (&condattr, cl) != 0)
  71. {
  72. puts ("1st condattr_setclock failed");
  73. return 1;
  74. }
  75. if (check (&condattr, PTHREAD_PROCESS_SHARED, cl))
  76. return 1;
  77. if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_PRIVATE) != 0)
  78. {
  79. puts ("2nd condattr_setpshared failed");
  80. return 1;
  81. }
  82. if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl))
  83. return 1;
  84. if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0)
  85. {
  86. puts ("2nd condattr_setclock failed");
  87. return 1;
  88. }
  89. if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME))
  90. return 1;
  91. if (pthread_condattr_setclock (&condattr, cl) != 0)
  92. {
  93. puts ("3rd condattr_setclock failed");
  94. return 1;
  95. }
  96. if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl))
  97. return 1;
  98. if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0)
  99. {
  100. puts ("3rd condattr_setpshared failed");
  101. return 1;
  102. }
  103. if (check (&condattr, PTHREAD_PROCESS_SHARED, cl))
  104. return 1;
  105. if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0)
  106. {
  107. puts ("4th condattr_setclock failed");
  108. return 1;
  109. }
  110. if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME))
  111. return 1;
  112. if (pthread_condattr_destroy (&condattr) != 0)
  113. {
  114. puts ("condattr_destroy failed");
  115. return 1;
  116. }
  117. return 0;
  118. }
  119. #endif
  120. static int
  121. do_test (void)
  122. {
  123. #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
  124. puts ("_POSIX_CLOCK_SELECTION not supported, test skipped");
  125. return 0;
  126. #else
  127. int res = run_test (CLOCK_REALTIME);
  128. # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
  129. # if _POSIX_MONOTONIC_CLOCK == 0
  130. int e = sysconf (_SC_MONOTONIC_CLOCK);
  131. if (e < 0)
  132. puts ("CLOCK_MONOTONIC not supported");
  133. else if (e == 0)
  134. {
  135. puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
  136. res = 1;
  137. }
  138. else
  139. # endif
  140. res |= run_test (CLOCK_MONOTONIC);
  141. # else
  142. puts ("_POSIX_MONOTONIC_CLOCK not defined");
  143. # endif
  144. return res;
  145. #endif
  146. }
  147. #define TEST_FUNCTION do_test ()
  148. #include "../test-skeleton.c"