1368ad365SApple OSS Distributions /*
2368ad365SApple OSS Distributions  * Copyright (c) 2001-2002 Apple Computer, Inc. All rights reserved.
3368ad365SApple OSS Distributions  *
4e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5368ad365SApple OSS Distributions  *
6e13b1fa5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7e13b1fa5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8e13b1fa5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9e13b1fa5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10e13b1fa5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11e13b1fa5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12e13b1fa5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13e13b1fa5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14368ad365SApple OSS Distributions  *
15e13b1fa5SApple OSS Distributions  * Please obtain a copy of the License at
16e13b1fa5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17e13b1fa5SApple OSS Distributions  *
18e13b1fa5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19e13b1fa5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20368ad365SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21368ad365SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22e13b1fa5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23e13b1fa5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24e13b1fa5SApple OSS Distributions  * limitations under the License.
25368ad365SApple OSS Distributions  *
26e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27368ad365SApple OSS Distributions  */
28368ad365SApple OSS Distributions 
29368ad365SApple OSS Distributions #include "IOPMPowerStateQueue.h"
303ca3bd55SApple OSS Distributions 
31368ad365SApple OSS Distributions #define super IOEventSource
OSDefineMetaClassAndStructors(IOPMPowerStateQueue,IOEventSource)323ca3bd55SApple OSS Distributions OSDefineMetaClassAndStructors( IOPMPowerStateQueue, IOEventSource )
33368ad365SApple OSS Distributions 
343ca3bd55SApple OSS Distributions IOPMPowerStateQueue * IOPMPowerStateQueue::PMPowerStateQueue(
353ca3bd55SApple OSS Distributions 	OSObject * inOwner, Action inAction )
36368ad365SApple OSS Distributions {
37368ad365SApple OSS Distributions 	IOPMPowerStateQueue * me = new IOPMPowerStateQueue;
38368ad365SApple OSS Distributions 
39a5e72196SApple OSS Distributions 	if (me && !me->init(inOwner, inAction)) {
40368ad365SApple OSS Distributions 		me->release();
41368ad365SApple OSS Distributions 		return NULL;
42368ad365SApple OSS Distributions 	}
43368ad365SApple OSS Distributions 
44368ad365SApple OSS Distributions 	return me;
45368ad365SApple OSS Distributions }
46368ad365SApple OSS Distributions 
47a5e72196SApple OSS Distributions bool
init(OSObject * inOwner,Action inAction)48a5e72196SApple OSS Distributions IOPMPowerStateQueue::init( OSObject * inOwner, Action inAction )
49368ad365SApple OSS Distributions {
50a5e72196SApple OSS Distributions 	if (!inAction || !(super::init(inOwner, inAction))) {
513ca3bd55SApple OSS Distributions 		return false;
52a5e72196SApple OSS Distributions 	}
53368ad365SApple OSS Distributions 
543ca3bd55SApple OSS Distributions 	queue_init( &queueHead );
553ca3bd55SApple OSS Distributions 
563ca3bd55SApple OSS Distributions 	queueLock = IOLockAlloc();
57a5e72196SApple OSS Distributions 	if (!queueLock) {
583ca3bd55SApple OSS Distributions 		return false;
59a5e72196SApple OSS Distributions 	}
603ca3bd55SApple OSS Distributions 
61368ad365SApple OSS Distributions 	return true;
62368ad365SApple OSS Distributions }
63368ad365SApple OSS Distributions 
64a5e72196SApple OSS Distributions bool
submitPowerEvent(uint32_t eventType,void * arg0,uint64_t arg1)65a5e72196SApple OSS Distributions IOPMPowerStateQueue::submitPowerEvent(
663ca3bd55SApple OSS Distributions 	uint32_t eventType,
673ca3bd55SApple OSS Distributions 	void *   arg0,
68855239e5SApple OSS Distributions 	uint64_t arg1 )
69368ad365SApple OSS Distributions {
703ca3bd55SApple OSS Distributions 	PowerEventEntry * entry;
71368ad365SApple OSS Distributions 
72*e6231be0SApple OSS Distributions 	entry = IOMallocType(PowerEventEntry);
73368ad365SApple OSS Distributions 
743ca3bd55SApple OSS Distributions 	entry->eventType = eventType;
75855239e5SApple OSS Distributions 	entry->arg0 = arg0;
76855239e5SApple OSS Distributions 	entry->arg1 = arg1;
77368ad365SApple OSS Distributions 
783ca3bd55SApple OSS Distributions 	IOLockLock(queueLock);
793ca3bd55SApple OSS Distributions 	queue_enter(&queueHead, entry, PowerEventEntry *, chain);
803ca3bd55SApple OSS Distributions 	IOLockUnlock(queueLock);
81368ad365SApple OSS Distributions 	signalWorkAvailable();
82368ad365SApple OSS Distributions 
83368ad365SApple OSS Distributions 	return true;
84368ad365SApple OSS Distributions }
85368ad365SApple OSS Distributions 
86a5e72196SApple OSS Distributions bool
checkForWork(void)87a5e72196SApple OSS Distributions IOPMPowerStateQueue::checkForWork( void )
88e13b1fa5SApple OSS Distributions {
893ca3bd55SApple OSS Distributions 	IOPMPowerStateQueueAction queueAction = (IOPMPowerStateQueueAction) action;
903ca3bd55SApple OSS Distributions 	PowerEventEntry * entry;
91e13b1fa5SApple OSS Distributions 
923ca3bd55SApple OSS Distributions 	IOLockLock(queueLock);
93a5e72196SApple OSS Distributions 	while (!queue_empty(&queueHead)) {
943ca3bd55SApple OSS Distributions 		queue_remove_first(&queueHead, entry, PowerEventEntry *, chain);
953ca3bd55SApple OSS Distributions 		IOLockUnlock(queueLock);
96368ad365SApple OSS Distributions 
97855239e5SApple OSS Distributions 		(*queueAction)(owner, entry->eventType, entry->arg0, entry->arg1);
98*e6231be0SApple OSS Distributions 		IOFreeType(entry, PowerEventEntry);
99368ad365SApple OSS Distributions 
1003ca3bd55SApple OSS Distributions 		IOLockLock(queueLock);
1013ca3bd55SApple OSS Distributions 	}
1023ca3bd55SApple OSS Distributions 	IOLockUnlock(queueLock);
103e13b1fa5SApple OSS Distributions 
1043ca3bd55SApple OSS Distributions 	return false;
105368ad365SApple OSS Distributions }
106