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