stress1.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * stress1.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  8. * Copyright(C) 2008 Jason Schmidlapp
  9. *
  10. * Contact Email: [email protected]
  11. *
  12. *
  13. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  14. * Copyright(C) 1998 John E. Bossom
  15. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  16. *
  17. * Contact Email: [email protected]
  18. *
  19. * The original list of contributors to the Pthreads-win32 project
  20. * is contained in the file CONTRIBUTORS.ptw32 included with the
  21. * source code distribution. The list can also be seen at the
  22. * following World Wide Web location:
  23. * http://sources.redhat.com/pthreads-win32/contributors.html
  24. *
  25. * This library is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU Lesser General Public
  27. * License as published by the Free Software Foundation; either
  28. * version 2 of the License, or (at your option) any later version.
  29. *
  30. * This library is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  33. * Lesser General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU Lesser General Public
  36. * License along with this library in the file COPYING.LIB;
  37. * if not, write to the Free Software Foundation, Inc.,
  38. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  39. *
  40. * --------------------------------------------------------------------------
  41. *
  42. * Test Synopsis:
  43. * - Stress test condition variables, mutexes, semaphores.
  44. *
  45. * Test Method (Validation or Falsification):
  46. * - Validation
  47. *
  48. * Requirements Tested:
  49. * - Correct accounting of semaphore and condition variable waiters.
  50. *
  51. * Features Tested:
  52. * -
  53. *
  54. * Cases Tested:
  55. * -
  56. *
  57. * Description:
  58. * Attempting to expose race conditions in cond vars, semaphores etc.
  59. * - Master attempts to signal slave close to when timeout is due.
  60. * - Master and slave do battle continuously until main tells them to stop.
  61. * - Afterwards, the CV must be successfully destroyed (will return an
  62. * error if there are waiters (including any internal semaphore waiters,
  63. * which, if there are, cannot not be real waiters).
  64. *
  65. * Environment:
  66. * -
  67. *
  68. * Input:
  69. * - None.
  70. *
  71. * Output:
  72. * - File name, Line number, and failed expression on failure.
  73. * - No output on success.
  74. *
  75. * Assumptions:
  76. * -
  77. *
  78. * Pass Criteria:
  79. * - CV is successfully destroyed.
  80. *
  81. * Fail Criteria:
  82. * - CV destroy fails.
  83. */
  84. #include <string.h>
  85. #include "test.h"
  86. typedef long long int64_t;
  87. const unsigned int ITERATIONS = 1000;
  88. static pthread_t master, slave;
  89. typedef struct
  90. {
  91. int value;
  92. pthread_cond_t cv;
  93. pthread_mutex_t mx;
  94. } mysig_t;
  95. static int allExit;
  96. static mysig_t control = {0, PTHREAD_COND_INITIALIZER, PTHREAD_MUTEX_INITIALIZER};
  97. static pthread_barrier_t startBarrier, readyBarrier, holdBarrier;
  98. static int timeoutCount = 0;
  99. static int signalsTakenCount = 0;
  100. static int signalsSent = 0;
  101. static int bias = 0;
  102. static int timeout = 10;
  103. // Must be > 0
  104. enum
  105. {
  106. CTL_STOP = -1
  107. };
  108. /*
  109. * Returns abstime 'milliseconds' from 'now'.
  110. *
  111. * Works for: -INT_MAX <= millisecs <= INT_MAX
  112. */
  113. static struct timespec *
  114. millisecondsFromNow (struct timespec * time, int millisecs)
  115. {
  116. struct _timeb currSysTime;
  117. int64_t nanosecs, secs;
  118. const int64_t NANOSEC_PER_MILLISEC = 1000000;
  119. const int64_t NANOSEC_PER_SEC = 1000000000;
  120. /* get current system time and add millisecs */
  121. _ftime(&currSysTime);
  122. secs = (int64_t)(currSysTime.time) + (millisecs / 1000);
  123. nanosecs = ((int64_t) (millisecs%1000 + currSysTime.millitm)) * NANOSEC_PER_MILLISEC;
  124. if (nanosecs >= NANOSEC_PER_SEC)
  125. {
  126. secs++;
  127. nanosecs -= NANOSEC_PER_SEC;
  128. }
  129. else if (nanosecs < 0)
  130. {
  131. secs--;
  132. nanosecs += NANOSEC_PER_SEC;
  133. }
  134. time->tv_nsec = (long)nanosecs;
  135. time->tv_sec = (long)secs;
  136. return time;
  137. }
  138. static void *
  139. masterThread (void * arg)
  140. {
  141. int dither = (int) arg;
  142. timeout = (int) arg;
  143. pthread_barrier_wait(&startBarrier);
  144. do
  145. {
  146. int sleepTime;
  147. assert(pthread_mutex_lock(&control.mx) == 0);
  148. control.value = timeout;
  149. assert(pthread_mutex_unlock(&control.mx) == 0);
  150. /*
  151. * We are attempting to send the signal close to when the slave
  152. * is due to timeout. We feel around by adding some [non-random] dither.
  153. *
  154. * dither is in the range 2*timeout peak-to-peak
  155. * sleep time is the average of timeout plus dither.
  156. * e.g.
  157. * if timeout = 10 then dither = 20 and
  158. * sleep millisecs is: 5 <= ms <= 15
  159. *
  160. * The bias value attempts to apply some negative feedback to keep
  161. * the ratio of timeouts to signals taken close to 1:1.
  162. * bias changes more slowly than dither so as to average more.
  163. *
  164. * Finally, if abs(bias) exceeds timeout then timeout is incremented.
  165. */
  166. if (signalsSent % timeout == 0)
  167. {
  168. if (timeoutCount > signalsTakenCount)
  169. {
  170. bias++;
  171. }
  172. else if (timeoutCount < signalsTakenCount)
  173. {
  174. bias--;
  175. }
  176. if (bias < -timeout || bias > timeout)
  177. {
  178. timeout++;
  179. }
  180. }
  181. dither = (dither + 1 ) % (timeout * 2);
  182. sleepTime = (timeout - bias + dither) / 2;
  183. pte_osThreadSleep(sleepTime);
  184. assert(pthread_cond_signal(&control.cv) == 0);
  185. signalsSent++;
  186. pthread_barrier_wait(&holdBarrier);
  187. pthread_barrier_wait(&readyBarrier);
  188. }
  189. while (!allExit);
  190. return NULL;
  191. }
  192. static void *
  193. slaveThread (void * arg)
  194. {
  195. struct timespec time;
  196. pthread_barrier_wait(&startBarrier);
  197. do
  198. {
  199. assert(pthread_mutex_lock(&control.mx) == 0);
  200. if (pthread_cond_timedwait(&control.cv,
  201. &control.mx,
  202. millisecondsFromNow(&time, control.value)) == ETIMEDOUT)
  203. {
  204. timeoutCount++;
  205. }
  206. else
  207. {
  208. signalsTakenCount++;
  209. }
  210. assert(pthread_mutex_unlock(&control.mx) == 0);
  211. pthread_barrier_wait(&holdBarrier);
  212. pthread_barrier_wait(&readyBarrier);
  213. }
  214. while (!allExit);
  215. return NULL;
  216. }
  217. int pthread_test_stress1()
  218. {
  219. unsigned int i;
  220. control.value = 0;
  221. control.cv = PTHREAD_COND_INITIALIZER;
  222. control.mx = PTHREAD_MUTEX_INITIALIZER;
  223. timeoutCount = 0;
  224. signalsTakenCount = 0;
  225. signalsSent = 0;
  226. bias = 0;
  227. timeout = 10;
  228. assert(pthread_barrier_init(&startBarrier, NULL, 3) == 0);
  229. assert(pthread_barrier_init(&readyBarrier, NULL, 3) == 0);
  230. assert(pthread_barrier_init(&holdBarrier, NULL, 3) == 0);
  231. assert(pthread_create(&master, NULL, masterThread, (void *) timeout) == 0);
  232. assert(pthread_create(&slave, NULL, slaveThread, NULL) == 0);
  233. allExit = 0;
  234. pthread_barrier_wait(&startBarrier);
  235. for (i = 1; !allExit; i++)
  236. {
  237. pthread_barrier_wait(&holdBarrier);
  238. if (i >= ITERATIONS)
  239. {
  240. allExit = 1;
  241. }
  242. pthread_barrier_wait(&readyBarrier);
  243. }
  244. assert(pthread_join(slave, NULL) == 0);
  245. assert(pthread_join(master, NULL) == 0);
  246. /* Cleanup */
  247. assert(pthread_barrier_destroy(&holdBarrier) == 0);
  248. assert(pthread_barrier_destroy(&readyBarrier) == 0);
  249. assert(pthread_barrier_destroy(&startBarrier) == 0);
  250. assert(pthread_cond_destroy(&control.cv) == 0);
  251. assert(pthread_mutex_destroy(&control.mx) == 0);
  252. /* Success. */
  253. return 0;
  254. }