xref: /xnu-11215/iokit/Tests/TestDevice.cpp (revision a3bb9fcc)
1 /*
2  * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_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. 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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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 
40 static TestDevice *sDevice;
41 
42 static mach_timespec_t hundredMill = { 0, 100000000 };
43 static semaphore_port_t completeSema;
44 
45 OSDefineMetaClassAndStructors(TestDevice, OSObject)
46 
47 kern_return_t
48 TestDevice::enqueueCommand(bool sleep,
49                            TestDeviceAction act, int tag, void *dataP)
50 {
51     return commQ->enqueueCommand(sleep, (void *) act, (void *) tag, dataP);
52 }
53 
54 bool TestDevice::init()
55 {
56     if ( !super::init() )
57         return false;
58 
59     workLoop = IOWorkLoop::workLoop();
60     if ( !workLoop )
61         return false;
62 
63     commQ = IOCommandQueue::commandQueue
64         (this, (IOCommandQueueAction) &rawCommandOccurred, 8);
65     if (!commQ || kIOReturnSuccess != workLoop->addEventSource(commQ))
66         return false;
67 
68     intES = IOInterruptEventSource::interruptEventSource
69         (this, (IOInterruptEventAction) &interruptAction);
70     if (!intES || kIOReturnSuccess != workLoop->addEventSource(intES))
71         return false;
72 
73     return true;
74 }
75 
76 void TestDevice::free()
77 {
78     if (intES)    intES->release();
79     if (commQ)    commQ->release();
80     if (workLoop) workLoop->release();
81 
82     super::free();
83 }
84 
85 void
86 TestDevice::rawCommandOccurred
87     (void *field0, void *field1, void *field2, void *)
88 {
89     (*(TestDeviceAction) field0)(this, (int) field1, field2);
90 }
91 
92 void
93 TestDevice::interruptAction(IOInterruptEventSource *, int count)
94 {
95     logPrintf(("I(%d, %d) ", count, ++intCount));
96 }
97 
98 void
99 TestDevice::producer1Action(int tag)
100 {
101     logPrintf(("C1(%d) ", tag));
102 }
103 
104 void
105 TestDevice::producer2Action(int tag, void *count)
106 {
107     logPrintf(("C2(%d,%d) ", tag, (int) count));
108     if ( !(tag % 10) )
109         IOSleep(1000);
110 }
111 
112 void
113 TestDevice::alarm()
114 {
115     intES->interruptOccurred(0, 0, 0);
116     IOScheduleFunc((IOThreadFunc) alarm, (void *) this, hundredMill, 1);
117 }
118 
119 static void producer(void *inProducerId)
120 {
121     int producerId = (int) inProducerId;
122     TestDeviceAction command;
123     int i;
124 
125     semaphore_wait(completeSema);
126 
127     if (producerId & 1)
128         command = (TestDeviceAction) sDevice->producer1Action;
129     else
130         command = (TestDeviceAction) sDevice->producer2Action;
131 
132     for (i = 0; i < 5 * (producerId << 1); i++) {
133         sDevice->enqueueCommand
134                 (true, command, i, (void *) (i % (producerId + 1)));
135         if ( !(i % (producerId + 1)) )
136             /* cthread_yield() */;
137         logPrintf(("TestDevice(%d): %d\n", producerId, i));
138     }
139 
140     logPrintf(("TestDevice: producer %d exiting\n", producerId));
141     semaphore_signal(completeSema);
142 
143     IOExitThread(producerId);
144 }
145 
146 void testWorkLoop()
147 {
148     int i;
149 
150     sDevice = new TestDevice;
151     if (!sDevice || !sDevice->init()) {
152         if (sDevice) sDevice->free();
153         logPrintf(("TestDevice: couldn't create device instance\n"));
154         return;
155     }
156 
157     IOSleep(1000);
158 
159     IOScheduleFunc((IOThreadFunc) sDevice->alarm, sDevice, hundredMill, 1);
160 
161     IOSleep(2000);
162 
163     if (KERN_SUCCESS
164     !=  semaphore_create(kernel_task, &completeSema, SYNC_POLICY_FIFO, 4))
165         return;
166 
167     IOCreateThread(producer, (void *) 4);
168     IOCreateThread(producer, (void *) 3);
169     IOCreateThread(producer, (void *) 2);
170     IOCreateThread(producer, (void *) 1);
171 
172     IOSleep(2000);
173 
174     for (i = 0; i < 4; i++)
175         semaphore_wait(completeSema);
176 
177     IOUnscheduleFunc((IOThreadFunc) sDevice->alarm, sDevice);
178 
179     sDevice->free(); sDevice = 0;
180 
181     logPrintf(("TestDevice: exiting\n"));
182 }
183 
184 #endif /* DEBUG */
185