]>
git.saurik.com Git - apple/security.git/blob - libsecurity_utilities/lib/globalizer.cpp
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.
28 // This is a tentative, partial implementation.
30 // module scope: constructs, optional cleanup
31 // thread scope: constructs, optional cleanup
32 // process scope: not implemented (obsolete implementation, unused)
33 // system scope: not implemented (probably never will)
35 // @@@ Assumption: {bool,T*} atomic unless PTHREAD_STRICT
37 #include <security_utilities/globalizer.h>
38 #include <security_utilities/debugging.h>
43 // The Error class thrown if Nexus operations fail
45 GlobalNexus::Error::~Error() throw()
51 // The long (and possibly contentious) path of ModuleNexus()
53 // Briefly, the trick here is to go through a three-stage sequence
54 // to lazily construct a unique singleton object, no matter how many
55 // threads all of a sudden decide they need it.
57 // State 0: pointer == 0, not initialized, idle
58 // State 1: pointer == mutexp | 0x1, where mutexp points to a Mutex
59 // used to serialize construction of the singleton object
60 // State 2: pointer == &singleton, and we're done
63 // This code is optimized with a particular issue in mind: when placed
64 // into static storage (as ModuleNexi are wont to), it should not require
65 // dynamic initialization. This is important because our code is, in effect,
66 // linked into just about every program in the system. The price we pay
67 // for this coolness is
68 // (a) This won't work *except* in static storage (not on stack or heap)
69 // (b) We slightly fracture portability (see below)
70 // This has been considered Worth It, at least for now. Before you throw
71 // up and throw this code out, please try to figure out whether you know
72 // the Whole Story. Thank you.
75 // This code makes the following non-portable assumptions:
76 // (a) NULL == 0 (binary representation of NULL pointer is zero value)
77 // (b) Pointers acquired from new have at least their LSB zero (are at
78 // least two-byte aligned).
79 // It seems like it's been a while since anyone made a machine/runtime that
80 // violated either of those. But you have been warned.
82 void *ModuleNexusCommon::create(void *(*make
)())
84 sync
++; // keep mutex alive if needed
86 void *initialPointer
= Atomic
<void *>::load(pointer
); // latch pointer
87 if (!initialPointer
|| (uintptr_t(initialPointer
) & 0x1)) {
89 if (initialPointer
== 0) {
92 if (!Atomic
<void *>::casb(0, (void *)(uintptr_t(mutex
) | 0x1), pointer
)) {
93 // somebody beat us to the lead - back off
100 void *singleton
= make();
102 // we need a write barrier here, but the mutex->unlock below provides it for free
104 secdebug("nexus", "ModuleNexus %p construction failed", this);
113 mutex
= reinterpret_cast<Mutex
*>(uintptr_t(initialPointer
) & ~0x1);
114 mutex
->lock(); // we'll wait here
117 //@@@ retry if not resolved -- or fail here (with "object can't be built")
125 // thread nexus static globals
126 ModuleNexus
<Mutex
> ThreadNexusBase::mInstanceLock
;
128 // Thread nexus globals
129 ModuleNexus
<RetentionSet
> ThreadNexusBase::mInstances
;
132 // Process nexus operation
134 ProcessNexusBase::ProcessNexusBase(const char *identifier
)
136 const char *env
= getenv(identifier
);
137 if (env
== NULL
) { // perhaps we're first...
138 auto_ptr
<Store
> store(new Store
);
139 char form
[2*sizeof(Store
*) + 2];
140 sprintf(form
, "*%p", &store
);
141 setenv(identifier
, form
, 0); // do NOT overwrite...
142 env
= getenv(identifier
); // ... and refetch to resolve races
143 if (sscanf(env
, "*%p", &mStore
) != 1)
144 throw std::runtime_error("environment communication failed");
145 if (mStore
== store
.get()) // we won the race...
146 store
.release(); // ... so keep the store
148 if (sscanf(env
, "*%p", &mStore
) != 1)
149 throw std::runtime_error("environment communication failed");