]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/globalizer.h
2 * Copyright (c) 2000-2004,2011-2012,2014 Apple 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>
34 #include <dispatch/dispatch.h>
35 #include <libkern/OSAtomic.h>
41 // GlobalNexus is the common superclass of all globality scopes.
42 // A Nexus is an *access point* to the *single* object of a given
43 // type in the Nexus's particular scope.
47 class Error
: public std::exception
{
49 virtual ~Error() throw();
50 const char * const message
;
51 Error(const char *m
) : message(m
) { }
52 const char *what() const throw() { return message
; }
57 class ModuleNexusCommon
: public GlobalNexus
{
59 void do_create(void *(*make
)());
62 void *create(void *(*make
)());
63 void lock() {OSSpinLockLock(&access
);}
64 void unlock() {OSSpinLockUnlock(&access
);}
67 // all of these will be statically initialized to zero
74 class ModuleNexus
: public ModuleNexusCommon
{
84 pointer
= create(make
);
95 return *reinterpret_cast<Type
*>(pointer
);
98 // does the object DEFINITELY exist already?
103 result
= pointer
!= NULL
;
108 // destroy the object (if any) and start over - not really thread-safe
114 delete reinterpret_cast<Type
*>(pointer
);
122 static void *make() { return new Type
; }
125 template <class Type
>
126 class CleanModuleNexus
: public ModuleNexus
<Type
> {
130 secdebug("nexus", "ModuleNexus %p destroyed object 0x%x",
131 this, ModuleNexus
<Type
>::pointer
);
132 delete reinterpret_cast<Type
*>(ModuleNexus
<Type
>::pointer
);
136 typedef std::set
<void*> RetentionSet
;
139 // A thread-scope nexus is tied to a particular native thread AND
140 // a particular nexus object. Its scope is all code in any one thread
141 // that access that particular Nexus object. Any number of Nexus objects
142 // can exist, and each implements a different scope for each thread.
143 // NOTE: ThreadNexus is dynamically constructed. If you want static,
144 // zero-initialization ThreadNexi, put them inside a ModuleNexus.
146 template <class Type
>
147 class ThreadNexus
: public GlobalNexus
{
149 ThreadNexus() : mSlot(true) { }
153 // no thread contention here!
161 PerThreadPointer
<Type
> mSlot
;
166 // A ProcessNexus is global within a single process, regardless of
167 // load module boundaries. You can have any number of ProcessNexus
168 // scopes, each identified by a C string (compared by value, not pointer).
170 class ProcessNexusBase
: public GlobalNexus
{
172 ProcessNexusBase(const char *identifier
);
181 template <class Type
>
182 class ProcessNexus
: public ProcessNexusBase
{
184 ProcessNexus(const char *identifier
) : ProcessNexusBase(identifier
) { }
185 Type
&operator () ();
191 template <class Type
>
192 Type
&ProcessNexus
<Type
>::operator () ()
194 #if !defined(PTHREAD_STRICT)
195 // not strictly kosher POSIX, but pointers are usually atomic types
197 return *reinterpret_cast<Type
*>(mStore
->mObject
);
199 StLock
<Mutex
> _(mStore
->mLock
);
200 if (mStore
->mObject
== NULL
)
201 mStore
->mObject
= new Type
;
202 return *reinterpret_cast<Type
*>(mStore
->mObject
);
206 } // end namespace Security
208 #endif //_H_GLOBALIZER