X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/1c79356b52d46aa6b508fb032f5ae709b1f2897b..f427ee49d309d8fc33ebf3042c3a775f2f530ded:/iokit/Kernel/IOCommandPool.cpp diff --git a/iokit/Kernel/IOCommandPool.cpp b/iokit/Kernel/IOCommandPool.cpp index c50a61636..0dcb24e0f 100644 --- a/iokit/Kernel/IOCommandPool.cpp +++ b/iokit/Kernel/IOCommandPool.cpp @@ -1,23 +1,29 @@ /* * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. * - * @APPLE_LICENSE_HEADER_START@ - * - * The contents of this file constitute Original Code as defined in and - * are subject to the Apple Public Source License Version 1.1 (the - * "License"). You may not use this file except in compliance with the - * License. Please obtain a copy of the License at - * http://www.apple.com/publicsource and read it before using this file. - * - * This Original Code and all software distributed under the License are - * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the - * License for the specific language governing rights and limitations - * under the License. - * - * @APPLE_LICENSE_HEADER_END@ + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* @@ -31,7 +37,10 @@ * */ +#define IOKIT_ENABLE_SHARED_PTR + #include +#include #define super OSObject OSDefineMetaClassAndStructors(IOCommandPool, OSObject); @@ -48,62 +57,67 @@ OSMetaClassDefineReservedUnused(IOCommandPool, 7); // withWorkLoop - primary initializer and factory method //-------------------------------------------------------------------------- -IOCommandPool *IOCommandPool:: +OSSharedPtr +IOCommandPool:: withWorkLoop(IOWorkLoop *inWorkLoop) { - IOCommandPool * me = new IOCommandPool; - - if (me && !me->initWithWorkLoop(inWorkLoop)) { - me->release(); - return 0; - } - - return me; + OSSharedPtr me = OSMakeShared(); + + if (me && !me->initWithWorkLoop(inWorkLoop)) { + return nullptr; + } + + return me; } -bool IOCommandPool:: +bool +IOCommandPool:: initWithWorkLoop(IOWorkLoop *inWorkLoop) { - assert(inWorkLoop); - - if (!super::init()) - return false; - - queue_init(&fQueueHead); - - fSerializer = IOCommandGate::commandGate(this); - assert(fSerializer); - if (!fSerializer) - return false; - - if (kIOReturnSuccess != inWorkLoop->addEventSource(fSerializer)) - return false; - - return true; + assert(inWorkLoop); + + if (!super::init()) { + return false; + } + + queue_init(&fQueueHead); + + fSerializer = IOCommandGate::commandGate(this); + assert(fSerializer); + if (!fSerializer) { + return false; + } + + if (kIOReturnSuccess != inWorkLoop->addEventSource(fSerializer.get())) { + return false; + } + + return true; } //-------------------------------------------------------------------------- // commandPool & init - obsolete initializer and factory method //-------------------------------------------------------------------------- -IOCommandPool *IOCommandPool:: +OSSharedPtr +IOCommandPool:: commandPool(IOService * inOwner, IOWorkLoop *inWorkLoop, UInt32 inSize) { - IOCommandPool * me = new IOCommandPool; - - if (me && !me->init(inOwner, inWorkLoop, inSize)) { - me->release(); - return 0; - } - - return me; + OSSharedPtr me = OSMakeShared(); + + if (me && !me->init(inOwner, inWorkLoop, inSize)) { + return nullptr; + } + + return me; } -bool IOCommandPool:: +bool +IOCommandPool:: init(IOService */* inOwner */, IOWorkLoop *inWorkLoop, UInt32 /* inSize */) { - return initWithWorkLoop(inWorkLoop); + return initWithWorkLoop(inWorkLoop); } @@ -114,18 +128,18 @@ init(IOService */* inOwner */, IOWorkLoop *inWorkLoop, UInt32 /* inSize */) void IOCommandPool::free(void) { - if (fSerializer) { - // remove our event source from owner's workloop - IOWorkLoop *wl = fSerializer->getWorkLoop(); - if (wl) - wl->removeEventSource(fSerializer); - - fSerializer->release(); - fSerializer = 0; - } - - // Tell our superclass to cleanup too - super::free(); + if (fSerializer) { + // remove our event source from owner's workloop + IOWorkLoop *wl = fSerializer->getWorkLoop(); + if (wl) { + wl->removeEventSource(fSerializer.get()); + } + + fSerializer.reset(); + } + + // Tell our superclass to cleanup too + super::free(); } @@ -135,19 +149,21 @@ IOCommandPool::free(void) // waiting for resources //-------------------------------------------------------------------------- -IOCommand * +OSSharedPtr IOCommandPool::getCommand(bool blockForCommand) { - IOReturn result = kIOReturnSuccess; - IOCommand *command = 0; - - result = fSerializer->runAction((IOCommandGate::Action) - &IOCommandPool::gatedGetCommand, - (void *) &command, (void *) blockForCommand); - if (kIOReturnSuccess == result) - return command; - else - return 0; + IOReturn result = kIOReturnSuccess; + IOCommand *command = NULL; + + IOCommandGate::Action func = OSMemberFunctionCast( + IOCommandGate::Action, this, &IOCommandPool::gatedGetCommand); + result = fSerializer-> + runAction(func, (void *) &command, (void *) blockForCommand); + if (kIOReturnSuccess == result) { + return OSSharedPtr(command, OSNoRetain); + } else { + return NULL; + } } @@ -156,20 +172,22 @@ IOCommandPool::getCommand(bool blockForCommand) // (on safe side of command gate) //-------------------------------------------------------------------------- -IOReturn IOCommandPool:: +IOReturn +IOCommandPool:: gatedGetCommand(IOCommand **command, bool blockForCommand) { - while (queue_empty(&fQueueHead)) { - if (!blockForCommand) - return kIOReturnNoResources; - - fSleepers++; - fSerializer->commandSleep(&fSleepers, THREAD_UNINT); - } - - queue_remove_first(&fQueueHead, - *command, IOCommand *, fCommandChain); - return kIOReturnSuccess; + while (queue_empty(&fQueueHead)) { + if (!blockForCommand) { + return kIOReturnNoResources; + } + + fSleepers++; + fSerializer->commandSleep(&fSleepers, THREAD_UNINT); + } + + queue_remove_first(&fQueueHead, + *command, IOCommand *, fCommandChain); + return kIOReturnSuccess; } @@ -177,26 +195,29 @@ gatedGetCommand(IOCommand **command, bool blockForCommand) // returnCommand - Returns command to the pool. //-------------------------------------------------------------------------- -void IOCommandPool:: +void +IOCommandPool:: returnCommand(IOCommand *command) { - (void) fSerializer->runAction((IOCommandGate::Action) - &IOCommandPool::gatedReturnCommand, (void *) command); + IOCommandGate::Action func = OSMemberFunctionCast( + IOCommandGate::Action, this, &IOCommandPool::gatedReturnCommand); + (void) fSerializer->runAction(func, (void *) command); } //-------------------------------------------------------------------------- // gatedReturnCommand - Callthrough function -// (on safe side of command gate) +// (on safe side of command gate) //-------------------------------------------------------------------------- -IOReturn IOCommandPool:: +IOReturn +IOCommandPool:: gatedReturnCommand(IOCommand *command) { - queue_enter(&fQueueHead, command, IOCommand *, fCommandChain); - if (fSleepers) { - fSerializer->commandWakeup(&fSleepers, /* oneThread */ true); - fSleepers--; - } - return kIOReturnSuccess; + queue_enter_first(&fQueueHead, command, IOCommand *, fCommandChain); + if (fSleepers) { + fSerializer->commandWakeup(&fSleepers, /* oneThread */ true); + fSleepers--; + } + return kIOReturnSuccess; }