condvar4.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * File: condvar4.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 PTHREAD_COND_INITIALIZER.
  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. * - Test basic CV function but starting with a static initialised
  53. * CV.
  54. *
  55. * Environment:
  56. * -
  57. *
  58. * Input:
  59. * - None.
  60. *
  61. * Output:
  62. * - File name, Line number, and failed expression on failure.
  63. * - No output on success.
  64. *
  65. * Assumptions:
  66. * -
  67. *
  68. * Pass Criteria:
  69. * - pthread_cond_timedwait returns 0.
  70. * - Process returns zero exit status.
  71. *
  72. * Fail Criteria:
  73. * - pthread_cond_timedwait returns ETIMEDOUT.
  74. * - Process returns non-zero exit status.
  75. */
  76. #include "test.h"
  77. #include <sys/timeb.h>
  78. typedef struct cvthing_t_ cvthing_t;
  79. struct cvthing_t_ {
  80. pthread_cond_t notbusy;
  81. pthread_mutex_t lock;
  82. int shared;
  83. };
  84. static cvthing_t cvthing = {
  85. PTHREAD_COND_INITIALIZER,
  86. PTHREAD_MUTEX_INITIALIZER,
  87. 0
  88. };
  89. enum {
  90. NUMTHREADS = 2
  91. };
  92. void *
  93. mythread(void * arg)
  94. {
  95. assert(pthread_mutex_lock(&cvthing.lock) == 0);
  96. cvthing.shared++;
  97. assert(pthread_mutex_unlock(&cvthing.lock) == 0);
  98. assert(pthread_cond_signal(&cvthing.notbusy) == 0);
  99. return (void *) 0;
  100. }
  101. int
  102. main()
  103. {
  104. pthread_t t[NUMTHREADS];
  105. struct timespec abstime = { 0, 0 };
  106. PTW32_STRUCT_TIMEB currSysTime;
  107. const DWORD NANOSEC_PER_MILLISEC = 1000000;
  108. cvthing.shared = 0;
  109. assert((t[0] = pthread_self()).p != NULL);
  110. assert(cvthing.notbusy == PTHREAD_COND_INITIALIZER);
  111. assert(cvthing.lock == PTHREAD_MUTEX_INITIALIZER);
  112. assert(pthread_mutex_lock(&cvthing.lock) == 0);
  113. assert(cvthing.lock != PTHREAD_MUTEX_INITIALIZER);
  114. /* get current system time */
  115. PTW32_FTIME(&currSysTime);
  116. abstime.tv_sec = (long)currSysTime.time;
  117. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  118. abstime.tv_sec += 5;
  119. assert(pthread_cond_timedwait(&cvthing.notbusy, &cvthing.lock, &abstime) == ETIMEDOUT);
  120. assert(cvthing.notbusy != PTHREAD_COND_INITIALIZER);
  121. assert(pthread_create(&t[1], NULL, mythread, (void *) 1) == 0);
  122. PTW32_FTIME(&currSysTime);
  123. abstime.tv_sec = (long)currSysTime.time;
  124. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  125. abstime.tv_sec += 5;
  126. while (! (cvthing.shared > 0))
  127. assert(pthread_cond_timedwait(&cvthing.notbusy, &cvthing.lock, &abstime) == 0);
  128. assert(cvthing.shared > 0);
  129. assert(pthread_mutex_unlock(&cvthing.lock) == 0);
  130. assert(pthread_join(t[1], NULL) == 0);
  131. assert(pthread_mutex_destroy(&cvthing.lock) == 0);
  132. assert(cvthing.lock == NULL);
  133. assert(pthread_cond_destroy(&cvthing.notbusy) == 0);
  134. assert(cvthing.notbusy == NULL);
  135. return 0;
  136. }