1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-19 12:20:17 -08:00
emacs/mps/code/lockutw3.c
Richard Brooksby c0bb4cd3cd Removing hopenames from the master sources.
This change will be integrated but ignored (-ay) to the gg-epcore/union sources, so that they retain HopeNames.

Copied from Perforce
 Change: 24911
 ServerID: perforce.ravenbrook.com
2001-12-07 13:19:25 +00:00

93 lines
1.3 KiB
C

/* impl.c.lockutw3: LOCK UTILIZATION TEST
*
* $Id$
* Copyright (c) 2001 Ravenbrook Limited.
*/
#include "mpm.h"
#include "testlib.h"
#include "mpswin.h"
#ifndef MPS_OS_W3
#error "Relies on Win32 threads"
#endif
#define nTHREADS 4
static Lock lock;
unsigned long shared, tmp;
void incR(unsigned long i)
{
LockClaimRecursive(lock);
if (i < 100) {
while(i--) {
tmp = shared;
shared = tmp + 1;
}
} else {
incR(i >> 1);
incR(i+1 >> 1);
}
LockReleaseRecursive(lock);
}
void inc(unsigned long i)
{
incR(i+1>>1);
i >>= 1;
while (i) {
LockClaim(lock);
if (i > 10000) {
incR(5000);
i -= 5000;
}
tmp = shared;
shared = tmp+1;
i--;
LockReleaseMPM(lock);
}
}
#define COUNT 100000l
DWORD WINAPI thread0(void *p)
{
(void)p;
inc(COUNT);
return 0;
}
int main(void)
{
DWORD id;
HANDLE t[10];
unsigned i;
lock = malloc(LockSize());
Insist(lock != NULL);
LockInit(lock);
shared = 0;
for(i = 0; i < nTHREADS; i++)
t[i] = CreateThread(NULL, 0, thread0, NULL, 0, &id);
for(i = 0; i < nTHREADS; i++)
WaitForSingleObject(t[i], INFINITE);
Insist(shared == nTHREADS*COUNT);
LockFinish(lock);
fflush(stdout); /* synchronize */
fprintf(stderr, "\nConclusion: Failed to find any defects.\n");
return 0;
}