]>
git.saurik.com Git - apple/security.git/blob - libsecurity_utilities/lib/globalizer.h
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 * globalizer - multiscope globalization services
31 #include <security_utilities/threading.h>
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.
45 class Error
: public std::exception
{
47 virtual ~Error() throw();
48 const char * const message
;
49 Error(const char *m
) : message(m
) { }
50 const char *what() const throw() { return message
; }
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.
61 // IMPORTANT notes on this class can be found in globalizer.cpp.
62 // DO NOT change anything here before carefully reading them.
64 class ModuleNexusCommon
: public GlobalNexus
{
66 void *create(void *(*make
)());
69 // both of these will be statically initialized to zero
71 StaticAtomicCounter
<uint32_t> sync
;
75 class ModuleNexus
: public ModuleNexusCommon
{
79 void *p
= Atomic
<void *>::load(pointer
); // latch pointer
80 if (!p
|| (uintptr_t(p
) & 0x1)) {
82 secdebug("nexus", "module %s 0x%p", Debug::typeName
<Type
>().c_str(), pointer
);
84 return *reinterpret_cast<Type
*>(p
);
87 // does the object DEFINITELY exist already?
90 return Atomic
<void *>::load(pointer
) != NULL
;
93 // destroy the object (if any) and start over - not really thread-safe
96 if (pointer
&& !(uintptr_t(pointer
) & 0x1)) {
97 delete reinterpret_cast<Type
*>(pointer
);
98 Atomic
<void *>::store(0, pointer
);
103 static void *make() { return new Type
; }
106 template <class Type
>
107 class CleanModuleNexus
: public ModuleNexus
<Type
> {
111 secdebug("nexus", "ModuleNexus %p destroyed object 0x%x",
112 this, ModuleNexus
<Type
>::pointer
);
113 delete reinterpret_cast<Type
*>(ModuleNexus
<Type
>::pointer
);
117 typedef std::set
<void*> RetentionSet
;
119 class ThreadNexusBase
{
121 static ModuleNexus
<Mutex
> mInstanceLock
;
122 static ModuleNexus
<RetentionSet
> mInstances
;
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.
135 template <class Type
>
136 class ThreadNexus
: public GlobalNexus
, private ThreadNexusBase
{
138 ThreadNexus() : mSlot(true) { }
142 // no thread contention here!
147 StLock
<Mutex
> _(mInstanceLock ());
148 mInstances ().insert(mSlot
);
154 PerThreadPointer
<Type
> mSlot
;
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).
163 class ProcessNexusBase
: public GlobalNexus
{
165 ProcessNexusBase(const char *identifier
);
174 template <class Type
>
175 class ProcessNexus
: public ProcessNexusBase
{
177 ProcessNexus(const char *identifier
) : ProcessNexusBase(identifier
) { }
178 Type
&operator () ();
184 template <class Type
>
185 Type
&ProcessNexus
<Type
>::operator () ()
187 #if !defined(PTHREAD_STRICT)
188 // not strictly kosher POSIX, but pointers are usually atomic types
190 return *reinterpret_cast<Type
*>(mStore
->mObject
);
192 StLock
<Mutex
> _(mStore
->mLock
);
193 if (mStore
->mObject
== NULL
)
194 mStore
->mObject
= new Type
;
195 return *reinterpret_cast<Type
*>(mStore
->mObject
);
199 } // end namespace Security
201 #endif //_H_GLOBALIZER