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