]> git.saurik.com Git - apple/xnu.git/blob - iokit/Tests/TestDevice.cpp
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / iokit / Tests / TestDevice.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #if DEBUG
29
30 #include "Tests.h"
31
32 #include <IOKit/IOCommandQueue.h>
33 #include <IOKit/IOInterruptEventSource.h>
34 #include <IOKit/IOWorkLoop.h>
35
36 #include <mach/sync_policy.h>
37
38 #define super OSObject
39
40 static TestDevice *sDevice;
41
42 static mach_timespec_t hundredMill = { 0, 100000000 };
43 static semaphore_port_t completeSema;
44
45 OSDefineMetaClassAndStructors(TestDevice, OSObject)
46
47 kern_return_t
48 TestDevice::enqueueCommand(bool sleep,
49 TestDeviceAction act, int tag, void *dataP)
50 {
51 return commQ->enqueueCommand(sleep, (void *) act, (void *) tag, dataP);
52 }
53
54 bool
55 TestDevice::init()
56 {
57 if (!super::init()) {
58 return false;
59 }
60
61 workLoop = IOWorkLoop::workLoop();
62 if (!workLoop) {
63 return false;
64 }
65
66 commQ = IOCommandQueue::commandQueue
67 (this, (IOCommandQueueAction) & rawCommandOccurred, 8);
68 if (!commQ || kIOReturnSuccess != workLoop->addEventSource(commQ)) {
69 return false;
70 }
71
72 intES = IOInterruptEventSource::interruptEventSource
73 (this, (IOInterruptEventAction) & interruptAction);
74 if (!intES || kIOReturnSuccess != workLoop->addEventSource(intES)) {
75 return false;
76 }
77
78 return true;
79 }
80
81 void
82 TestDevice::free()
83 {
84 if (intES) {
85 intES->release();
86 }
87 if (commQ) {
88 commQ->release();
89 }
90 if (workLoop) {
91 workLoop->release();
92 }
93
94 super::free();
95 }
96
97 void
98 TestDevice::rawCommandOccurred
99 (void *field0, void *field1, void *field2, void *)
100 {
101 (*(TestDeviceAction) field0)(this, (int) field1, field2);
102 }
103
104 void
105 TestDevice::interruptAction(IOInterruptEventSource *, int count)
106 {
107 logPrintf(("I(%d, %d) ", count, ++intCount));
108 }
109
110 void
111 TestDevice::producer1Action(int tag)
112 {
113 logPrintf(("C1(%d) ", tag));
114 }
115
116 void
117 TestDevice::producer2Action(int tag, void *count)
118 {
119 logPrintf(("C2(%d,%d) ", tag, (int) count));
120 if (!(tag % 10)) {
121 IOSleep(1000);
122 }
123 }
124
125 void
126 TestDevice::alarm()
127 {
128 intES->interruptOccurred(0, 0, 0);
129 IOScheduleFunc((IOThreadFunc) alarm, (void *) this, hundredMill, 1);
130 }
131
132 static void
133 producer(void *inProducerId)
134 {
135 int producerId = (int) inProducerId;
136 TestDeviceAction command;
137 int i;
138
139 semaphore_wait(completeSema);
140
141 if (producerId & 1) {
142 command = (TestDeviceAction) sDevice->producer1Action;
143 } else {
144 command = (TestDeviceAction) sDevice->producer2Action;
145 }
146
147 for (i = 0; i < 5 * (producerId << 1); i++) {
148 sDevice->enqueueCommand
149 (true, command, i, (void *) (i % (producerId + 1)));
150 if (!(i % (producerId + 1))) {
151 /* cthread_yield() */;
152 }
153 logPrintf(("TestDevice(%d): %d\n", producerId, i));
154 }
155
156 logPrintf(("TestDevice: producer %d exiting\n", producerId));
157 semaphore_signal(completeSema);
158
159 IOExitThread(producerId);
160 }
161
162 void
163 testWorkLoop()
164 {
165 int i;
166
167 sDevice = new TestDevice;
168 if (!sDevice || !sDevice->init()) {
169 if (sDevice) {
170 sDevice->free();
171 }
172 logPrintf(("TestDevice: couldn't create device instance\n"));
173 return;
174 }
175
176 IOSleep(1000);
177
178 IOScheduleFunc((IOThreadFunc) sDevice->alarm, sDevice, hundredMill, 1);
179
180 IOSleep(2000);
181
182 if (KERN_SUCCESS
183 != semaphore_create(kernel_task, &completeSema, SYNC_POLICY_FIFO, 4)) {
184 return;
185 }
186
187 IOCreateThread(producer, (void *) 4);
188 IOCreateThread(producer, (void *) 3);
189 IOCreateThread(producer, (void *) 2);
190 IOCreateThread(producer, (void *) 1);
191
192 IOSleep(2000);
193
194 for (i = 0; i < 4; i++) {
195 semaphore_wait(completeSema);
196 }
197
198 IOUnscheduleFunc((IOThreadFunc) sDevice->alarm, sDevice);
199
200 sDevice->free(); sDevice = 0;
201
202 logPrintf(("TestDevice: exiting\n"));
203 }
204
205 #endif /* DEBUG */