condvar3.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * File: condvar3.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 basic function of a CV
  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. * - The primary thread takes the lock before creating any threads.
  53. * The secondary thread blocks on the lock allowing the primary
  54. * thread to enter the cv wait state which releases the lock.
  55. * The secondary thread then takes the lock and signals the waiting
  56. * primary thread.
  57. *
  58. * Environment:
  59. * -
  60. *
  61. * Input:
  62. * - None.
  63. *
  64. * Output:
  65. * - File name, Line number, and failed expression on failure.
  66. * - No output on success.
  67. *
  68. * Assumptions:
  69. * -
  70. *
  71. * Pass Criteria:
  72. * - pthread_cond_timedwait returns 0.
  73. * - Process returns zero exit status.
  74. *
  75. * Fail Criteria:
  76. * - pthread_cond_timedwait returns ETIMEDOUT.
  77. * - Process returns non-zero exit status.
  78. */
  79. #include "test.h"
  80. #include <sys/timeb.h>
  81. static pthread_cond_t cv;
  82. static pthread_mutex_t mutex;
  83. static int shared = 0;
  84. enum {
  85. NUMTHREADS = 2 /* Including the primary thread. */
  86. };
  87. void *
  88. mythread(void * arg)
  89. {
  90. int result = 0;
  91. assert(pthread_mutex_lock(&mutex) == 0);
  92. shared++;
  93. assert(pthread_mutex_unlock(&mutex) == 0);
  94. if ((result = pthread_cond_signal(&cv)) != 0)
  95. {
  96. printf("Error = %s\n", error_string[result]);
  97. }
  98. assert(result == 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. assert((t[0] = pthread_self()).p != NULL);
  109. assert(pthread_cond_init(&cv, NULL) == 0);
  110. assert(pthread_mutex_init(&mutex, NULL) == 0);
  111. assert(pthread_mutex_lock(&mutex) == 0);
  112. /* get current system time */
  113. PTW32_FTIME(&currSysTime);
  114. abstime.tv_sec = (long)currSysTime.time;
  115. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  116. assert(pthread_create(&t[1], NULL, mythread, (void *) 1) == 0);
  117. abstime.tv_sec += 5;
  118. while (! (shared > 0))
  119. assert(pthread_cond_timedwait(&cv, &mutex, &abstime) == 0);
  120. assert(shared > 0);
  121. assert(pthread_mutex_unlock(&mutex) == 0);
  122. assert(pthread_join(t[1], NULL) == 0);
  123. assert(pthread_cond_destroy(&cv) == 0);
  124. return 0;
  125. }