]>
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>
42 // GlobalNexus is the common superclass of all globality scopes.
43 // A Nexus is an *access point* to the *single* object of a given
44 // type in the Nexus's particular scope.
48 class Error
: public std::exception
{
50 virtual ~Error() _NOEXCEPT
;
51 const char * const message
;
52 Error(const char *m
) : message(m
) { }
53 const char *what() const _NOEXCEPT
{ return message
; }
58 class ModuleNexusCommon
: public GlobalNexus
{
60 void do_create(void *(*make
)());
63 void *create(void *(*make
)());
64 void lock() {os_unfair_lock_lock(&access
);}
65 void unlock() {os_unfair_lock_unlock(&access
);}
68 // all of these will be statically initialized to zero
71 os_unfair_lock access
;
75 class ModuleNexus
: public ModuleNexusCommon
{
85 pointer
= create(make
);
96 return *reinterpret_cast<Type
*>(pointer
);
99 // does the object DEFINITELY exist already?
104 result
= pointer
!= NULL
;
109 // destroy the object (if any) and start over - not really thread-safe
115 delete reinterpret_cast<Type
*>(pointer
);
123 static void *make() { return new Type
; }
126 template <class Type
>
127 class CleanModuleNexus
: public ModuleNexus
<Type
> {
131 secinfo("nexus", "ModuleNexus %p destroyed object 0x%x",
132 this, ModuleNexus
<Type
>::pointer
);
133 delete reinterpret_cast<Type
*>(ModuleNexus
<Type
>::pointer
);
137 typedef std::set
<void*> RetentionSet
;
140 // A thread-scope nexus is tied to a particular native thread AND
141 // a particular nexus object. Its scope is all code in any one thread
142 // that access that particular Nexus object. Any number of Nexus objects
143 // can exist, and each implements a different scope for each thread.
144 // NOTE: ThreadNexus is dynamically constructed. If you want static,
145 // zero-initialization ThreadNexi, put them inside a ModuleNexus.
147 template <class Type
>
148 class ThreadNexus
: public GlobalNexus
{
150 ThreadNexus() : mSlot(true) { }
154 // no thread contention here!
162 PerThreadPointer
<Type
> mSlot
;
167 // A ProcessNexus is global within a single process, regardless of
168 // load module boundaries. You can have any number of ProcessNexus
169 // scopes, each identified by a C string (compared by value, not pointer).
171 class ProcessNexusBase
: public GlobalNexus
{
173 ProcessNexusBase(const char *identifier
);
182 template <class Type
>
183 class ProcessNexus
: public ProcessNexusBase
{
185 ProcessNexus(const char *identifier
) : ProcessNexusBase(identifier
) { }
186 Type
&operator () ();
192 template <class Type
>
193 Type
&ProcessNexus
<Type
>::operator () ()
195 #if !defined(PTHREAD_STRICT)
196 // not strictly kosher POSIX, but pointers are usually atomic types
198 return *reinterpret_cast<Type
*>(mStore
->mObject
);
200 StLock
<Mutex
> _(mStore
->mLock
);
201 if (mStore
->mObject
== NULL
)
202 mStore
->mObject
= new Type
;
203 return *reinterpret_cast<Type
*>(mStore
->mObject
);
207 } // end namespace Security
209 #endif //_H_GLOBALIZER