condvar1_2.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * File: condvar1_2.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 CV linked list management and serialisation.
  38. *
  39. * Test Method (Validation or Falsification):
  40. * - Validation:
  41. * Initiate and destroy several CVs in random order.
  42. * Asynchronously traverse the CV list and broadcast.
  43. *
  44. * Requirements Tested:
  45. * -
  46. *
  47. * Features Tested:
  48. * -
  49. *
  50. * Cases Tested:
  51. * -
  52. *
  53. * Description:
  54. * - Creates and then imediately destroys a CV. Does not
  55. * test the CV.
  56. *
  57. * Environment:
  58. * -
  59. *
  60. * Input:
  61. * - None.
  62. *
  63. * Output:
  64. * - File name, Line number, and failed expression on failure.
  65. * - No output on success.
  66. *
  67. * Assumptions:
  68. * -
  69. *
  70. * Pass Criteria:
  71. * - All initialised CVs destroyed without segfault.
  72. * - Successfully broadcasts all remaining CVs after
  73. * each CV is removed.
  74. *
  75. * Fail Criteria:
  76. */
  77. #include <stdlib.h>
  78. #include "test.h"
  79. enum {
  80. NUM_CV = 5,
  81. NUM_LOOPS = 5
  82. };
  83. static pthread_cond_t cv[NUM_CV];
  84. int
  85. main()
  86. {
  87. int i, j, k;
  88. void* result = (void*)-1;
  89. pthread_t t;
  90. for (k = 0; k < NUM_LOOPS; k++)
  91. {
  92. for (i = 0; i < NUM_CV; i++)
  93. {
  94. assert(pthread_cond_init(&cv[i], NULL) == 0);
  95. }
  96. j = NUM_CV;
  97. (void) srand((unsigned)time(NULL));
  98. /* Traverse the list asynchronously. */
  99. assert(pthread_create(&t, NULL, pthread_timechange_handler_np, NULL) == 0);
  100. do
  101. {
  102. i = (NUM_CV - 1) * rand() / RAND_MAX;
  103. if (cv[i] != NULL)
  104. {
  105. j--;
  106. assert(pthread_cond_destroy(&cv[i]) == 0);
  107. }
  108. }
  109. while (j > 0);
  110. assert(pthread_join(t, &result) == 0);
  111. assert ((int)(size_t)result == 0);
  112. }
  113. return 0;
  114. }