]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* IOSyncer.cpp created by wgulland on 2000-02-02 */ | |
29 | ||
30 | #include <IOKit/IOLib.h> | |
31 | #include <IOKit/IOSyncer.h> | |
32 | ||
33 | OSDefineMetaClassAndStructors(IOSyncer, OSObject) | |
34 | ||
35 | IOSyncer * IOSyncer::create(bool twoRetains) | |
36 | { | |
0a7de745 | 37 | IOSyncer * me = new IOSyncer; |
1c79356b | 38 | |
0a7de745 A |
39 | if (me && !me->init(twoRetains)) { |
40 | me->release(); | |
cb323159 | 41 | return NULL; |
0a7de745 | 42 | } |
1c79356b | 43 | |
0a7de745 | 44 | return me; |
1c79356b A |
45 | } |
46 | ||
0a7de745 A |
47 | bool |
48 | IOSyncer::init(bool twoRetains) | |
1c79356b | 49 | { |
0a7de745 A |
50 | if (!OSObject::init()) { |
51 | return false; | |
52 | } | |
53 | ||
54 | if (!(guardLock = IOSimpleLockAlloc())) { | |
55 | return false; | |
56 | } | |
1c79356b | 57 | |
0a7de745 | 58 | IOSimpleLockInit(guardLock); |
1c79356b | 59 | |
0a7de745 A |
60 | if (twoRetains) { |
61 | retain(); | |
62 | } | |
1c79356b | 63 | |
0a7de745 | 64 | fResult = kIOReturnSuccess; |
1c79356b | 65 | |
0a7de745 | 66 | reinit(); |
1c79356b | 67 | |
0a7de745 | 68 | return true; |
1c79356b A |
69 | } |
70 | ||
0a7de745 A |
71 | void |
72 | IOSyncer::reinit() | |
1c79356b | 73 | { |
0a7de745 A |
74 | IOInterruptState is = IOSimpleLockLockDisableInterrupt(guardLock); |
75 | threadMustStop = true; | |
76 | IOSimpleLockUnlockEnableInterrupt(guardLock, is); | |
1c79356b A |
77 | } |
78 | ||
0a7de745 A |
79 | void |
80 | IOSyncer::free() | |
1c79356b | 81 | { |
0a7de745 A |
82 | // just in case a thread is blocked here: |
83 | privateSignal(); | |
1c79356b | 84 | |
0a7de745 A |
85 | if (guardLock != NULL) { |
86 | IOSimpleLockFree(guardLock); | |
87 | } | |
1c79356b | 88 | |
0a7de745 | 89 | OSObject::free(); |
1c79356b A |
90 | } |
91 | ||
0a7de745 A |
92 | IOReturn |
93 | IOSyncer::wait(bool autoRelease) | |
1c79356b | 94 | { |
0a7de745 | 95 | IOInterruptState is = IOSimpleLockLockDisableInterrupt(guardLock); |
1c79356b | 96 | |
0a7de745 A |
97 | if (threadMustStop) { |
98 | assert_wait((void *) &threadMustStop, false); | |
99 | IOSimpleLockUnlockEnableInterrupt(guardLock, is); | |
100 | thread_block(THREAD_CONTINUE_NULL); | |
101 | } else { | |
102 | IOSimpleLockUnlockEnableInterrupt(guardLock, is); | |
103 | } | |
1c79356b | 104 | |
0a7de745 | 105 | IOReturn result = fResult; // Pick up before auto deleting! |
1c79356b | 106 | |
0a7de745 A |
107 | if (autoRelease) { |
108 | release(); | |
109 | } | |
1c79356b | 110 | |
0a7de745 | 111 | return result; |
1c79356b A |
112 | } |
113 | ||
0a7de745 A |
114 | void |
115 | IOSyncer::signal(IOReturn res, bool autoRelease) | |
1c79356b | 116 | { |
0a7de745 A |
117 | fResult = res; |
118 | privateSignal(); | |
119 | if (autoRelease) { | |
120 | release(); | |
121 | } | |
1c79356b A |
122 | } |
123 | ||
0a7de745 A |
124 | void |
125 | IOSyncer::privateSignal() | |
1c79356b | 126 | { |
0a7de745 A |
127 | if (threadMustStop) { |
128 | IOInterruptState is = IOSimpleLockLockDisableInterrupt(guardLock); | |
129 | threadMustStop = false; | |
130 | thread_wakeup_one((void *) &threadMustStop); | |
131 | IOSimpleLockUnlockEnableInterrupt(guardLock, is); | |
132 | } | |
1c79356b | 133 | } |