]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOSyncer.cpp
xnu-123.5.tar.gz
[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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* IOSyncer.cpp created by wgulland on 2000-02-02 */
23
24 #include <IOKit/IOLib.h>
25 #include <IOKit/IOSyncer.h>
26
27 OSDefineMetaClassAndStructors(IOSyncer, OSObject)
28
29 IOSyncer * IOSyncer::create(bool twoRetains)
30 {
31 IOSyncer * me = new IOSyncer;
32
33 if (me && !me->init(twoRetains)) {
34 me->release();
35 return 0;
36 }
37
38 return me;
39 }
40
41 bool IOSyncer::init(bool twoRetains)
42 {
43 if (!OSObject::init())
44 return false;
45
46 if (!(guardLock = IOSimpleLockAlloc()) )
47 return false;
48
49 IOSimpleLockInit(guardLock);
50
51 if(twoRetains)
52 retain();
53
54 fResult = kIOReturnSuccess;
55
56 reinit();
57
58 return true;
59 }
60
61 void IOSyncer::reinit()
62 {
63 IOInterruptState is = IOSimpleLockLockDisableInterrupt(guardLock);
64 threadMustStop = true;
65 IOSimpleLockUnlockEnableInterrupt(guardLock, is);
66 }
67
68 void IOSyncer::free()
69 {
70 // just in case a thread is blocked here:
71 privateSignal();
72
73 if (guardLock != NULL)
74 IOSimpleLockFree(guardLock);
75
76 OSObject::free();
77 }
78
79 IOReturn IOSyncer::wait(bool autoRelease = true)
80 {
81 IOInterruptState is = IOSimpleLockLockDisableInterrupt(guardLock);
82
83 if (threadMustStop) {
84 assert_wait((void *) &threadMustStop, false);
85 IOSimpleLockUnlockEnableInterrupt(guardLock, is);
86 thread_block(0);
87 }
88 else
89 IOSimpleLockUnlockEnableInterrupt(guardLock, is);
90
91 IOReturn result = fResult; // Pick up before auto deleting!
92
93 if(autoRelease)
94 release();
95
96 return result;
97 }
98
99 void IOSyncer::signal(IOReturn res = kIOReturnSuccess,
100 bool autoRelease = true)
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 }