2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 #include <IOKit/IOCommandQueue.h>
35 #include <IOKit/IOInterruptEventSource.h>
36 #include <IOKit/IOWorkLoop.h>
38 #include <mach/sync_policy.h>
40 #define super OSObject
42 static TestDevice
*sDevice
;
44 static mach_timespec_t hundredMill
= { 0, 100000000 };
45 static semaphore_port_t completeSema
;
47 OSDefineMetaClassAndStructors(TestDevice
, OSObject
)
50 TestDevice::enqueueCommand(bool sleep
,
51 TestDeviceAction act
, int tag
, void *dataP
)
53 return commQ
->enqueueCommand(sleep
, (void *) act
, (void *) tag
, dataP
);
56 bool TestDevice::init()
61 workLoop
= IOWorkLoop::workLoop();
65 commQ
= IOCommandQueue::commandQueue
66 (this, (IOCommandQueueAction
) &rawCommandOccurred
, 8);
67 if (!commQ
|| kIOReturnSuccess
!= workLoop
->addEventSource(commQ
))
70 intES
= IOInterruptEventSource::interruptEventSource
71 (this, (IOInterruptEventAction
) &interruptAction
);
72 if (!intES
|| kIOReturnSuccess
!= workLoop
->addEventSource(intES
))
78 void TestDevice::free()
80 if (intES
) intES
->release();
81 if (commQ
) commQ
->release();
82 if (workLoop
) workLoop
->release();
88 TestDevice::rawCommandOccurred
89 (void *field0
, void *field1
, void *field2
, void *)
91 (*(TestDeviceAction
) field0
)(this, (int) field1
, field2
);
95 TestDevice::interruptAction(IOInterruptEventSource
*, int count
)
97 logPrintf(("I(%d, %d) ", count
, ++intCount
));
101 TestDevice::producer1Action(int tag
)
103 logPrintf(("C1(%d) ", tag
));
107 TestDevice::producer2Action(int tag
, void *count
)
109 logPrintf(("C2(%d,%d) ", tag
, (int) count
));
117 intES
->interruptOccurred(0, 0, 0);
118 IOScheduleFunc((IOThreadFunc
) alarm
, (void *) this, hundredMill
, 1);
121 static void producer(void *inProducerId
)
123 int producerId
= (int) inProducerId
;
124 TestDeviceAction command
;
127 semaphore_wait(completeSema
);
130 command
= (TestDeviceAction
) sDevice
->producer1Action
;
132 command
= (TestDeviceAction
) sDevice
->producer2Action
;
134 for (i
= 0; i
< 5 * (producerId
<< 1); i
++) {
135 sDevice
->enqueueCommand
136 (true, command
, i
, (void *) (i
% (producerId
+ 1)));
137 if ( !(i
% (producerId
+ 1)) )
138 /* cthread_yield() */;
139 logPrintf(("TestDevice(%d): %d\n", producerId
, i
));
142 logPrintf(("TestDevice: producer %d exiting\n", producerId
));
143 semaphore_signal(completeSema
);
145 IOExitThread(producerId
);
152 sDevice
= new TestDevice
;
153 if (!sDevice
|| !sDevice
->init()) {
154 if (sDevice
) sDevice
->free();
155 logPrintf(("TestDevice: couldn't create device instance\n"));
161 IOScheduleFunc((IOThreadFunc
) sDevice
->alarm
, sDevice
, hundredMill
, 1);
166 != semaphore_create(kernel_task
, &completeSema
, SYNC_POLICY_FIFO
, 4))
169 IOCreateThread(producer
, (void *) 4);
170 IOCreateThread(producer
, (void *) 3);
171 IOCreateThread(producer
, (void *) 2);
172 IOCreateThread(producer
, (void *) 1);
176 for (i
= 0; i
< 4; i
++)
177 semaphore_wait(completeSema
);
179 IOUnscheduleFunc((IOThreadFunc
) sDevice
->alarm
, sDevice
);
181 sDevice
->free(); sDevice
= 0;
183 logPrintf(("TestDevice: exiting\n"));