condvar3_1.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * File: condvar3_1.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-win32 - POSIX Threads Library for Win32
  8. * Copyright(C) 1998 John E. Bossom
  9. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * The current list of contributors is contained
  14. * in the file CONTRIBUTORS included with the source
  15. * code distribution. The list can also be seen at the
  16. * following World Wide Web location:
  17. * http://sources.redhat.com/pthreads-win32/contributors.html
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library in the file COPYING.LIB;
  31. * if not, write to the Free Software Foundation, Inc.,
  32. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * Test Synopsis:
  37. * - Test timeout of multiple waits on a CV with some signaled.
  38. *
  39. * Test Method (Validation or Falsification):
  40. * - Validation
  41. *
  42. * Requirements Tested:
  43. * -
  44. *
  45. * Features Tested:
  46. * -
  47. *
  48. * Cases Tested:
  49. * -
  50. *
  51. * Description:
  52. * - Because some CVs are never signaled, we expect their waits to time out.
  53. * Some are signaled, the rest time out. Pthread_cond_destroy() will fail
  54. * unless all are accounted for, either signaled or timedout.
  55. *
  56. * Environment:
  57. * -
  58. *
  59. * Input:
  60. * - None.
  61. *
  62. * Output:
  63. * - File name, Line number, and failed expression on failure.
  64. * - No output on success.
  65. *
  66. * Assumptions:
  67. * -
  68. *
  69. * Pass Criteria:
  70. * - pthread_cond_timedwait returns ETIMEDOUT.
  71. * - Process returns zero exit status.
  72. *
  73. * Fail Criteria:
  74. * - pthread_cond_timedwait does not return ETIMEDOUT.
  75. * - Process returns non-zero exit status.
  76. */
  77. #define _WIN32_WINNT 0x400
  78. #include "test.h"
  79. #include <sys/timeb.h>
  80. static pthread_cond_t cv;
  81. static pthread_cond_t cv1;
  82. static pthread_mutex_t mutex;
  83. static pthread_mutex_t mutex1;
  84. static struct timespec abstime = { 0, 0 };
  85. static int timedout = 0;
  86. static int signaled = 0;
  87. static int awoken = 0;
  88. static int waiting = 0;
  89. enum {
  90. NUMTHREADS = 30
  91. };
  92. void *
  93. mythread(void * arg)
  94. {
  95. int result;
  96. assert(pthread_mutex_lock(&mutex1) == 0);
  97. ++waiting;
  98. assert(pthread_mutex_unlock(&mutex1) == 0);
  99. assert(pthread_cond_signal(&cv1) == 0);
  100. assert(pthread_mutex_lock(&mutex) == 0);
  101. result = pthread_cond_timedwait(&cv, &mutex, &abstime);
  102. if (result == ETIMEDOUT)
  103. {
  104. timedout++;
  105. }
  106. else
  107. {
  108. awoken++;
  109. }
  110. assert(pthread_mutex_unlock(&mutex) == 0);
  111. return arg;
  112. }
  113. #include "../implement.h"
  114. int
  115. main()
  116. {
  117. int i;
  118. pthread_t t[NUMTHREADS + 1];
  119. void* result = (void*)0;
  120. PTW32_STRUCT_TIMEB currSysTime;
  121. const DWORD NANOSEC_PER_MILLISEC = 1000000;
  122. assert(pthread_cond_init(&cv, NULL) == 0);
  123. assert(pthread_cond_init(&cv1, NULL) == 0);
  124. assert(pthread_mutex_init(&mutex, NULL) == 0);
  125. assert(pthread_mutex_init(&mutex1, NULL) == 0);
  126. /* get current system time */
  127. PTW32_FTIME(&currSysTime);
  128. abstime.tv_sec = (long)currSysTime.time;
  129. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  130. abstime.tv_sec += 5;
  131. assert(pthread_mutex_lock(&mutex1) == 0);
  132. for (i = 1; i <= NUMTHREADS; i++)
  133. {
  134. assert(pthread_create(&t[i], NULL, mythread, (void *)(size_t)i) == 0);
  135. }
  136. do {
  137. assert(pthread_cond_wait(&cv1,&mutex1) == 0);
  138. } while ( NUMTHREADS > waiting );
  139. assert(pthread_mutex_unlock(&mutex1) == 0);
  140. for (i = NUMTHREADS/3; i <= 2*NUMTHREADS/3; i++)
  141. {
  142. // assert(pthread_mutex_lock(&mutex) == 0);
  143. assert(pthread_cond_signal(&cv) == 0);
  144. // assert(pthread_mutex_unlock(&mutex) == 0);
  145. signaled++;
  146. }
  147. for (i = 1; i <= NUMTHREADS; i++)
  148. {
  149. assert(pthread_join(t[i], &result) == 0);
  150. assert((int)(size_t)result == i);
  151. }
  152. fprintf(stderr, "awk = %d\n", awoken);
  153. fprintf(stderr, "sig = %d\n", signaled);
  154. fprintf(stderr, "tot = %d\n", timedout);
  155. assert(signaled == awoken);
  156. assert(timedout == NUMTHREADS - signaled);
  157. assert(pthread_cond_destroy(&cv1) == 0);
  158. {
  159. int result = pthread_cond_destroy(&cv);
  160. if (result != 0)
  161. {
  162. fprintf(stderr, "Result = %s\n", error_string[result]);
  163. fprintf(stderr, "\tWaitersBlocked = %ld\n", cv->nWaitersBlocked);
  164. fprintf(stderr, "\tWaitersGone = %ld\n", cv->nWaitersGone);
  165. fprintf(stderr, "\tWaitersToUnblock = %ld\n", cv->nWaitersToUnblock);
  166. fflush(stderr);
  167. }
  168. assert(result == 0);
  169. }
  170. assert(pthread_mutex_destroy(&mutex1) == 0);
  171. assert(pthread_mutex_destroy(&mutex) == 0);
  172. return 0;
  173. }