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