]>
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 #ifndef __ppc__ /* ppc does this right and doesn't need these routines */
37 void * OSDequeueAtomic(void * volatile * inList
, SInt32 inOffset
)
39 /* The _pointer_ is volatile, not the listhead itself */
40 void * volatile oldListHead
;
41 void * volatile newListHead
;
44 oldListHead
= *inList
;
45 if (oldListHead
== NULL
) {
49 newListHead
= *(void * volatile *) (((char *) oldListHead
) + inOffset
);
50 } while (! OSCompareAndSwap((UInt32
)oldListHead
,
51 (UInt32
)newListHead
, (volatile UInt32
*)inList
));
56 void OSEnqueueAtomic(void * volatile * inList
, void * inNewLink
, SInt32 inOffset
)
58 /* The _pointer_ is volatile, not the listhead itself */
59 void * volatile oldListHead
;
60 void * volatile newListHead
= inNewLink
;
61 void * volatile * newLinkNextPtr
= (void * volatile *) (((char *) inNewLink
) + inOffset
);
64 oldListHead
= *inList
;
65 *newLinkNextPtr
= oldListHead
;
66 } while (! OSCompareAndSwap((UInt32
)oldListHead
, (UInt32
)newListHead
,
67 (volatile UInt32
*)inList
));
69 #endif /* ! __ppc__ */
72 IOPMPowerStateQueue
*IOPMPowerStateQueue::PMPowerStateQueue(OSObject
*inOwner
)
74 IOPMPowerStateQueue
*me
= new IOPMPowerStateQueue
;
76 if(me
&& !me
->init(inOwner
, 0) )
85 bool IOPMPowerStateQueue::init(OSObject
*owner
, Action action
)
87 if(!(super::init(owner
, (IOEventSource::Action
) action
))) return false;
89 // Queue of powerstate changes
92 if (!(tmpLock
= IOLockAlloc())) panic("IOPMPowerStateQueue::init can't alloc lock");
98 bool IOPMPowerStateQueue::unIdleOccurred(IOService
*inTarget
, unsigned long inState
)
100 PowerChangeEntry
*new_one
= NULL
;
102 new_one
= (PowerChangeEntry
*)IOMalloc(sizeof(PowerChangeEntry
));
103 if(!new_one
) return false;
105 new_one
->actionType
= IOPMPowerStateQueue::kUnIdle
;
106 new_one
->state
= inState
;
107 new_one
->target
= inTarget
;
113 OSEnqueueAtomic((void **)&changes
, (void *)new_one
, 0);
115 IOLockUnlock(tmpLock
);
117 signalWorkAvailable();
122 bool IOPMPowerStateQueue::featureChangeOccurred(
126 PowerChangeEntry
*new_one
= NULL
;
128 new_one
= (PowerChangeEntry
*)IOMalloc(sizeof(PowerChangeEntry
));
129 if(!new_one
) return false;
131 new_one
->actionType
= IOPMPowerStateQueue::kPMFeatureChange
;
132 new_one
->state
= inState
;
133 new_one
->target
= inTarget
;
139 OSEnqueueAtomic((void **)&changes
, (void *)new_one
, 0);
141 IOLockUnlock(tmpLock
);
143 signalWorkAvailable();
149 // checkForWork() is called in a gated context
150 bool IOPMPowerStateQueue::checkForWork()
152 PowerChangeEntry
*theNode
;
154 IOService
*theTarget
;
157 // Dequeue and process the state change request
161 if((theNode
= (PowerChangeEntry
*)OSDequeueAtomic((void **)&changes
, 0)))
164 IOLockUnlock(tmpLock
);
166 theState
= theNode
->state
;
167 theTarget
= theNode
->target
;
168 theAction
= theNode
->actionType
;
169 IOFree((void *)theNode
, sizeof(PowerChangeEntry
));
174 theTarget
->command_received((void *)theState
, 0, 0, 0);
177 case kPMFeatureChange
:
178 theTarget
->messageClients(theState
, theTarget
);
184 IOLockUnlock(tmpLock
);
187 // Return true if there's more work to be done
188 if(changes
) return true;