findlanabyname-20040228
[openafs.git] / src / WINNT / client_osi / perf.c
1 /* 
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* Copyright (C) 1994 Cazamar Systems, Inc. */
11
12 #include <afs/param.h>
13 #include <afs/stds.h>
14
15 #include "windows.h"
16 #include <string.h>
17 #include "main.h"
18 #include "perf.h"
19 #include "osi.h"
20 #include <assert.h>
21
22 static osi_mutex_t main_perfMutex;
23 static done;
24
25 #define STARTA  1
26 #define STARTB  2
27 static long flags;
28 static long count;
29
30 #define main_NITERS     20000   /* bops between the two */
31
32 long main_Perf1(long parm)
33 {
34         while (1) {
35                 lock_ObtainMutex(&main_perfMutex);
36                 if (!(flags & STARTA)) {
37                         /* we're not supposed to be running */
38                         osi_SleepM((long) &flags, &main_perfMutex);
39                         continue;
40                 }
41                 
42                 /* hand off to the other guy */
43                 flags &= ~STARTA;
44                 flags |= STARTB;
45                 osi_Wakeup((long) &flags);
46
47                 /* we're running, bump the counter.
48                  * do this after hand-off, so the other guy gets to run.
49                  */
50                 count++;
51                 if (count > main_NITERS) {
52                         break;
53                 }
54                 
55                 osi_SleepM((long) &flags, &main_perfMutex);
56         }
57         done++;
58         lock_ReleaseMutex(&main_perfMutex);
59         osi_Wakeup((long) &done);       /* wakeup anyone waiting for completion */
60         return 0;
61 }
62
63 long main_Perf2(long parm)
64 {
65         while (1) {
66                 lock_ObtainMutex(&main_perfMutex);
67                 if (!(flags & STARTB)) {
68                         /* we're not supposed to be running */
69                         osi_SleepM((long) &flags, &main_perfMutex);
70                         continue;
71                 }
72                 
73                 /* hand off to the other guy */
74                 flags &= ~STARTB;
75                 flags |= STARTA;
76                 osi_Wakeup((long) &flags);
77
78                 /* we're running, bump the counter.  Do after hand-off so other
79                  * guy also gets to notice that we're done.
80                  */
81                 count++;
82                 if (count > main_NITERS) {
83                         break;
84                 }
85                 
86                 osi_SleepM((long)&flags, &main_perfMutex);
87         }
88         done++;
89         lock_ReleaseMutex(&main_perfMutex);
90         osi_Wakeup((long) &done);       /* wakeup anyone waiting for completion */
91         return 0;
92 }
93
94 main_PerfTest(HANDLE hWnd)
95 {
96         long mod1ID;
97         long mod2ID;
98         HANDLE mod1Handle;
99         HANDLE mod2Handle;
100
101         osi_Init();
102         
103         main_ForceDisplay(hWnd);
104
105         /* create three processes, two modifiers and one scanner.  The scanner
106          * checks that the basic invariants are being maintained, while the
107          * modifiers modify the global variables, maintaining certain invariants
108          * by using locks.
109          *
110          * The invariant is that global variables a and b total 100.
111          */
112         done = 0;
113         count = 0;
114         flags = STARTA;
115         
116         lock_InitializeMutex(&main_perfMutex, "perf test mutex");
117
118         mod1Handle = CreateThread((SECURITY_ATTRIBUTES *) 0, 0,
119                 (LPTHREAD_START_ROUTINE) main_Perf1, 0, 0, &mod1ID);
120         if (mod1Handle == NULL) return -1;
121
122         mod2Handle = CreateThread((SECURITY_ATTRIBUTES *) 0, 0,
123                 (LPTHREAD_START_ROUTINE) main_Perf2, 0, 0, &mod2ID);
124         if (mod2Handle == NULL) return -2;
125
126         /* start running check daemon */
127         while (1) {
128                 /* copy out count of # of dudes finished */
129                 lock_ObtainMutex(&main_perfMutex);
130                 if (done == 2) {
131                         lock_ReleaseMutex(&main_perfMutex);
132                         break;
133                 }
134                 osi_SleepM((long) &done, &main_perfMutex);
135         }
136         
137         /* done, release and finalize all locks */
138         lock_FinalizeMutex(&main_perfMutex);
139
140         /* finally clean up thread handles */
141         CloseHandle(mod1Handle);
142         CloseHandle(mod2Handle);
143
144         return 0;
145 }