2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 #include <IOKit/IOCommandQueue.h>
33 #include <IOKit/IOInterruptEventSource.h>
34 #include <IOKit/IOWorkLoop.h>
36 #include <mach/sync_policy.h>
38 #define super OSObject
40 static TestDevice
*sDevice
;
42 static mach_timespec_t hundredMill
= { 0, 100000000 };
43 static semaphore_port_t completeSema
;
45 OSDefineMetaClassAndStructors(TestDevice
, OSObject
)
48 TestDevice::enqueueCommand(bool sleep
,
49 TestDeviceAction act
, int tag
, void *dataP
)
51 return commQ
->enqueueCommand(sleep
, (void *) act
, (void *) tag
, dataP
);
61 workLoop
= IOWorkLoop::workLoop();
66 commQ
= IOCommandQueue::commandQueue
67 (this, (IOCommandQueueAction
) & rawCommandOccurred
, 8);
68 if (!commQ
|| kIOReturnSuccess
!= workLoop
->addEventSource(commQ
)) {
72 intES
= IOInterruptEventSource::interruptEventSource
73 (this, (IOInterruptEventAction
) & interruptAction
);
74 if (!intES
|| kIOReturnSuccess
!= workLoop
->addEventSource(intES
)) {
98 TestDevice::rawCommandOccurred
99 (void *field0
, void *field1
, void *field2
, void *)
101 (*(TestDeviceAction
) field0
)(this, (int) field1
, field2
);
105 TestDevice::interruptAction(IOInterruptEventSource
*, int count
)
107 logPrintf(("I(%d, %d) ", count
, ++intCount
));
111 TestDevice::producer1Action(int tag
)
113 logPrintf(("C1(%d) ", tag
));
117 TestDevice::producer2Action(int tag
, void *count
)
119 logPrintf(("C2(%d,%d) ", tag
, (int) count
));
128 intES
->interruptOccurred(0, 0, 0);
129 IOScheduleFunc((IOThreadFunc
) alarm
, (void *) this, hundredMill
, 1);
133 producer(void *inProducerId
)
135 int producerId
= (int) inProducerId
;
136 TestDeviceAction command
;
139 semaphore_wait(completeSema
);
141 if (producerId
& 1) {
142 command
= (TestDeviceAction
) sDevice
->producer1Action
;
144 command
= (TestDeviceAction
) sDevice
->producer2Action
;
147 for (i
= 0; i
< 5 * (producerId
<< 1); i
++) {
148 sDevice
->enqueueCommand
149 (true, command
, i
, (void *) (i
% (producerId
+ 1)));
150 if (!(i
% (producerId
+ 1))) {
151 /* cthread_yield() */;
153 logPrintf(("TestDevice(%d): %d\n", producerId
, i
));
156 logPrintf(("TestDevice: producer %d exiting\n", producerId
));
157 semaphore_signal(completeSema
);
159 IOExitThread(producerId
);
167 sDevice
= new TestDevice
;
168 if (!sDevice
|| !sDevice
->init()) {
172 logPrintf(("TestDevice: couldn't create device instance\n"));
178 IOScheduleFunc((IOThreadFunc
) sDevice
->alarm
, sDevice
, hundredMill
, 1);
183 != semaphore_create(kernel_task
, &completeSema
, SYNC_POLICY_FIFO
, 4)) {
187 IOCreateThread(producer
, (void *) 4);
188 IOCreateThread(producer
, (void *) 3);
189 IOCreateThread(producer
, (void *) 2);
190 IOCreateThread(producer
, (void *) 1);
194 for (i
= 0; i
< 4; i
++) {
195 semaphore_wait(completeSema
);
198 IOUnscheduleFunc((IOThreadFunc
) sDevice
->alarm
, sDevice
);
200 sDevice
->free(); sDevice
= 0;
202 logPrintf(("TestDevice: exiting\n"));