michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /* michael@0: * michael@0: * Test atomic stack operations michael@0: * michael@0: * Two stacks are created and threads add data items (each containing michael@0: * one of the first n integers) to the first stack, remove data items michael@0: * from the first stack and add them to the second stack. The primordial michael@0: * thread compares the sum of the first n integers to the sum of the michael@0: * integers in the data items in the second stack. The test succeeds if michael@0: * they are equal. michael@0: */ michael@0: michael@0: #include "nspr.h" michael@0: #include "plgetopt.h" michael@0: michael@0: typedef struct _DataRecord { michael@0: PRInt32 data; michael@0: PRStackElem link; michael@0: } DataRecord; michael@0: michael@0: #define RECORD_LINK_PTR(lp) ((DataRecord*) ((char*) (lp) - offsetof(DataRecord,link))) michael@0: michael@0: #define MAX_THREAD_CNT 100 michael@0: #define DEFAULT_THREAD_CNT 4 michael@0: #define DEFAULT_DATA_CNT 100 michael@0: #define DEFAULT_LOOP_CNT 10000 michael@0: michael@0: /* michael@0: * sum of the first n numbers using the formula n*(n+1)/2 michael@0: */ michael@0: #define SUM_OF_NUMBERS(n) ((n & 1) ? (((n + 1)/2) * n) : ((n/2) * (n+1))) michael@0: michael@0: typedef struct stack_data { michael@0: PRStack *list1; michael@0: PRStack *list2; michael@0: PRInt32 initial_data_value; michael@0: PRInt32 data_cnt; michael@0: PRInt32 loops; michael@0: } stack_data; michael@0: michael@0: static void stackop(void *arg); michael@0: michael@0: static int _debug_on; michael@0: michael@0: PRFileDesc *output; michael@0: PRFileDesc *errhandle; michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: #if !(defined(SYMBIAN) && defined(__WINS__)) michael@0: PRInt32 rv, cnt, sum; michael@0: DataRecord *Item; michael@0: PRStack *list1, *list2; michael@0: PRStackElem *node; michael@0: PRStatus rc; michael@0: michael@0: PRInt32 thread_cnt = DEFAULT_THREAD_CNT; michael@0: PRInt32 data_cnt = DEFAULT_DATA_CNT; michael@0: PRInt32 loops = DEFAULT_LOOP_CNT; michael@0: PRThread **threads; michael@0: stack_data *thread_args; michael@0: michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "dt:c:l:"); michael@0: michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'd': /* debug mode */ michael@0: _debug_on = 1; michael@0: break; michael@0: case 't': /* thread count */ michael@0: thread_cnt = atoi(opt->value); michael@0: break; michael@0: case 'c': /* data count */ michael@0: data_cnt = atoi(opt->value); michael@0: break; michael@0: case 'l': /* loop count */ michael@0: loops = atoi(opt->value); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: PR_SetConcurrency(4); michael@0: michael@0: output = PR_GetSpecialFD(PR_StandardOutput); michael@0: errhandle = PR_GetSpecialFD(PR_StandardError); michael@0: list1 = PR_CreateStack("Stack_1"); michael@0: if (list1 == NULL) { michael@0: PR_fprintf(errhandle, "PR_CreateStack failed - error %d\n", michael@0: PR_GetError()); michael@0: return 1; michael@0: } michael@0: michael@0: list2 = PR_CreateStack("Stack_2"); michael@0: if (list2 == NULL) { michael@0: PR_fprintf(errhandle, "PR_CreateStack failed - error %d\n", michael@0: PR_GetError()); michael@0: return 1; michael@0: } michael@0: michael@0: michael@0: threads = (PRThread**) PR_CALLOC(sizeof(PRThread*) * thread_cnt); michael@0: thread_args = (stack_data *) PR_CALLOC(sizeof(stack_data) * thread_cnt); michael@0: michael@0: if (_debug_on) michael@0: PR_fprintf(output,"%s: thread_cnt = %d data_cnt = %d\n", argv[0], michael@0: thread_cnt, data_cnt); michael@0: for(cnt = 0; cnt < thread_cnt; cnt++) { michael@0: PRThreadScope scope; michael@0: michael@0: thread_args[cnt].list1 = list1; michael@0: thread_args[cnt].list2 = list2; michael@0: thread_args[cnt].loops = loops; michael@0: thread_args[cnt].data_cnt = data_cnt; michael@0: thread_args[cnt].initial_data_value = 1 + cnt * data_cnt; michael@0: michael@0: if (cnt & 1) michael@0: scope = PR_GLOBAL_THREAD; michael@0: else michael@0: scope = PR_LOCAL_THREAD; michael@0: michael@0: michael@0: threads[cnt] = PR_CreateThread(PR_USER_THREAD, michael@0: stackop, &thread_args[cnt], michael@0: PR_PRIORITY_NORMAL, michael@0: scope, michael@0: PR_JOINABLE_THREAD, michael@0: 0); michael@0: if (threads[cnt] == NULL) { michael@0: PR_fprintf(errhandle, "PR_CreateThread failed - error %d\n", michael@0: PR_GetError()); michael@0: PR_ProcessExit(2); michael@0: } michael@0: if (_debug_on) michael@0: PR_fprintf(output,"%s: created thread = 0x%x\n", argv[0], michael@0: threads[cnt]); michael@0: } michael@0: michael@0: for(cnt = 0; cnt < thread_cnt; cnt++) { michael@0: rc = PR_JoinThread(threads[cnt]); michael@0: PR_ASSERT(rc == PR_SUCCESS); michael@0: } michael@0: michael@0: node = PR_StackPop(list1); michael@0: /* michael@0: * list1 should be empty michael@0: */ michael@0: if (node != NULL) { michael@0: PR_fprintf(errhandle, "Error - Stack 1 not empty\n"); michael@0: PR_ASSERT(node == NULL); michael@0: PR_ProcessExit(4); michael@0: } michael@0: michael@0: cnt = data_cnt * thread_cnt; michael@0: sum = 0; michael@0: while (cnt-- > 0) { michael@0: node = PR_StackPop(list2); michael@0: /* michael@0: * There should be at least 'cnt' number of records michael@0: */ michael@0: if (node == NULL) { michael@0: PR_fprintf(errhandle, "Error - PR_StackPop returned NULL\n"); michael@0: PR_ProcessExit(3); michael@0: } michael@0: Item = RECORD_LINK_PTR(node); michael@0: sum += Item->data; michael@0: } michael@0: node = PR_StackPop(list2); michael@0: /* michael@0: * there should be exactly 'cnt' number of records michael@0: */ michael@0: if (node != NULL) { michael@0: PR_fprintf(errhandle, "Error - Stack 2 not empty\n"); michael@0: PR_ASSERT(node == NULL); michael@0: PR_ProcessExit(4); michael@0: } michael@0: PR_DELETE(threads); michael@0: PR_DELETE(thread_args); michael@0: michael@0: PR_DestroyStack(list1); michael@0: PR_DestroyStack(list2); michael@0: michael@0: if (sum == SUM_OF_NUMBERS(data_cnt * thread_cnt)) { michael@0: PR_fprintf(output, "%s successful\n", argv[0]); michael@0: PR_fprintf(output, "\t\tsum = 0x%x, expected = 0x%x\n", sum, michael@0: SUM_OF_NUMBERS(thread_cnt * data_cnt)); michael@0: return 0; michael@0: } else { michael@0: PR_fprintf(output, "%s failed: sum = 0x%x, expected = 0x%x\n", michael@0: argv[0], sum, michael@0: SUM_OF_NUMBERS(data_cnt * thread_cnt)); michael@0: return 2; michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: static void stackop(void *thread_arg) michael@0: { michael@0: PRInt32 val, cnt, index, loops; michael@0: DataRecord *Items, *Item; michael@0: PRStack *list1, *list2; michael@0: PRStackElem *node; michael@0: stack_data *arg = (stack_data *) thread_arg; michael@0: michael@0: val = arg->initial_data_value; michael@0: cnt = arg->data_cnt; michael@0: loops = arg->loops; michael@0: list1 = arg->list1; michael@0: list2 = arg->list2; michael@0: michael@0: /* michael@0: * allocate memory for the data records michael@0: */ michael@0: Items = (DataRecord *) PR_CALLOC(sizeof(DataRecord) * cnt); michael@0: PR_ASSERT(Items != NULL); michael@0: index = 0; michael@0: michael@0: if (_debug_on) michael@0: PR_fprintf(output, michael@0: "Thread[0x%x] init_val = %d cnt = %d data1 = 0x%x datan = 0x%x\n", michael@0: PR_GetCurrentThread(), val, cnt, &Items[0], &Items[cnt-1]); michael@0: michael@0: michael@0: /* michael@0: * add the data records to list1 michael@0: */ michael@0: while (cnt-- > 0) { michael@0: Items[index].data = val++; michael@0: PR_StackPush(list1, &Items[index].link); michael@0: index++; michael@0: } michael@0: michael@0: /* michael@0: * pop data records from list1 and add them back to list1 michael@0: * generates contention for the stack accesses michael@0: */ michael@0: while (loops-- > 0) { michael@0: cnt = arg->data_cnt; michael@0: while (cnt-- > 0) { michael@0: node = PR_StackPop(list1); michael@0: if (node == NULL) { michael@0: PR_fprintf(errhandle, "Error - PR_StackPop returned NULL\n"); michael@0: PR_ASSERT(node != NULL); michael@0: PR_ProcessExit(3); michael@0: } michael@0: PR_StackPush(list1, node); michael@0: } michael@0: } michael@0: /* michael@0: * remove the data records from list1 and add them to list2 michael@0: */ michael@0: cnt = arg->data_cnt; michael@0: while (cnt-- > 0) { michael@0: node = PR_StackPop(list1); michael@0: if (node == NULL) { michael@0: PR_fprintf(errhandle, "Error - PR_StackPop returned NULL\n"); michael@0: PR_ASSERT(node != NULL); michael@0: PR_ProcessExit(3); michael@0: } michael@0: PR_StackPush(list2, node); michael@0: } michael@0: if (_debug_on) michael@0: PR_fprintf(output, michael@0: "Thread[0x%x] init_val = %d cnt = %d exiting\n", michael@0: PR_GetCurrentThread(), val, cnt); michael@0: michael@0: } michael@0: