]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/handleobject.cpp
Security-28.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / handleobject.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // handleobject - give an object a process-global unique handle
21 //
22 #ifdef __MWERKS__
23 #define _CPP_HANDLEOBJECT
24 #endif
25 #include <Security/handleobject.h>
26
27
28 //
29 // Static members of HandleObject
30 //
31 ModuleNexus<HandleObject::State> HandleObject::state;
32
33
34 //
35 // Bring the State constructor out of line
36 //
37 HandleObject::State::State()
38 { }
39
40
41 //
42 // Assign a HandleObject's (new) Handle.
43 //
44 void HandleObject::State::make(HandleObject *obj)
45 {
46 StLock<Mutex> _(mLock);
47 for (;;) {
48 Handle handle = reinterpret_cast<uint32>(obj) ^ (++sequence << 19);
49 if (handleMap[handle] == NULL) {
50 debug("handleobj", "create 0x%lx for %p", handle, obj);
51 obj->setHandle(handle);
52 handleMap[handle] = obj;
53 return;
54 }
55 }
56 }
57
58
59 //
60 // Clean up a HandleObject that dies.
61 // Note that an object MAY clear its handle before (in which case we do nothing).
62 // In particular, killHandle will do this.
63 //
64 void HandleObject::State::erase(HandleObject *obj)
65 {
66 StLock<Mutex> _(mLock);
67 if (obj->validHandle())
68 handleMap.erase(obj->handle());
69 }
70
71
72 //
73 // This is the main locator driver. It translates an object handle
74 // into an object pointer, on the way atomically locking it and/or
75 // removing it from the handle map for atomic deletion.
76 //
77 HandleObject *HandleObject::State::locate(CSSM_HANDLE h, LocateMode mode, CSSM_RETURN error)
78 {
79 for (;;) {
80 {
81 StLock<Mutex> _(mLock);
82 HandleMap::iterator it = handleMap.find(h);
83 if (it == handleMap.end())
84 CssmError::throwMe(error);
85 HandleObject *obj = it->second;
86 if (obj == NULL || obj->handle() != h)
87 CssmError::throwMe(error);
88 if (mode == findTarget)
89 return obj; // that's all, folks
90 // atomic find-and-lock requested (implicit in remove operation)
91 if (obj->tryLock()) {
92 // got object lock - assured of exit path
93 if (mode == removeTarget) {
94 debug("handleobj", "killing %p", obj);
95 handleMap.erase(h);
96 obj->clearHandle();
97 }
98 return obj;
99 }
100 // obj is busy; relinquish maplock and try again later
101 debug("handleobj", "object %p (handle 0x%lx) is busy - backing off",
102 obj, h);
103 }
104 #if _USE_THREADS == _USE_NO_THREADS
105 assert(false); // impossible; tryLock above always succeeds
106 #else // real threads
107 Thread::yield();
108 #endif // real threads
109 }
110 }
111
112
113 //
114 // The default locking virtual methods do nothing and succeed.
115 //
116 void HandleObject::lock() { }
117
118 bool HandleObject::tryLock() { return true; }