]> git.saurik.com Git - apple/security.git/blob - libsecurity_utilities/lib/globalizer.h
Security-55178.0.1.tar.gz
[apple/security.git] / libsecurity_utilities / lib / globalizer.h
1 /*
2 * Copyright (c) 2000-2004 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
24
25 /*
26 * globalizer - multiscope globalization services
27 */
28 #ifndef _H_GLOBALIZER
29 #define _H_GLOBALIZER
30
31 #include <security_utilities/threading.h>
32 #include <memory>
33 #include <set>
34
35 namespace Security {
36
37
38 //
39 // GlobalNexus is the common superclass of all globality scopes.
40 // A Nexus is an *access point* to the *single* object of a given
41 // type in the Nexus's particular scope.
42 //
43 class GlobalNexus {
44 public:
45 class Error : public std::exception {
46 public:
47 virtual ~Error() throw();
48 const char * const message;
49 Error(const char *m) : message(m) { }
50 const char *what() const throw() { return message; }
51 };
52 };
53
54
55 //
56 // A module-scope nexus is tied to the linker Nexus object itself.
57 // Its scope is all code accessing that particular Nexus object
58 // from within a process. Any number of ModuleNexus objects can
59 // exist, and each implements a different scope.
60 //
61 // IMPORTANT notes on this class can be found in globalizer.cpp.
62 // DO NOT change anything here before carefully reading them.
63 //
64 class ModuleNexusCommon : public GlobalNexus {
65 protected:
66 void *create(void *(*make)());
67
68 protected:
69 // both of these will be statically initialized to zero
70 void *pointer;
71 StaticAtomicCounter<uint32_t> sync;
72 };
73
74 template <class Type>
75 class ModuleNexus : public ModuleNexusCommon {
76 public:
77 Type &operator () ()
78 {
79 void *p = Atomic<void *>::load(pointer); // latch pointer
80 if (!p || (uintptr_t(p) & 0x1)) {
81 p = create(make);
82 secdebug("nexus", "module %s 0x%p", Debug::typeName<Type>().c_str(), pointer);
83 }
84 return *reinterpret_cast<Type *>(p);
85 }
86
87 // does the object DEFINITELY exist already?
88 bool exists() const
89 {
90 return Atomic<void *>::load(pointer) != NULL;
91 }
92
93 // destroy the object (if any) and start over - not really thread-safe
94 void reset()
95 {
96 if (pointer && !(uintptr_t(pointer) & 0x1)) {
97 delete reinterpret_cast<Type *>(pointer);
98 Atomic<void *>::store(0, pointer);
99 }
100 }
101
102 private:
103 static void *make() { return new Type; }
104 };
105
106 template <class Type>
107 class CleanModuleNexus : public ModuleNexus<Type> {
108 public:
109 ~CleanModuleNexus()
110 {
111 secdebug("nexus", "ModuleNexus %p destroyed object 0x%x",
112 this, ModuleNexus<Type>::pointer);
113 delete reinterpret_cast<Type *>(ModuleNexus<Type>::pointer);
114 }
115 };
116
117 typedef std::set<void*> RetentionSet;
118
119 class ThreadNexusBase {
120 protected:
121 static ModuleNexus<Mutex> mInstanceLock;
122 static ModuleNexus<RetentionSet> mInstances;
123 };
124
125
126
127 //
128 // A thread-scope nexus is tied to a particular native thread AND
129 // a particular nexus object. Its scope is all code in any one thread
130 // that access that particular Nexus object. Any number of Nexus objects
131 // can exist, and each implements a different scope for each thread.
132 // NOTE: ThreadNexus is dynamically constructed. If you want static,
133 // zero-initialization ThreadNexi, put them inside a ModuleNexus.
134 //
135 template <class Type>
136 class ThreadNexus : public GlobalNexus, private ThreadNexusBase {
137 public:
138 ThreadNexus() : mSlot(true) { }
139
140 Type &operator () ()
141 {
142 // no thread contention here!
143 if (Type *p = mSlot)
144 return *p;
145 mSlot = new Type;
146 {
147 StLock<Mutex> _(mInstanceLock ());
148 mInstances ().insert(mSlot);
149 }
150 return *mSlot;
151 }
152
153 private:
154 PerThreadPointer<Type> mSlot;
155 };
156
157
158 //
159 // A ProcessNexus is global within a single process, regardless of
160 // load module boundaries. You can have any number of ProcessNexus
161 // scopes, each identified by a C string (compared by value, not pointer).
162 //
163 class ProcessNexusBase : public GlobalNexus {
164 protected:
165 ProcessNexusBase(const char *identifier);
166
167 struct Store {
168 void *mObject;
169 Mutex mLock;
170 };
171 Store *mStore;
172 };
173
174 template <class Type>
175 class ProcessNexus : public ProcessNexusBase {
176 public:
177 ProcessNexus(const char *identifier) : ProcessNexusBase(identifier) { }
178 Type &operator () ();
179
180 private:
181 Type *mObject;
182 };
183
184 template <class Type>
185 Type &ProcessNexus<Type>::operator () ()
186 {
187 #if !defined(PTHREAD_STRICT)
188 // not strictly kosher POSIX, but pointers are usually atomic types
189 if (mStore->mObject)
190 return *reinterpret_cast<Type *>(mStore->mObject);
191 #endif
192 StLock<Mutex> _(mStore->mLock);
193 if (mStore->mObject == NULL)
194 mStore->mObject = new Type;
195 return *reinterpret_cast<Type *>(mStore->mObject);
196 };
197
198
199 } // end namespace Security
200
201 #endif //_H_GLOBALIZER