tst-cond23.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0
  21. static int
  22. check (pthread_condattr_t *condattr, int pshared, clockid_t cl)
  23. {
  24. clockid_t cl2;
  25. if (pthread_condattr_getclock (condattr, &cl2) != 0)
  26. {
  27. puts ("condattr_getclock failed");
  28. return 1;
  29. }
  30. if (cl != cl2)
  31. {
  32. printf ("condattr_getclock returned wrong value: %d, expected %d\n",
  33. (int) cl2, (int) cl);
  34. return 1;
  35. }
  36. int p;
  37. if (pthread_condattr_getpshared (condattr, &p) != 0)
  38. {
  39. puts ("condattr_getpshared failed");
  40. return 1;
  41. }
  42. else if (p != pshared)
  43. {
  44. printf ("condattr_getpshared returned wrong value: %d, expected %d\n",
  45. p, pshared);
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. static int
  51. run_test (clockid_t cl)
  52. {
  53. pthread_condattr_t condattr;
  54. printf ("clock = %d\n", (int) cl);
  55. if (pthread_condattr_init (&condattr) != 0)
  56. {
  57. puts ("condattr_init failed");
  58. return 1;
  59. }
  60. if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME))
  61. return 1;
  62. if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0)
  63. {
  64. puts ("1st condattr_setpshared failed");
  65. return 1;
  66. }
  67. if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME))
  68. return 1;
  69. if (pthread_condattr_setclock (&condattr, cl) != 0)
  70. {
  71. puts ("1st condattr_setclock failed");
  72. return 1;
  73. }
  74. if (check (&condattr, PTHREAD_PROCESS_SHARED, cl))
  75. return 1;
  76. if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_PRIVATE) != 0)
  77. {
  78. puts ("2nd condattr_setpshared failed");
  79. return 1;
  80. }
  81. if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl))
  82. return 1;
  83. if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0)
  84. {
  85. puts ("2nd condattr_setclock failed");
  86. return 1;
  87. }
  88. if (check (&condattr, PTHREAD_PROCESS_PRIVATE, CLOCK_REALTIME))
  89. return 1;
  90. if (pthread_condattr_setclock (&condattr, cl) != 0)
  91. {
  92. puts ("3rd condattr_setclock failed");
  93. return 1;
  94. }
  95. if (check (&condattr, PTHREAD_PROCESS_PRIVATE, cl))
  96. return 1;
  97. if (pthread_condattr_setpshared (&condattr, PTHREAD_PROCESS_SHARED) != 0)
  98. {
  99. puts ("3rd condattr_setpshared failed");
  100. return 1;
  101. }
  102. if (check (&condattr, PTHREAD_PROCESS_SHARED, cl))
  103. return 1;
  104. if (pthread_condattr_setclock (&condattr, CLOCK_REALTIME) != 0)
  105. {
  106. puts ("4th condattr_setclock failed");
  107. return 1;
  108. }
  109. if (check (&condattr, PTHREAD_PROCESS_SHARED, CLOCK_REALTIME))
  110. return 1;
  111. if (pthread_condattr_destroy (&condattr) != 0)
  112. {
  113. puts ("condattr_destroy failed");
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. #endif
  119. static int
  120. do_test (void)
  121. {
  122. #if !defined _POSIX_CLOCK_SELECTION || _POSIX_CLOCK_SELECTION == -1
  123. puts ("_POSIX_CLOCK_SELECTION not supported, test skipped");
  124. return 0;
  125. #else
  126. int res = run_test (CLOCK_REALTIME);
  127. # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
  128. # if _POSIX_MONOTONIC_CLOCK == 0
  129. int e = sysconf (_SC_MONOTONIC_CLOCK);
  130. if (e < 0)
  131. puts ("CLOCK_MONOTONIC not supported");
  132. else if (e == 0)
  133. {
  134. puts ("sysconf (_SC_MONOTONIC_CLOCK) must not return 0");
  135. res = 1;
  136. }
  137. else
  138. # endif
  139. res |= run_test (CLOCK_MONOTONIC);
  140. # else
  141. puts ("_POSIX_MONOTONIC_CLOCK not defined");
  142. # endif
  143. return res;
  144. #endif
  145. }
  146. #define TEST_FUNCTION do_test ()
  147. #include "../test-skeleton.c"