]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/globalizer.cpp
0f7fa6299524ee84d64f8541b558e07980b483fa
[apple/security.git] / cdsa / cdsa_utilities / globalizer.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // globalizer - multiscope globalization services.
21 //
22 // This is a tentative, partial implementation.
23 // Status:
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)
28 //
29 // @@@ Assumption: {bool,T*} atomic unless PTHREAD_STRICT
30 //
31 #ifdef __MWERKS__
32 #define _CPP_GLOBALIZER
33 #endif
34 #include <Security/globalizer.h>
35 #include <cstdlib>
36
37
38 //
39 // The Error class thrown if Nexus operations fail
40 //
41 GlobalNexus::Error::~Error()
42 {
43 }
44
45
46 //
47 // The long (and possibly contentious) path of ModuleNexus()
48 //
49 // Briefly, the trick here is to go through a three-stage sequence
50 // to lazily construct a unique singleton object, no matter how many
51 // threads all of a sudden decide they need it.
52 // State sequence:
53 // State 0: pointer == 0, not initialized, idle
54 // State 1: pointer == mutexp | 0x1, where mutexp points to a Mutex
55 // used to serialize construction of the singleton object
56 // State 2: pointer == &singleton, and we're done
57 //
58 // TAKE NOTE:
59 // This code is optimized with a particular issue in mind: when placed
60 // into static storage (as ModuleNexi are wont to), it should not require
61 // dynamic initialization. This is important because our code is, in effect,
62 // linked into just about every program in the system. The price we pay
63 // for this coolness is
64 // (a) This won't work *except* in static storage (not on stack or heap)
65 // (b) We slightly fracture portability (see below)
66 // This has been considered Worth It, at least for now. Before you throw
67 // up and throw this code out, please try to figure out whether you know
68 // the Whole Story. Thank you.
69 //
70 // WARNING:
71 // This code makes the following non-portable assumptions:
72 // (a) NULL == 0 (binary representation of NULL pointer is zero value)
73 // Pointers acquired from new have at least their LSB zero (are at
74 // (b) least two-byte aligned).
75 // It seems like it's been a while since anyone made a machine/runtime that
76 // violated either of those. But you have been warned.
77 //
78 #if defined(_HAVE_ATOMIC_OPERATIONS)
79
80 AtomicWord ModuleNexusCommon::create(void *(*make)())
81 {
82 sync++; // keep mutex alive if needed
83 retry:
84 AtomicWord initialPointer = pointer; // latch pointer
85 if (!initialPointer || (initialPointer & 0x1)) {
86 Mutex *mutex;
87 if (initialPointer == 0) {
88 mutex = new Mutex(false); // don't bother debugging this one
89 mutex->lock();
90 if (atomicStore(pointer, AtomicWord(mutex) | 0x1, 0) != 0) {
91 // somebody beat us to the lead - back off
92 mutex->unlock();
93 delete mutex;
94 goto retry;
95 }
96 // we have the ball
97 try {
98 void *singleton = make();
99 pointer = AtomicWord(singleton);
100 // we need a write barrier here, but the mutex->unlock below provides it for free
101 debug("nexus", "ModuleNexus %p constructed object 0x%x", this, pointer);
102 } catch (...) {
103 debug("nexus", "ModuleNexus %p construction failed", this);
104 mutex->unlock();
105 sync--;
106 //@@@ set up for retry here?
107 throw;
108 }
109 } else {
110 mutex = reinterpret_cast<Mutex *>(pointer & ~0x1);
111 mutex->lock(); // we'll wait here
112 }
113 mutex->unlock();
114 //@@@ retry if not resolved -- or fail here (with "object can't be built")
115 if (--sync == 0)
116 delete mutex;
117 }
118 return pointer;
119 }
120
121 #endif //_HAVE_ATOMIC_OPERATIONS
122
123
124 //
125 // Process nexus operation
126 //
127 ProcessNexusBase::ProcessNexusBase(const char *identifier)
128 {
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
140 } else
141 if (sscanf(env, "*%p", &mStore) != 1)
142 CssmError::throwMe(CSSM_ERRCODE_INTERNAL_ERROR /*"environment communication failed"*/);
143 }