2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <IOKit/IOCommandQueue.h>
28 #include <IOKit/IOInterruptEventSource.h>
29 #include <IOKit/IOWorkLoop.h>
31 #include <mach/sync_policy.h>
33 #define super OSObject
35 static TestDevice
*sDevice
;
37 static mach_timespec_t hundredMill
= { 0, 100000000 };
38 static semaphore_port_t completeSema
;
40 OSDefineMetaClassAndStructors(TestDevice
, OSObject
)
43 TestDevice::enqueueCommand(bool sleep
,
44 TestDeviceAction act
, int tag
, void *dataP
)
46 return commQ
->enqueueCommand(sleep
, (void *) act
, (void *) tag
, dataP
);
49 bool TestDevice::init()
54 workLoop
= IOWorkLoop::workLoop();
58 commQ
= IOCommandQueue::commandQueue
59 (this, (IOCommandQueueAction
) &rawCommandOccurred
, 8);
60 if (!commQ
|| kIOReturnSuccess
!= workLoop
->addEventSource(commQ
))
63 intES
= IOInterruptEventSource::interruptEventSource
64 (this, (IOInterruptEventAction
) &interruptAction
);
65 if (!intES
|| kIOReturnSuccess
!= workLoop
->addEventSource(intES
))
71 void TestDevice::free()
73 if (intES
) intES
->release();
74 if (commQ
) commQ
->release();
75 if (workLoop
) workLoop
->release();
81 TestDevice::rawCommandOccurred
82 (void *field0
, void *field1
, void *field2
, void *)
84 (*(TestDeviceAction
) field0
)(this, (int) field1
, field2
);
88 TestDevice::interruptAction(IOInterruptEventSource
*, int count
)
90 logPrintf(("I(%d, %d) ", count
, ++intCount
));
94 TestDevice::producer1Action(int tag
)
96 logPrintf(("C1(%d) ", tag
));
100 TestDevice::producer2Action(int tag
, void *count
)
102 logPrintf(("C2(%d,%d) ", tag
, (int) count
));
110 intES
->interruptOccurred(0, 0, 0);
111 IOScheduleFunc((IOThreadFunc
) alarm
, (void *) this, hundredMill
, 1);
114 static void producer(void *inProducerId
)
116 int producerId
= (int) inProducerId
;
117 TestDeviceAction command
;
120 semaphore_wait(completeSema
);
123 command
= (TestDeviceAction
) sDevice
->producer1Action
;
125 command
= (TestDeviceAction
) sDevice
->producer2Action
;
127 for (i
= 0; i
< 5 * (producerId
<< 1); i
++) {
128 sDevice
->enqueueCommand
129 (true, command
, i
, (void *) (i
% (producerId
+ 1)));
130 if ( !(i
% (producerId
+ 1)) )
131 /* cthread_yield() */;
132 logPrintf(("TestDevice(%d): %d\n", producerId
, i
));
135 logPrintf(("TestDevice: producer %d exiting\n", producerId
));
136 semaphore_signal(completeSema
);
138 IOExitThread(producerId
);
145 sDevice
= new TestDevice
;
146 if (!sDevice
|| !sDevice
->init()) {
147 if (sDevice
) sDevice
->free();
148 logPrintf(("TestDevice: couldn't create device instance\n"));
154 IOScheduleFunc((IOThreadFunc
) sDevice
->alarm
, sDevice
, hundredMill
, 1);
159 != semaphore_create(kernel_task
, &completeSema
, SYNC_POLICY_FIFO
, 4))
162 IOCreateThread(producer
, (void *) 4);
163 IOCreateThread(producer
, (void *) 3);
164 IOCreateThread(producer
, (void *) 2);
165 IOCreateThread(producer
, (void *) 1);
169 for (i
= 0; i
< 4; i
++)
170 semaphore_wait(completeSema
);
172 IOUnscheduleFunc((IOThreadFunc
) sDevice
->alarm
, sDevice
);
174 sDevice
->free(); sDevice
= 0;
176 logPrintf(("TestDevice: exiting\n"));