cvar.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /***********************************************************************
  6. ** 1996 - Netscape Communications Corporation
  7. **
  8. ** Name: cvar.c
  9. **
  10. ** Description: Tests Condition Variable Operations
  11. **
  12. ** Modification History:
  13. ** 13-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  14. ** The debug mode will print all of the printfs associated with this test.
  15. ** The regress mode will be the default mode. Since the regress tool limits
  16. ** the output to a one line status:PASS or FAIL,all of the printf statements
  17. ** have been handled with an if (debug_mode) statement.
  18. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  19. ** recognize the return code from tha main program.
  20. ** 12-June-97 Revert to return code 0 and 1.
  21. ***********************************************************************/
  22. /***********************************************************************
  23. ** Includes
  24. ***********************************************************************/
  25. #include "nspr.h"
  26. /* Used to get the command line option */
  27. #include "plgetopt.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. PRMonitor *mon;
  32. #define DEFAULT_COUNT 1000
  33. PRInt32 count = 0;
  34. PRIntn debug_mode;
  35. #define kQSIZE 1
  36. typedef struct {
  37. PRLock *bufLock;
  38. int startIdx;
  39. int numFull;
  40. PRCondVar *notFull;
  41. PRCondVar *notEmpty;
  42. void *data[kQSIZE];
  43. } CircBuf;
  44. static PRBool failed = PR_FALSE;
  45. /*
  46. ** NewCB creates and initializes a new circular buffer.
  47. */
  48. static CircBuf* NewCB(void)
  49. {
  50. CircBuf *cbp;
  51. cbp = PR_NEW(CircBuf);
  52. if (cbp == NULL) {
  53. return (NULL);
  54. }
  55. cbp->bufLock = PR_NewLock();
  56. cbp->startIdx = 0;
  57. cbp->numFull = 0;
  58. cbp->notFull = PR_NewCondVar(cbp->bufLock);
  59. cbp->notEmpty = PR_NewCondVar(cbp->bufLock);
  60. return (cbp);
  61. }
  62. /*
  63. ** DeleteCB frees a circular buffer.
  64. */
  65. static void DeleteCB(CircBuf *cbp)
  66. {
  67. PR_DestroyLock(cbp->bufLock);
  68. PR_DestroyCondVar(cbp->notFull);
  69. PR_DestroyCondVar(cbp->notEmpty);
  70. PR_DELETE(cbp);
  71. }
  72. /*
  73. ** PutCBData puts new data on the queue. If the queue is full, it waits
  74. ** until there is room.
  75. */
  76. static void PutCBData(CircBuf *cbp, void *data)
  77. {
  78. PR_Lock(cbp->bufLock);
  79. /* wait while the buffer is full */
  80. while (cbp->numFull == kQSIZE) {
  81. PR_WaitCondVar(cbp->notFull,PR_INTERVAL_NO_TIMEOUT);
  82. }
  83. cbp->data[(cbp->startIdx + cbp->numFull) % kQSIZE] = data;
  84. cbp->numFull += 1;
  85. /* let a waiting reader know that there is data */
  86. PR_NotifyCondVar(cbp->notEmpty);
  87. PR_Unlock(cbp->bufLock);
  88. }
  89. /*
  90. ** GetCBData gets the oldest data on the queue. If the queue is empty, it waits
  91. ** until new data appears.
  92. */
  93. static void* GetCBData(CircBuf *cbp)
  94. {
  95. void *data;
  96. PR_Lock(cbp->bufLock);
  97. /* wait while the buffer is empty */
  98. while (cbp->numFull == 0) {
  99. PR_WaitCondVar(cbp->notEmpty,PR_INTERVAL_NO_TIMEOUT);
  100. }
  101. data = cbp->data[cbp->startIdx];
  102. cbp->startIdx =(cbp->startIdx + 1) % kQSIZE;
  103. cbp->numFull -= 1;
  104. /* let a waiting writer know that there is room */
  105. PR_NotifyCondVar(cbp->notFull);
  106. PR_Unlock(cbp->bufLock);
  107. return (data);
  108. }
  109. /************************************************************************/
  110. static int alive;
  111. static void PR_CALLBACK CXReader(void *arg)
  112. {
  113. CircBuf *cbp = (CircBuf *)arg;
  114. PRInt32 i, n;
  115. void *data;
  116. n = count / 2;
  117. for (i = 0; i < n; i++) {
  118. data = GetCBData(cbp);
  119. if ((int)data != i)
  120. if (debug_mode) {
  121. printf("data mismatch at for i = %d usec\n", i);
  122. }
  123. }
  124. PR_EnterMonitor(mon);
  125. --alive;
  126. PR_Notify(mon);
  127. PR_ExitMonitor(mon);
  128. }
  129. static void PR_CALLBACK CXWriter(void *arg)
  130. {
  131. CircBuf *cbp = (CircBuf *)arg;
  132. PRInt32 i, n;
  133. n = count / 2;
  134. for (i = 0; i < n; i++) {
  135. PutCBData(cbp, (void *)i);
  136. }
  137. PR_EnterMonitor(mon);
  138. --alive;
  139. PR_Notify(mon);
  140. PR_ExitMonitor(mon);
  141. }
  142. static void CondWaitContextSwitch(PRThreadScope scope1, PRThreadScope scope2)
  143. {
  144. PRThread *t1, *t2;
  145. CircBuf *cbp;
  146. PR_EnterMonitor(mon);
  147. alive = 2;
  148. cbp = NewCB();
  149. t1 = PR_CreateThread(PR_USER_THREAD,
  150. CXReader, cbp,
  151. PR_PRIORITY_NORMAL,
  152. scope1,
  153. PR_UNJOINABLE_THREAD,
  154. 0);
  155. PR_ASSERT(t1);
  156. t2 = PR_CreateThread(PR_USER_THREAD,
  157. CXWriter, cbp,
  158. PR_PRIORITY_NORMAL,
  159. scope2,
  160. PR_UNJOINABLE_THREAD,
  161. 0);
  162. PR_ASSERT(t2);
  163. /* Wait for both of the threads to exit */
  164. while (alive) {
  165. PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
  166. }
  167. DeleteCB(cbp);
  168. PR_ExitMonitor(mon);
  169. }
  170. static void CondWaitContextSwitchUU(void)
  171. {
  172. CondWaitContextSwitch(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
  173. }
  174. static void CondWaitContextSwitchUK(void)
  175. {
  176. CondWaitContextSwitch(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
  177. }
  178. static void CondWaitContextSwitchKK(void)
  179. {
  180. CondWaitContextSwitch(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
  181. }
  182. /************************************************************************/
  183. static void Measure(void (*func)(void), const char *msg)
  184. {
  185. PRIntervalTime start, stop;
  186. double d;
  187. start = PR_IntervalNow();
  188. (*func)();
  189. stop = PR_IntervalNow();
  190. d = (double)PR_IntervalToMicroseconds(stop - start);
  191. if (debug_mode) {
  192. printf("%40s: %6.2f usec\n", msg, d / count);
  193. }
  194. if (0 == d) {
  195. failed = PR_TRUE;
  196. }
  197. }
  198. static PRIntn PR_CALLBACK RealMain(int argc, char **argv)
  199. {
  200. /* The command line argument: -d is used to determine if the test is being run
  201. in debug mode. The regress tool requires only one line output:PASS or FAIL.
  202. All of the printfs associated with this test has been handled with a if (debug_mode)
  203. test.
  204. Usage: test_name [-d] [-c n]
  205. */
  206. PLOptStatus os;
  207. PLOptState *opt = PL_CreateOptState(argc, argv, "dc:");
  208. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  209. {
  210. if (PL_OPT_BAD == os) {
  211. continue;
  212. }
  213. switch (opt->option)
  214. {
  215. case 'd': /* debug mode */
  216. debug_mode = 1;
  217. break;
  218. case 'c': /* loop count */
  219. count = atoi(opt->value);
  220. break;
  221. default:
  222. break;
  223. }
  224. }
  225. PL_DestroyOptState(opt);
  226. if (0 == count) {
  227. count = DEFAULT_COUNT;
  228. }
  229. mon = PR_NewMonitor();
  230. Measure(CondWaitContextSwitchUU, "cond var wait context switch- user/user");
  231. Measure(CondWaitContextSwitchUK, "cond var wait context switch- user/kernel");
  232. Measure(CondWaitContextSwitchKK, "cond var wait context switch- kernel/kernel");
  233. PR_DestroyMonitor(mon);
  234. if (debug_mode) {
  235. printf("%s\n", (failed) ? "FAILED" : "PASSED");
  236. }
  237. if(failed) {
  238. return 1;
  239. }
  240. else {
  241. return 0;
  242. }
  243. }
  244. int main(int argc, char *argv[])
  245. {
  246. PRIntn rv;
  247. PR_STDIO_INIT();
  248. rv = PR_Initialize(RealMain, argc, argv, 0);
  249. return rv;
  250. } /* main */