5
* --------------------------------------------------------------------------
7
* Pthreads-win32 - POSIX Threads Library for Win32
8
* Copyright(C) 1998 John E. Bossom
9
* Copyright(C) 1999,2005 Pthreads-win32 contributors
11
* Contact Email: rpj@callisto.canberra.edu.au
13
* The current list of contributors is contained
14
* in the file CONTRIBUTORS included with the source
15
* code distribution. The list can also be seen at the
16
* following World Wide Web location:
17
* http://sources.redhat.com/pthreads-win32/contributors.html
19
* This library is free software; you can redistribute it and/or
20
* modify it under the terms of the GNU Lesser General Public
21
* License as published by the Free Software Foundation; either
22
* version 2 of the License, or (at your option) any later version.
24
* This library is distributed in the hope that it will be useful,
25
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27
* Lesser General Public License for more details.
29
* You should have received a copy of the GNU Lesser General Public
30
* License along with this library in the file COPYING.LIB;
31
* if not, write to the Free Software Foundation, Inc.,
32
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
34
* --------------------------------------------------------------------------
36
* Measure time taken to complete an elementary operation.
39
* Single thread iteration over lock/unlock for each mutex type.
43
#include <sys/timeb.h>
49
#include "benchtest.h"
51
#define PTW32_MUTEX_TYPES
52
#define ITERATIONS 10000000L
55
pthread_mutexattr_t ma;
56
struct _timeb currSysTimeStart;
57
struct _timeb currSysTimeStop;
58
long durationMilliSecs;
59
long overHeadMilliSecs = 0;
63
#define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \
64
- (_TStart.time*1000+_TStart.millitm))
67
* Dummy use of j, otherwise the loop may be removed by the optimiser
68
* when doing the overhead timing with an empty loop.
71
{ int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++;
74
}; _ftime(&currSysTimeStop); if (j + k == i) j++; }
78
runTest (char * testNameString, int mType)
80
#ifdef PTW32_MUTEX_TYPES
81
assert(pthread_mutexattr_settype(&ma, mType) == 0);
83
assert(pthread_mutex_init(&mx, &ma) == 0);
86
assert(pthread_mutex_lock(&mx) == zero);
87
assert(pthread_mutex_unlock(&mx) == zero);
90
assert(pthread_mutex_destroy(&mx) == 0);
92
durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
94
printf( "%-45s %15ld %15.3f\n",
97
(float) durationMilliSecs * 1E3 / ITERATIONS);
102
main (int argc, char *argv[])
107
pthread_mutexattr_init(&ma);
109
printf( "=============================================================================\n");
110
printf( "\nLock plus unlock on an unlocked mutex.\n%ld iterations\n\n",
112
printf( "%-45s %15s %15s\n",
116
printf( "-----------------------------------------------------------------------------\n");
119
* Time the loop overhead so we can subtract it from the actual test times.
127
durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
128
overHeadMilliSecs = durationMilliSecs;
132
assert((dummy_call(&i), 1) == one);
133
assert((dummy_call(&i), 1) == one);
136
durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
138
printf( "%-45s %15ld %15.3f\n",
141
(float) durationMilliSecs * 1E3 / ITERATIONS);
145
assert((interlocked_inc_with_conditionals(&i), 1) == one);
146
assert((interlocked_dec_with_conditionals(&i), 1) == one);
149
durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
151
printf( "%-45s %15ld %15.3f\n",
152
"Dummy call -> Interlocked with cond x 2",
154
(float) durationMilliSecs * 1E3 / ITERATIONS);
158
assert((InterlockedIncrement((LPLONG)&i), 1) == (LONG)one);
159
assert((InterlockedDecrement((LPLONG)&i), 1) == (LONG)one);
162
durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
164
printf( "%-45s %15ld %15.3f\n",
167
(float) durationMilliSecs * 1E3 / ITERATIONS);
170
InitializeCriticalSection(&cs);
173
assert((EnterCriticalSection(&cs), 1) == one);
174
assert((LeaveCriticalSection(&cs), 1) == one);
177
DeleteCriticalSection(&cs);
179
durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
181
printf( "%-45s %15ld %15.3f\n",
182
"Simple Critical Section",
184
(float) durationMilliSecs * 1E3 / ITERATIONS);
187
old_mutex_use = OLD_WIN32CS;
188
assert(old_mutex_init(&ox, NULL) == 0);
191
assert(old_mutex_lock(&ox) == zero);
192
assert(old_mutex_unlock(&ox) == zero);
195
assert(old_mutex_destroy(&ox) == 0);
197
durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
199
printf( "%-45s %15ld %15.3f\n",
200
"Old PT Mutex using a Critical Section (WNT)",
202
(float) durationMilliSecs * 1E3 / ITERATIONS);
205
old_mutex_use = OLD_WIN32MUTEX;
206
assert(old_mutex_init(&ox, NULL) == 0);
209
assert(old_mutex_lock(&ox) == zero);
210
assert(old_mutex_unlock(&ox) == zero);
213
assert(old_mutex_destroy(&ox) == 0);
215
durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
217
printf( "%-45s %15ld %15.3f\n",
218
"Old PT Mutex using a Win32 Mutex (W9x)",
220
(float) durationMilliSecs * 1E3 / ITERATIONS);
222
printf( ".............................................................................\n");
225
* Now we can start the actual tests
227
#ifdef PTW32_MUTEX_TYPES
228
runTest("PTHREAD_MUTEX_DEFAULT (W9x,WNT)", PTHREAD_MUTEX_DEFAULT);
230
runTest("PTHREAD_MUTEX_NORMAL (W9x,WNT)", PTHREAD_MUTEX_NORMAL);
232
runTest("PTHREAD_MUTEX_ERRORCHECK (W9x,WNT)", PTHREAD_MUTEX_ERRORCHECK);
234
runTest("PTHREAD_MUTEX_RECURSIVE (W9x,WNT)", PTHREAD_MUTEX_RECURSIVE);
236
runTest("Non-blocking lock", 0);
239
printf( "=============================================================================\n");
245
pthread_mutexattr_destroy(&ma);
247
one = i; /* Dummy assignment to avoid 'variable unused' warning */