]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOPMPowerStateQueue.cpp
2 * Copyright (c) 2001-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include "IOPMPowerStateQueue.h"
30 #include "IOKit/IOLocks.h"
32 #define super IOEventSource
33 OSDefineMetaClassAndStructors(IOPMPowerStateQueue
, IOEventSource
);
35 #ifdef __i386__ /* ppc does this right and doesn't need these routines */
37 void * OSDequeueAtomic(void ** inList
, SInt32 inOffset
)
43 oldListHead
= *inList
;
44 if (oldListHead
== NULL
) {
48 newListHead
= *(void **) (((char *) oldListHead
) + inOffset
);
49 } while (! OSCompareAndSwap((UInt32
)oldListHead
,
50 (UInt32
)newListHead
, (UInt32
*)inList
));
55 void OSEnqueueAtomic(void ** inList
, void * inNewLink
, SInt32 inOffset
)
58 void * newListHead
= inNewLink
;
59 void ** newLinkNextPtr
= (void **) (((char *) inNewLink
) + inOffset
);
62 oldListHead
= *inList
;
63 *newLinkNextPtr
= oldListHead
;
64 } while (! OSCompareAndSwap((UInt32
)oldListHead
, (UInt32
)newListHead
,
70 IOPMPowerStateQueue
*IOPMPowerStateQueue::PMPowerStateQueue(OSObject
*inOwner
)
72 IOPMPowerStateQueue
*me
= new IOPMPowerStateQueue
;
74 if(me
&& !me
->init(inOwner
, 0) )
83 bool IOPMPowerStateQueue::init(OSObject
*owner
, Action action
)
85 if(!(super::init(owner
, (IOEventSource::Action
) action
))) return false;
87 // Queue of powerstate changes
90 if (!(tmpLock
= IOLockAlloc())) panic("IOPMPowerStateQueue::init can't alloc lock");
96 bool IOPMPowerStateQueue::unIdleOccurred(IOService
*inTarget
, unsigned long inState
)
98 PowerChangeEntry
*new_one
= NULL
;
100 new_one
= (PowerChangeEntry
*)IOMalloc(sizeof(PowerChangeEntry
));
101 if(!new_one
) return false;
103 new_one
->actionType
= IOPMPowerStateQueue::kUnIdle
;
104 new_one
->state
= inState
;
105 new_one
->target
= inTarget
;
111 OSEnqueueAtomic((void **)&changes
, (void *)new_one
, 0);
113 IOLockUnlock(tmpLock
);
115 signalWorkAvailable();
120 // checkForWork() is called in a gated context
121 bool IOPMPowerStateQueue::checkForWork()
123 PowerChangeEntry
*theNode
;
125 IOService
*theTarget
;
128 // Dequeue and process the state change request
132 if((theNode
= (PowerChangeEntry
*)OSDequeueAtomic((void **)&changes
, 0)))
135 IOLockUnlock(tmpLock
);
137 theState
= theNode
->state
;
138 theTarget
= theNode
->target
;
139 theAction
= theNode
->actionType
;
140 IOFree((void *)theNode
, sizeof(PowerChangeEntry
));
145 theTarget
->command_received((void *)theState
, 0, 0, 0);
151 IOLockUnlock(tmpLock
);
154 // Return true if there's more work to be done
155 if(changes
) return true;