stack.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. *
  7. * Test atomic stack operations
  8. *
  9. * Two stacks are created and threads add data items (each containing
  10. * one of the first n integers) to the first stack, remove data items
  11. * from the first stack and add them to the second stack. The primordial
  12. * thread compares the sum of the first n integers to the sum of the
  13. * integers in the data items in the second stack. The test succeeds if
  14. * they are equal.
  15. */
  16. #include "nspr.h"
  17. #include "plgetopt.h"
  18. typedef struct _DataRecord {
  19. PRInt32 data;
  20. PRStackElem link;
  21. } DataRecord;
  22. #define RECORD_LINK_PTR(lp) ((DataRecord*) ((char*) (lp) - offsetof(DataRecord,link)))
  23. #define MAX_THREAD_CNT 100
  24. #define DEFAULT_THREAD_CNT 4
  25. #define DEFAULT_DATA_CNT 100
  26. #define DEFAULT_LOOP_CNT 10000
  27. /*
  28. * sum of the first n numbers using the formula n*(n+1)/2
  29. */
  30. #define SUM_OF_NUMBERS(n) ((n & 1) ? (((n + 1)/2) * n) : ((n/2) * (n+1)))
  31. typedef struct stack_data {
  32. PRStack *list1;
  33. PRStack *list2;
  34. PRInt32 initial_data_value;
  35. PRInt32 data_cnt;
  36. PRInt32 loops;
  37. } stack_data;
  38. static void stackop(void *arg);
  39. static int _debug_on;
  40. PRFileDesc *output;
  41. PRFileDesc *errhandle;
  42. int main(int argc, char **argv)
  43. {
  44. PRInt32 rv, cnt, sum;
  45. DataRecord *Item;
  46. PRStack *list1, *list2;
  47. PRStackElem *node;
  48. PRStatus rc;
  49. PRInt32 thread_cnt = DEFAULT_THREAD_CNT;
  50. PRInt32 data_cnt = DEFAULT_DATA_CNT;
  51. PRInt32 loops = DEFAULT_LOOP_CNT;
  52. PRThread **threads;
  53. stack_data *thread_args;
  54. PLOptStatus os;
  55. PLOptState *opt = PL_CreateOptState(argc, argv, "dt:c:l:");
  56. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  57. {
  58. if (PL_OPT_BAD == os) {
  59. continue;
  60. }
  61. switch (opt->option)
  62. {
  63. case 'd': /* debug mode */
  64. _debug_on = 1;
  65. break;
  66. case 't': /* thread count */
  67. thread_cnt = atoi(opt->value);
  68. break;
  69. case 'c': /* data count */
  70. data_cnt = atoi(opt->value);
  71. break;
  72. case 'l': /* loop count */
  73. loops = atoi(opt->value);
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. PL_DestroyOptState(opt);
  80. PR_SetConcurrency(4);
  81. output = PR_GetSpecialFD(PR_StandardOutput);
  82. errhandle = PR_GetSpecialFD(PR_StandardError);
  83. list1 = PR_CreateStack("Stack_1");
  84. if (list1 == NULL) {
  85. PR_fprintf(errhandle, "PR_CreateStack failed - error %d\n",
  86. PR_GetError());
  87. return 1;
  88. }
  89. list2 = PR_CreateStack("Stack_2");
  90. if (list2 == NULL) {
  91. PR_fprintf(errhandle, "PR_CreateStack failed - error %d\n",
  92. PR_GetError());
  93. return 1;
  94. }
  95. threads = (PRThread**) PR_CALLOC(sizeof(PRThread*) * thread_cnt);
  96. thread_args = (stack_data *) PR_CALLOC(sizeof(stack_data) * thread_cnt);
  97. if (_debug_on)
  98. PR_fprintf(output,"%s: thread_cnt = %d data_cnt = %d\n", argv[0],
  99. thread_cnt, data_cnt);
  100. for(cnt = 0; cnt < thread_cnt; cnt++) {
  101. PRThreadScope scope;
  102. thread_args[cnt].list1 = list1;
  103. thread_args[cnt].list2 = list2;
  104. thread_args[cnt].loops = loops;
  105. thread_args[cnt].data_cnt = data_cnt;
  106. thread_args[cnt].initial_data_value = 1 + cnt * data_cnt;
  107. if (cnt & 1) {
  108. scope = PR_GLOBAL_THREAD;
  109. }
  110. else {
  111. scope = PR_LOCAL_THREAD;
  112. }
  113. threads[cnt] = PR_CreateThread(PR_USER_THREAD,
  114. stackop, &thread_args[cnt],
  115. PR_PRIORITY_NORMAL,
  116. scope,
  117. PR_JOINABLE_THREAD,
  118. 0);
  119. if (threads[cnt] == NULL) {
  120. PR_fprintf(errhandle, "PR_CreateThread failed - error %d\n",
  121. PR_GetError());
  122. PR_ProcessExit(2);
  123. }
  124. if (_debug_on)
  125. PR_fprintf(output,"%s: created thread = 0x%x\n", argv[0],
  126. threads[cnt]);
  127. }
  128. for(cnt = 0; cnt < thread_cnt; cnt++) {
  129. rc = PR_JoinThread(threads[cnt]);
  130. PR_ASSERT(rc == PR_SUCCESS);
  131. }
  132. node = PR_StackPop(list1);
  133. /*
  134. * list1 should be empty
  135. */
  136. if (node != NULL) {
  137. PR_fprintf(errhandle, "Error - Stack 1 not empty\n");
  138. PR_ASSERT(node == NULL);
  139. PR_ProcessExit(4);
  140. }
  141. cnt = data_cnt * thread_cnt;
  142. sum = 0;
  143. while (cnt-- > 0) {
  144. node = PR_StackPop(list2);
  145. /*
  146. * There should be at least 'cnt' number of records
  147. */
  148. if (node == NULL) {
  149. PR_fprintf(errhandle, "Error - PR_StackPop returned NULL\n");
  150. PR_ProcessExit(3);
  151. }
  152. Item = RECORD_LINK_PTR(node);
  153. sum += Item->data;
  154. }
  155. node = PR_StackPop(list2);
  156. /*
  157. * there should be exactly 'cnt' number of records
  158. */
  159. if (node != NULL) {
  160. PR_fprintf(errhandle, "Error - Stack 2 not empty\n");
  161. PR_ASSERT(node == NULL);
  162. PR_ProcessExit(4);
  163. }
  164. PR_DELETE(threads);
  165. PR_DELETE(thread_args);
  166. PR_DestroyStack(list1);
  167. PR_DestroyStack(list2);
  168. if (sum == SUM_OF_NUMBERS(data_cnt * thread_cnt)) {
  169. PR_fprintf(output, "%s successful\n", argv[0]);
  170. PR_fprintf(output, "\t\tsum = 0x%x, expected = 0x%x\n", sum,
  171. SUM_OF_NUMBERS(thread_cnt * data_cnt));
  172. return 0;
  173. } else {
  174. PR_fprintf(output, "%s failed: sum = 0x%x, expected = 0x%x\n",
  175. argv[0], sum,
  176. SUM_OF_NUMBERS(data_cnt * thread_cnt));
  177. return 2;
  178. }
  179. }
  180. static void stackop(void *thread_arg)
  181. {
  182. PRInt32 val, cnt, index, loops;
  183. DataRecord *Items, *Item;
  184. PRStack *list1, *list2;
  185. PRStackElem *node;
  186. stack_data *arg = (stack_data *) thread_arg;
  187. val = arg->initial_data_value;
  188. cnt = arg->data_cnt;
  189. loops = arg->loops;
  190. list1 = arg->list1;
  191. list2 = arg->list2;
  192. /*
  193. * allocate memory for the data records
  194. */
  195. Items = (DataRecord *) PR_CALLOC(sizeof(DataRecord) * cnt);
  196. PR_ASSERT(Items != NULL);
  197. index = 0;
  198. if (_debug_on)
  199. PR_fprintf(output,
  200. "Thread[0x%x] init_val = %d cnt = %d data1 = 0x%x datan = 0x%x\n",
  201. PR_GetCurrentThread(), val, cnt, &Items[0], &Items[cnt-1]);
  202. /*
  203. * add the data records to list1
  204. */
  205. while (cnt-- > 0) {
  206. Items[index].data = val++;
  207. PR_StackPush(list1, &Items[index].link);
  208. index++;
  209. }
  210. /*
  211. * pop data records from list1 and add them back to list1
  212. * generates contention for the stack accesses
  213. */
  214. while (loops-- > 0) {
  215. cnt = arg->data_cnt;
  216. while (cnt-- > 0) {
  217. node = PR_StackPop(list1);
  218. if (node == NULL) {
  219. PR_fprintf(errhandle, "Error - PR_StackPop returned NULL\n");
  220. PR_ASSERT(node != NULL);
  221. PR_ProcessExit(3);
  222. }
  223. PR_StackPush(list1, node);
  224. }
  225. }
  226. /*
  227. * remove the data records from list1 and add them to list2
  228. */
  229. cnt = arg->data_cnt;
  230. while (cnt-- > 0) {
  231. node = PR_StackPop(list1);
  232. if (node == NULL) {
  233. PR_fprintf(errhandle, "Error - PR_StackPop returned NULL\n");
  234. PR_ASSERT(node != NULL);
  235. PR_ProcessExit(3);
  236. }
  237. PR_StackPush(list2, node);
  238. }
  239. if (_debug_on)
  240. PR_fprintf(output,
  241. "Thread[0x%x] init_val = %d cnt = %d exiting\n",
  242. PR_GetCurrentThread(), val, cnt);
  243. }