stack.c revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Netscape Portable Runtime (NSPR).
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
*
* Test atomic stack operations
*
* Two stacks are created and threads add data items (each containing
* one of the first n integers) to the first stack, remove data items
* from the first stack and add them to the second stack. The primordial
* thread compares the sum of the first n integers to the sum of the
* integers in the data items in the second stack. The test succeeds if
* they are equal.
*/
#include "nspr.h"
#include "plgetopt.h"
typedef struct _DataRecord {
} DataRecord;
#define MAX_THREAD_CNT 100
#define DEFAULT_THREAD_CNT 4
#define DEFAULT_DATA_CNT 100
#define DEFAULT_LOOP_CNT 10000
/*
* sum of the first n numbers using the formula n*(n+1)/2
*/
typedef struct stack_data {
} stack_data;
static int _debug_on;
{
{
if (PL_OPT_BAD == os) continue;
{
case 'd': /* debug mode */
_debug_on = 1;
break;
case 't': /* thread count */
break;
case 'c': /* data count */
break;
case 'l': /* loop count */
break;
default:
break;
}
}
PR_GetError());
return 1;
}
PR_GetError());
return 1;
}
if (_debug_on)
if (cnt & 1)
else
0);
PR_GetError());
PR_ProcessExit(2);
}
if (_debug_on)
}
}
/*
* list1 should be empty
*/
PR_ProcessExit(4);
}
sum = 0;
while (cnt-- > 0) {
/*
* There should be at least 'cnt' number of records
*/
PR_ProcessExit(3);
}
}
/*
* there should be exactly 'cnt' number of records
*/
PR_ProcessExit(4);
}
return 0;
} else {
return 2;
}
}
static void stackop(void *thread_arg)
{
/*
* allocate memory for the data records
*/
index = 0;
if (_debug_on)
"Thread[0x%x] init_val = %d cnt = %d data1 = 0x%x datan = 0x%x\n",
/*
* add the data records to list1
*/
while (cnt-- > 0) {
index++;
}
/*
* pop data records from list1 and add them back to list1
* generates contention for the stack accesses
*/
while (loops-- > 0) {
while (cnt-- > 0) {
PR_ProcessExit(3);
}
}
}
/*
* remove the data records from list1 and add them to list2
*/
while (cnt-- > 0) {
PR_ProcessExit(3);
}
}
if (_debug_on)
"Thread[0x%x] init_val = %d cnt = %d exiting\n",
}