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