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
);
54 bool TestDevice::init()
59 workLoop
= IOWorkLoop::workLoop();
63 commQ
= IOCommandQueue::commandQueue
64 (this, (IOCommandQueueAction
) &rawCommandOccurred
, 8);
65 if (!commQ
|| kIOReturnSuccess
!= workLoop
->addEventSource(commQ
))
68 intES
= IOInterruptEventSource::interruptEventSource
69 (this, (IOInterruptEventAction
) &interruptAction
);
70 if (!intES
|| kIOReturnSuccess
!= workLoop
->addEventSource(intES
))
76 void TestDevice::free()
78 if (intES
) intES
->release();
79 if (commQ
) commQ
->release();
80 if (workLoop
) workLoop
->release();
86 TestDevice::rawCommandOccurred
87 (void *field0
, void *field1
, void *field2
, void *)
89 (*(TestDeviceAction
) field0
)(this, (int) field1
, field2
);
93 TestDevice::interruptAction(IOInterruptEventSource
*, int count
)
95 logPrintf(("I(%d, %d) ", count
, ++intCount
));
99 TestDevice::producer1Action(int tag
)
101 logPrintf(("C1(%d) ", tag
));
105 TestDevice::producer2Action(int tag
, void *count
)
107 logPrintf(("C2(%d,%d) ", tag
, (int) count
));
115 intES
->interruptOccurred(0, 0, 0);
116 IOScheduleFunc((IOThreadFunc
) alarm
, (void *) this, hundredMill
, 1);
119 static void producer(void *inProducerId
)
121 int producerId
= (int) inProducerId
;
122 TestDeviceAction command
;
125 semaphore_wait(completeSema
);
128 command
= (TestDeviceAction
) sDevice
->producer1Action
;
130 command
= (TestDeviceAction
) sDevice
->producer2Action
;
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
));
140 logPrintf(("TestDevice: producer %d exiting\n", producerId
));
141 semaphore_signal(completeSema
);
143 IOExitThread(producerId
);
150 sDevice
= new TestDevice
;
151 if (!sDevice
|| !sDevice
->init()) {
152 if (sDevice
) sDevice
->free();
153 logPrintf(("TestDevice: couldn't create device instance\n"));
159 IOScheduleFunc((IOThreadFunc
) sDevice
->alarm
, sDevice
, hundredMill
, 1);
164 != semaphore_create(kernel_task
, &completeSema
, SYNC_POLICY_FIFO
, 4))
167 IOCreateThread(producer
, (void *) 4);
168 IOCreateThread(producer
, (void *) 3);
169 IOCreateThread(producer
, (void *) 2);
170 IOCreateThread(producer
, (void *) 1);
174 for (i
= 0; i
< 4; i
++)
175 semaphore_wait(completeSema
);
177 IOUnscheduleFunc((IOThreadFunc
) sDevice
->alarm
, sDevice
);
179 sDevice
->free(); sDevice
= 0;
181 logPrintf(("TestDevice: exiting\n"));