2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 #include <IOKit/IOCommandQueue.h>
30 #include <IOKit/IOInterruptEventSource.h>
31 #include <IOKit/IOWorkLoop.h>
33 #include <mach/sync_policy.h>
35 #define super OSObject
37 static TestDevice
*sDevice
;
39 static mach_timespec_t hundredMill
= { 0, 100000000 };
40 static semaphore_port_t completeSema
;
42 OSDefineMetaClassAndStructors(TestDevice
, OSObject
)
45 TestDevice::enqueueCommand(bool sleep
,
46 TestDeviceAction act
, int tag
, void *dataP
)
48 return commQ
->enqueueCommand(sleep
, (void *) act
, (void *) tag
, dataP
);
51 bool TestDevice::init()
56 workLoop
= IOWorkLoop::workLoop();
60 commQ
= IOCommandQueue::commandQueue
61 (this, (IOCommandQueueAction
) &rawCommandOccurred
, 8);
62 if (!commQ
|| kIOReturnSuccess
!= workLoop
->addEventSource(commQ
))
65 intES
= IOInterruptEventSource::interruptEventSource
66 (this, (IOInterruptEventAction
) &interruptAction
);
67 if (!intES
|| kIOReturnSuccess
!= workLoop
->addEventSource(intES
))
73 void TestDevice::free()
75 if (intES
) intES
->release();
76 if (commQ
) commQ
->release();
77 if (workLoop
) workLoop
->release();
83 TestDevice::rawCommandOccurred
84 (void *field0
, void *field1
, void *field2
, void *)
86 (*(TestDeviceAction
) field0
)(this, (int) field1
, field2
);
90 TestDevice::interruptAction(IOInterruptEventSource
*, int count
)
92 logPrintf(("I(%d, %d) ", count
, ++intCount
));
96 TestDevice::producer1Action(int tag
)
98 logPrintf(("C1(%d) ", tag
));
102 TestDevice::producer2Action(int tag
, void *count
)
104 logPrintf(("C2(%d,%d) ", tag
, (int) count
));
112 intES
->interruptOccurred(0, 0, 0);
113 IOScheduleFunc((IOThreadFunc
) alarm
, (void *) this, hundredMill
, 1);
116 static void producer(void *inProducerId
)
118 int producerId
= (int) inProducerId
;
119 TestDeviceAction command
;
122 semaphore_wait(completeSema
);
125 command
= (TestDeviceAction
) sDevice
->producer1Action
;
127 command
= (TestDeviceAction
) sDevice
->producer2Action
;
129 for (i
= 0; i
< 5 * (producerId
<< 1); i
++) {
130 sDevice
->enqueueCommand
131 (true, command
, i
, (void *) (i
% (producerId
+ 1)));
132 if ( !(i
% (producerId
+ 1)) )
133 /* cthread_yield() */;
134 logPrintf(("TestDevice(%d): %d\n", producerId
, i
));
137 logPrintf(("TestDevice: producer %d exiting\n", producerId
));
138 semaphore_signal(completeSema
);
140 IOExitThread(producerId
);
147 sDevice
= new TestDevice
;
148 if (!sDevice
|| !sDevice
->init()) {
149 if (sDevice
) sDevice
->free();
150 logPrintf(("TestDevice: couldn't create device instance\n"));
156 IOScheduleFunc((IOThreadFunc
) sDevice
->alarm
, sDevice
, hundredMill
, 1);
161 != semaphore_create(kernel_task
, &completeSema
, SYNC_POLICY_FIFO
, 4))
164 IOCreateThread(producer
, (void *) 4);
165 IOCreateThread(producer
, (void *) 3);
166 IOCreateThread(producer
, (void *) 2);
167 IOCreateThread(producer
, (void *) 1);
171 for (i
= 0; i
< 4; i
++)
172 semaphore_wait(completeSema
);
174 IOUnscheduleFunc((IOThreadFunc
) sDevice
->alarm
, sDevice
);
176 sDevice
->free(); sDevice
= 0;
178 logPrintf(("TestDevice: exiting\n"));