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