|
|
1.1 root 1: /*
2: * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
6: * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7: * Reserved. This file contains Original Code and/or Modifications of
8: * Original Code as defined in and that are subject to the Apple Public
9: * Source License Version 1.1 (the "License"). You may not use this file
10: * except in compliance with the License. Please obtain a copy of the
11: * License at http://www.apple.com/publicsource and read it before using
12: * this file.
13: *
14: * The Original Code and all software distributed under the License are
15: * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19: * License for the specific language governing rights and limitations
20: * under the License.
21: *
22: * @APPLE_LICENSE_HEADER_END@
23: */
24: /*
25: * Test of NXLock, NXConditionLock, and NXSpinLock (user version).
26: */
27:
28: #import <machkit/NXLock.h>
29: #import <driverkit/generalFuncs.h>
30: #import <bsd/sys/printf.h>
31:
32: static int ioThread();
33:
34: id lock;
35: id condLock;
36: id spinLock;
37:
38: /*
39: * flags meean "I'm waiting for you" when YES.
40: */
41: BOOL threadFlag = NO;
42: BOOL mainFlag = NO;
43:
44: /*
45: * part main thread
46: * ---- ------------------- -----------------------
47: * 1 mainFlag NO threadFlag NO
48: * grab spin lock wait for mainFlag
49: * mainFlag YES
50: * wait for threadFlag
51: * threadFlag YES
52: * try to acquire spinLock (held by main)
53: * mainFlag NO
54: * sleep
55: * unlock spin lock
56: * wait for threadFlag NO
57: * unlock spinLock
58: * threadFlag NO
59: *
60: * 2 grab lock wait for mainFlag
61: * mainFlag YES
62: * wait for threadFlag
63: * threadFlag YES
64: * try to acquire lock (held by main)
65: * mainFlag NO
66: * sleep
67: * unlock lock
68: * wait for threadFlag NO
69: * unlock spinLock
70: * threadFlag NO
71: *
72: * 3 grab condLock wait for mainFlag
73: * mainFlag YES
74: * wait for threadFlag
75: * threadFlag YES
76: * [condLock lockWhen 1]
77: * mainFlag NO
78: * sleep
79: * [condLock unlockWith 1]
80: * [condLock lockWhen 2]
81: * sleep
82: * [condLock unlockWith 2]
83: * done done
84: */
85:
86: int main(int argc, char **argv)
87: {
88: /*
89: * Init the three locks.
90: */
91: lock = [NXLock new];
92: condLock = [NXConditionLock alloc];
93: [condLock initWith:0];
94: spinLock = [NXSpinLock new];
95:
96: /*
97: * Start up the I/O thread.
98: */
99: IOForkThread((IOThreadFunc)ioThread, (void *)ioThread);
100:
101: /*
102: * Part 1.
103: */
104: [spinLock lock];
105: mainFlag = YES;
106: while(!threadFlag)
107: ;
108: mainFlag = NO;
109: printf("1 (main) - threadFlag YES, main() holds spin lock\n");
110: IOSleep(1000);
111: [spinLock unlock];
112:
113: /*
114: * Wait for I/O Thread to finish part 1.
115: */
116: while(threadFlag)
117: ;
118:
119: /*
120: * Part 2.
121: */
122: [lock lock];
123: mainFlag = YES;
124: while(!threadFlag)
125: ;
126: mainFlag = NO;
127: printf("2 (main) - threadFlag YES, main() holds lock\n");
128: IOSleep(1000);
129: [lock unlock];
130:
131: /*
132: * Wait for I/O Thread to finish part 2.
133: */
134: while(threadFlag)
135: ;
136:
137: /*
138: * Part 3.
139: */
140: [condLock lock];
141: mainFlag = YES;
142: while(!threadFlag)
143: ;
144: printf("3 (main) - threadFlag YES, main() holds condLock\n");
145: IOSleep(1000);
146: [condLock unlockWith:1];
147: [condLock lockWhen:2];
148: printf("3 (main) - got condLock:2; done\n");
149: return(0);
150: }
151:
152: static int ioThread()
153: {
154: /*
155: * part 1.
156: */
157: while(!mainFlag)
158: ;
159: threadFlag = YES;
160: [spinLock lock];
161: printf("1 (thread) - thread got spinLock\n");
162: [spinLock unlock];
163: threadFlag = NO;
164:
165: /*
166: * part 2.
167: */
168: while(!mainFlag)
169: ;
170: threadFlag = YES;
171: [lock lock];
172: printf("2 (thread) - thread got lock\n");
173: [lock unlock];
174: threadFlag = NO;
175:
176: /*
177: * Part 3.
178: */
179: while(!mainFlag)
180: ;
181: threadFlag = YES;
182: [condLock lockWhen:1];
183: printf("3 (thread) - got condLock:1\n");
184: IOSleep(1000);
185: [condLock unlockWith:2];
186: IOExitThread();
187: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.