]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOSyncer.cpp
9099daa5711d22ee6c5ae589644b9befefebfdb3
[apple/xnu.git] / iokit / Kernel / IOSyncer.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* IOSyncer.cpp created by wgulland on 2000-02-02 */
24
25 #include <IOKit/IOLib.h>
26 #include <IOKit/IOSyncer.h>
27
28 OSDefineMetaClassAndStructors(IOSyncer, OSObject)
29
30 IOSyncer * IOSyncer::create(bool twoRetains)
31 {
32 IOSyncer * me = new IOSyncer;
33
34 if (me && !me->init(twoRetains)) {
35 me->release();
36 return 0;
37 }
38
39 return me;
40 }
41
42 bool IOSyncer::init(bool twoRetains)
43 {
44 if (!OSObject::init())
45 return false;
46
47 if (!(guardLock = IOSimpleLockAlloc()) )
48 return false;
49
50 IOSimpleLockInit(guardLock);
51
52 if(twoRetains)
53 retain();
54
55 fResult = kIOReturnSuccess;
56
57 reinit();
58
59 return true;
60 }
61
62 void IOSyncer::reinit()
63 {
64 IOInterruptState is = IOSimpleLockLockDisableInterrupt(guardLock);
65 threadMustStop = true;
66 IOSimpleLockUnlockEnableInterrupt(guardLock, is);
67 }
68
69 void IOSyncer::free()
70 {
71 // just in case a thread is blocked here:
72 privateSignal();
73
74 if (guardLock != NULL)
75 IOSimpleLockFree(guardLock);
76
77 OSObject::free();
78 }
79
80 IOReturn IOSyncer::wait(bool autoRelease)
81 {
82 IOInterruptState is = IOSimpleLockLockDisableInterrupt(guardLock);
83
84 if (threadMustStop) {
85 assert_wait((void *) &threadMustStop, false);
86 IOSimpleLockUnlockEnableInterrupt(guardLock, is);
87 thread_block(THREAD_CONTINUE_NULL);
88 }
89 else
90 IOSimpleLockUnlockEnableInterrupt(guardLock, is);
91
92 IOReturn result = fResult; // Pick up before auto deleting!
93
94 if(autoRelease)
95 release();
96
97 return result;
98 }
99
100 void IOSyncer::signal(IOReturn res, bool autoRelease)
101 {
102 fResult = res;
103 privateSignal();
104 if(autoRelease)
105 release();
106 }
107
108 void IOSyncer::privateSignal()
109 {
110 if (threadMustStop) {
111 IOInterruptState is = IOSimpleLockLockDisableInterrupt(guardLock);
112 threadMustStop = false;
113 thread_wakeup_one((void *) &threadMustStop);
114 IOSimpleLockUnlockEnableInterrupt(guardLock, is);
115 }
116 }