2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 #include <IOKit/IOCommandQueue.h>
27 #include <IOKit/IOInterruptEventSource.h>
28 #include <IOKit/IOWorkLoop.h>
30 #include <mach/sync_policy.h>
32 #define super OSObject
34 static TestDevice
*sDevice
;
36 static mach_timespec_t hundredMill
= { 0, 100000000 };
37 static semaphore_port_t completeSema
;
39 OSDefineMetaClassAndStructors(TestDevice
, OSObject
)
42 TestDevice::enqueueCommand(bool sleep
,
43 TestDeviceAction act
, int tag
, void *dataP
)
45 return commQ
->enqueueCommand(sleep
, (void *) act
, (void *) tag
, dataP
);
48 bool TestDevice::init()
53 workLoop
= IOWorkLoop::workLoop();
57 commQ
= IOCommandQueue::commandQueue
58 (this, (IOCommandQueueAction
) &rawCommandOccurred
, 8);
59 if (!commQ
|| kIOReturnSuccess
!= workLoop
->addEventSource(commQ
))
62 intES
= IOInterruptEventSource::interruptEventSource
63 (this, (IOInterruptEventAction
) &interruptAction
);
64 if (!intES
|| kIOReturnSuccess
!= workLoop
->addEventSource(intES
))
70 void TestDevice::free()
72 if (intES
) intES
->release();
73 if (commQ
) commQ
->release();
74 if (workLoop
) workLoop
->release();
80 TestDevice::rawCommandOccurred
81 (void *field0
, void *field1
, void *field2
, void *)
83 (*(TestDeviceAction
) field0
)(this, (int) field1
, field2
);
87 TestDevice::interruptAction(IOInterruptEventSource
*, int count
)
89 logPrintf(("I(%d, %d) ", count
, ++intCount
));
93 TestDevice::producer1Action(int tag
)
95 logPrintf(("C1(%d) ", tag
));
99 TestDevice::producer2Action(int tag
, void *count
)
101 logPrintf(("C2(%d,%d) ", tag
, (int) count
));
109 intES
->interruptOccurred(0, 0, 0);
110 IOScheduleFunc((IOThreadFunc
) alarm
, (void *) this, hundredMill
, 1);
113 static void producer(void *inProducerId
)
115 int producerId
= (int) inProducerId
;
116 TestDeviceAction command
;
119 semaphore_wait(completeSema
);
122 command
= (TestDeviceAction
) sDevice
->producer1Action
;
124 command
= (TestDeviceAction
) sDevice
->producer2Action
;
126 for (i
= 0; i
< 5 * (producerId
<< 1); i
++) {
127 sDevice
->enqueueCommand
128 (true, command
, i
, (void *) (i
% (producerId
+ 1)));
129 if ( !(i
% (producerId
+ 1)) )
130 /* cthread_yield() */;
131 logPrintf(("TestDevice(%d): %d\n", producerId
, i
));
134 logPrintf(("TestDevice: producer %d exiting\n", producerId
));
135 semaphore_signal(completeSema
);
137 IOExitThread(producerId
);
144 sDevice
= new TestDevice
;
145 if (!sDevice
|| !sDevice
->init()) {
146 if (sDevice
) sDevice
->free();
147 logPrintf(("TestDevice: couldn't create device instance\n"));
153 IOScheduleFunc((IOThreadFunc
) sDevice
->alarm
, sDevice
, hundredMill
, 1);
158 != semaphore_create(kernel_task
, &completeSema
, SYNC_POLICY_FIFO
, 4))
161 IOCreateThread(producer
, (void *) 4);
162 IOCreateThread(producer
, (void *) 3);
163 IOCreateThread(producer
, (void *) 2);
164 IOCreateThread(producer
, (void *) 1);
168 for (i
= 0; i
< 4; i
++)
169 semaphore_wait(completeSema
);
171 IOUnscheduleFunc((IOThreadFunc
) sDevice
->alarm
, sDevice
);
173 sDevice
->free(); sDevice
= 0;
175 logPrintf(("TestDevice: exiting\n"));