]> git.saurik.com Git - apple/xnu.git/blame - iokit/Tests/TestDevice.cpp
xnu-6153.11.26.tar.gz
[apple/xnu.git] / iokit / Tests / TestDevice.cpp
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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
40static TestDevice *sDevice;
41
42static mach_timespec_t hundredMill = { 0, 100000000 };
43static semaphore_port_t completeSema;
44
45OSDefineMetaClassAndStructors(TestDevice, OSObject)
46
47kern_return_t
48TestDevice::enqueueCommand(bool sleep,
0a7de745 49 TestDeviceAction act, int tag, void *dataP)
1c79356b 50{
0a7de745 51 return commQ->enqueueCommand(sleep, (void *) act, (void *) tag, dataP);
1c79356b
A
52}
53
0a7de745
A
54bool
55TestDevice::init()
1c79356b 56{
0a7de745
A
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;
1c79356b
A
79}
80
0a7de745
A
81void
82TestDevice::free()
1c79356b 83{
0a7de745
A
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();
1c79356b
A
95}
96
97void
98TestDevice::rawCommandOccurred
0a7de745 99(void *field0, void *field1, void *field2, void *)
1c79356b 100{
0a7de745 101 (*(TestDeviceAction) field0)(this, (int) field1, field2);
1c79356b
A
102}
103
104void
105TestDevice::interruptAction(IOInterruptEventSource *, int count)
106{
0a7de745 107 logPrintf(("I(%d, %d) ", count, ++intCount));
1c79356b
A
108}
109
110void
111TestDevice::producer1Action(int tag)
112{
0a7de745 113 logPrintf(("C1(%d) ", tag));
1c79356b
A
114}
115
116void
117TestDevice::producer2Action(int tag, void *count)
118{
0a7de745
A
119 logPrintf(("C2(%d,%d) ", tag, (int) count));
120 if (!(tag % 10)) {
121 IOSleep(1000);
122 }
1c79356b
A
123}
124
125void
126TestDevice::alarm()
127{
0a7de745
A
128 intES->interruptOccurred(0, 0, 0);
129 IOScheduleFunc((IOThreadFunc) alarm, (void *) this, hundredMill, 1);
1c79356b
A
130}
131
0a7de745
A
132static void
133producer(void *inProducerId)
1c79356b 134{
0a7de745
A
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);
1c79356b
A
160}
161
0a7de745
A
162void
163testWorkLoop()
1c79356b 164{
0a7de745 165 int i;
1c79356b 166
0a7de745
A
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 }
1c79356b 175
0a7de745 176 IOSleep(1000);
1c79356b 177
0a7de745 178 IOScheduleFunc((IOThreadFunc) sDevice->alarm, sDevice, hundredMill, 1);
1c79356b 179
0a7de745 180 IOSleep(2000);
1c79356b 181
0a7de745
A
182 if (KERN_SUCCESS
183 != semaphore_create(kernel_task, &completeSema, SYNC_POLICY_FIFO, 4)) {
184 return;
185 }
1c79356b 186
0a7de745
A
187 IOCreateThread(producer, (void *) 4);
188 IOCreateThread(producer, (void *) 3);
189 IOCreateThread(producer, (void *) 2);
190 IOCreateThread(producer, (void *) 1);
1c79356b 191
0a7de745 192 IOSleep(2000);
1c79356b 193
0a7de745
A
194 for (i = 0; i < 4; i++) {
195 semaphore_wait(completeSema);
196 }
1c79356b 197
0a7de745 198 IOUnscheduleFunc((IOThreadFunc) sDevice->alarm, sDevice);
1c79356b 199
0a7de745 200 sDevice->free(); sDevice = 0;
1c79356b 201
0a7de745 202 logPrintf(("TestDevice: exiting\n"));
1c79356b
A
203}
204
205#endif /* DEBUG */