2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // globalizer - multiscope globalization services.
22 // This is a tentative, partial implementation.
24 // module scope: constructs, optional cleanup
25 // thread scope: constructs, optional cleanup
26 // process scope: not implemented (obsolete implementation, unused)
27 // system scope: not implemented (probably never will)
29 // @@@ Assumption: {bool,T*} atomic unless PTHREAD_STRICT
31 #include <Security/globalizer.h>
32 #include <Security/debugging.h>
37 // The Error class thrown if Nexus operations fail
39 GlobalNexus::Error::~Error() throw()
45 // The long (and possibly contentious) path of ModuleNexus()
47 // Briefly, the trick here is to go through a three-stage sequence
48 // to lazily construct a unique singleton object, no matter how many
49 // threads all of a sudden decide they need it.
51 // State 0: pointer == 0, not initialized, idle
52 // State 1: pointer == mutexp | 0x1, where mutexp points to a Mutex
53 // used to serialize construction of the singleton object
54 // State 2: pointer == &singleton, and we're done
57 // This code is optimized with a particular issue in mind: when placed
58 // into static storage (as ModuleNexi are wont to), it should not require
59 // dynamic initialization. This is important because our code is, in effect,
60 // linked into just about every program in the system. The price we pay
61 // for this coolness is
62 // (a) This won't work *except* in static storage (not on stack or heap)
63 // (b) We slightly fracture portability (see below)
64 // This has been considered Worth It, at least for now. Before you throw
65 // up and throw this code out, please try to figure out whether you know
66 // the Whole Story. Thank you.
69 // This code makes the following non-portable assumptions:
70 // (a) NULL == 0 (binary representation of NULL pointer is zero value)
71 // (b) Pointers acquired from new have at least their LSB zero (are at
72 // least two-byte aligned).
73 // It seems like it's been a while since anyone made a machine/runtime that
74 // violated either of those. But you have been warned.
76 #if defined(_HAVE_ATOMIC_OPERATIONS)
78 AtomicWord
ModuleNexusCommon::create(void *(*make
)())
80 sync
++; // keep mutex alive if needed
82 AtomicWord initialPointer
= pointer
; // latch pointer
83 if (!initialPointer
|| (initialPointer
& 0x1)) {
85 if (initialPointer
== 0) {
86 mutex
= new Mutex(false); // don't bother debugging this one
88 if (atomicStore(pointer
, AtomicWord(mutex
) | 0x1, 0) != 0) {
89 // somebody beat us to the lead - back off
96 void *singleton
= make();
97 pointer
= AtomicWord(singleton
);
98 // we need a write barrier here, but the mutex->unlock below provides it for free
99 debug("nexus", "ModuleNexus %p constructed object 0x%x", this, pointer
);
101 debug("nexus", "ModuleNexus %p construction failed", this);
110 mutex
= reinterpret_cast<Mutex
*>(initialPointer
& ~0x1);
111 mutex
->lock(); // we'll wait here
114 //@@@ retry if not resolved -- or fail here (with "object can't be built")
121 #endif //_HAVE_ATOMIC_OPERATIONS
125 // Process nexus operation
127 ProcessNexusBase::ProcessNexusBase(const char *identifier
)
129 const char *env
= getenv(identifier
);
130 if (env
== NULL
) { // perhaps we're first...
131 auto_ptr
<Store
> store(new Store
);
132 char form
[2*sizeof(Store
*) + 2];
133 sprintf(form
, "*%p", &store
);
134 setenv(identifier
, form
, 0); // do NOT overwrite...
135 env
= getenv(identifier
); // ... and refetch to resolve races
136 if (sscanf(env
, "*%p", &mStore
) != 1)
137 CssmError::throwMe(CSSM_ERRCODE_INTERNAL_ERROR
/*"environment communication failed" */);
138 if (mStore
== store
.get()) // we won the race...
139 store
.release(); // ... so keep the store
141 if (sscanf(env
, "*%p", &mStore
) != 1)
142 CssmError::throwMe(CSSM_ERRCODE_INTERNAL_ERROR
/*"environment communication failed"*/);