]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOCommandPool.cpp
xnu-6153.11.26.tar.gz
[apple/xnu.git] / iokit / Kernel / IOCommandPool.cpp
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28
29/*
30 *
31 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
32 *
33 * HISTORY
34 *
35 * 2001-01-17 gvdl Re-implement on IOCommandGate::commandSleep
36 * 10/9/2000 CJS Created IOCommandPool class and implementation
37 *
38 */
39
40#include <IOKit/IOCommandPool.h>
41
42#define super OSObject
43OSDefineMetaClassAndStructors(IOCommandPool, OSObject);
44OSMetaClassDefineReservedUnused(IOCommandPool, 0);
45OSMetaClassDefineReservedUnused(IOCommandPool, 1);
46OSMetaClassDefineReservedUnused(IOCommandPool, 2);
47OSMetaClassDefineReservedUnused(IOCommandPool, 3);
48OSMetaClassDefineReservedUnused(IOCommandPool, 4);
49OSMetaClassDefineReservedUnused(IOCommandPool, 5);
50OSMetaClassDefineReservedUnused(IOCommandPool, 6);
51OSMetaClassDefineReservedUnused(IOCommandPool, 7);
52
53//--------------------------------------------------------------------------
54// withWorkLoop - primary initializer and factory method
55//--------------------------------------------------------------------------
56
0a7de745
A
57IOCommandPool *
58IOCommandPool::
1c79356b
A
59withWorkLoop(IOWorkLoop *inWorkLoop)
60{
0a7de745
A
61 IOCommandPool * me = new IOCommandPool;
62
63 if (me && !me->initWithWorkLoop(inWorkLoop)) {
64 me->release();
cb323159 65 return NULL;
0a7de745
A
66 }
67
68 return me;
1c79356b
A
69}
70
71
0a7de745
A
72bool
73IOCommandPool::
1c79356b
A
74initWithWorkLoop(IOWorkLoop *inWorkLoop)
75{
0a7de745
A
76 assert(inWorkLoop);
77
78 if (!super::init()) {
79 return false;
80 }
81
82 queue_init(&fQueueHead);
83
84 fSerializer = IOCommandGate::commandGate(this);
85 assert(fSerializer);
86 if (!fSerializer) {
87 return false;
88 }
89
90 if (kIOReturnSuccess != inWorkLoop->addEventSource(fSerializer)) {
91 return false;
92 }
93
94 return true;
1c79356b
A
95}
96
97//--------------------------------------------------------------------------
98// commandPool & init - obsolete initializer and factory method
99//--------------------------------------------------------------------------
100
0a7de745
A
101IOCommandPool *
102IOCommandPool::
1c79356b
A
103commandPool(IOService * inOwner, IOWorkLoop *inWorkLoop, UInt32 inSize)
104{
0a7de745
A
105 IOCommandPool * me = new IOCommandPool;
106
107 if (me && !me->init(inOwner, inWorkLoop, inSize)) {
108 me->release();
cb323159 109 return NULL;
0a7de745
A
110 }
111
112 return me;
1c79356b
A
113}
114
0a7de745
A
115bool
116IOCommandPool::
1c79356b
A
117init(IOService */* inOwner */, IOWorkLoop *inWorkLoop, UInt32 /* inSize */)
118{
0a7de745 119 return initWithWorkLoop(inWorkLoop);
1c79356b
A
120}
121
122
123//--------------------------------------------------------------------------
124// free - free all allocated resources
125//--------------------------------------------------------------------------
126
127void
128IOCommandPool::free(void)
129{
0a7de745
A
130 if (fSerializer) {
131 // remove our event source from owner's workloop
132 IOWorkLoop *wl = fSerializer->getWorkLoop();
133 if (wl) {
134 wl->removeEventSource(fSerializer);
135 }
136
137 fSerializer->release();
cb323159 138 fSerializer = NULL;
0a7de745
A
139 }
140
141 // Tell our superclass to cleanup too
142 super::free();
1c79356b
A
143}
144
145
146//--------------------------------------------------------------------------
147// getCommand - Gets a command from the pool. Pass true in
148// blockForCommand if you want your thread to sleep
149// waiting for resources
150//--------------------------------------------------------------------------
151
152IOCommand *
153IOCommandPool::getCommand(bool blockForCommand)
154{
0a7de745 155 IOReturn result = kIOReturnSuccess;
cb323159 156 IOCommand *command = NULL;
0a7de745
A
157
158 IOCommandGate::Action func = OSMemberFunctionCast(
159 IOCommandGate::Action, this, &IOCommandPool::gatedGetCommand);
160 result = fSerializer->
161 runAction(func, (void *) &command, (void *) blockForCommand);
162 if (kIOReturnSuccess == result) {
163 return command;
164 } else {
cb323159 165 return NULL;
0a7de745 166 }
1c79356b
A
167}
168
169
170//--------------------------------------------------------------------------
171// gatedGetCommand - Static callthrough function
172// (on safe side of command gate)
173//--------------------------------------------------------------------------
174
0a7de745
A
175IOReturn
176IOCommandPool::
1c79356b
A
177gatedGetCommand(IOCommand **command, bool blockForCommand)
178{
0a7de745
A
179 while (queue_empty(&fQueueHead)) {
180 if (!blockForCommand) {
181 return kIOReturnNoResources;
182 }
183
184 fSleepers++;
185 fSerializer->commandSleep(&fSleepers, THREAD_UNINT);
186 }
187
188 queue_remove_first(&fQueueHead,
189 *command, IOCommand *, fCommandChain);
190 return kIOReturnSuccess;
1c79356b
A
191}
192
193
194//--------------------------------------------------------------------------
195// returnCommand - Returns command to the pool.
196//--------------------------------------------------------------------------
197
0a7de745
A
198void
199IOCommandPool::
1c79356b
A
200returnCommand(IOCommand *command)
201{
0a7de745
A
202 IOCommandGate::Action func = OSMemberFunctionCast(
203 IOCommandGate::Action, this, &IOCommandPool::gatedReturnCommand);
204 (void) fSerializer->runAction(func, (void *) command);
1c79356b
A
205}
206
207
208//--------------------------------------------------------------------------
209// gatedReturnCommand - Callthrough function
0a7de745 210// (on safe side of command gate)
1c79356b
A
211//--------------------------------------------------------------------------
212
0a7de745
A
213IOReturn
214IOCommandPool::
1c79356b
A
215gatedReturnCommand(IOCommand *command)
216{
0a7de745
A
217 queue_enter_first(&fQueueHead, command, IOCommand *, fCommandChain);
218 if (fSleepers) {
219 fSerializer->commandWakeup(&fSleepers, /* oneThread */ true);
220 fSleepers--;
221 }
222 return kIOReturnSuccess;
1c79356b 223}