]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/threading.cpp
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 // threading - generic thread support
25 // Since we are planning to generate "stub" out of line code for threading methods,
26 // we must force THREAD_NDEBUG to off while compiling our header. Trust me.
28 #include <Security/threading.h>
29 #include <Security/memutils.h>
33 // Thread-local storage primitive
35 #if _USE_THREADS == _USE_PTHREADS
37 ThreadStoreSlot::ThreadStoreSlot(Destructor
*destructor
)
39 if (int err
= pthread_key_create(&mKey
, destructor
))
40 UnixError::throwMe(err
);
43 ThreadStoreSlot::~ThreadStoreSlot()
45 //@@@ if we wanted to dispose of pending task objects, we'd have
46 //@@@ to keep a set of them and delete them explicitly here
47 pthread_key_delete(mKey
);
54 // Mutex implementation
56 #if _USE_THREADS == _USE_PTHREADS
58 bool Mutex::debugHasInitialized
;
59 bool Mutex::loggingMutexi
;
61 Mutex::Mutex(bool log
)
63 #if !defined(THREAD_NDEBUG)
64 // this debug-setup code isn't interlocked, but it's idempotent
65 // (don't worry, be happy)
66 if (!debugHasInitialized
) {
67 loggingMutexi
= Debug::debugging("mutex") || Debug::debugging("mutex-c");
68 debugHasInitialized
= true;
70 debugLog
= log
&& loggingMutexi
;
71 useCount
= contentionCount
= 0;
74 #endif //THREAD_NDEBUG
75 check(pthread_mutex_init(&me
, NULL
));
80 #if !defined(THREAD_NDEBUG)
82 if (contentionCount
> 0)
83 secdebug("mutex-c", "%p destroyed after %ld/%ld locks/contentions",
84 this, useCount
, contentionCount
);
85 else if (useCount
> 100)
86 secdebug("mutex", "%p destroyed after %ld locks", this, useCount
);
88 #endif //THREAD_NDEBUG
89 check(pthread_mutex_destroy(&me
));
94 #if !defined(THREAD_NDEBUG)
97 switch (int err
= pthread_mutex_trylock(&me
)) {
102 secdebug("mutex-c", "%p contended (%ld of %ld)", this, ++contentionCount
, useCount
);
103 check(pthread_mutex_lock(&me
));
106 UnixError::throwMe(err
);
108 if (useCount
% 100 == 0)
109 secdebug("mutex", "%p locked %ld", this, useCount
);
111 secdebug("mutex", "%p locked", this);
114 #endif //THREAD_NDEBUG
115 check(pthread_mutex_lock(&me
));
118 bool Mutex::tryLock()
121 if (int err
= pthread_mutex_trylock(&me
)) {
123 UnixError::throwMe(err
);
124 #if !defined(THREAD_NDEBUG)
126 secdebug("mutex-c", "%p trylock contended (%ld of %ld)",
127 this, ++contentionCount
, useCount
);
128 #endif //THREAD_NDEBUG
131 #if !defined(THREAD_NDEBUG)
133 if (useCount
% 100 == 0)
134 secdebug("mutex", "%p locked %ld", this, useCount
);
136 secdebug("mutex", "%p locked", this);
137 #endif //THREAD_NDEBUG
143 #if !defined(MUTEX_NDEBUG)
145 secdebug("mutex", "%p unlocked", this);
146 #endif //MUTEX_NDEBUG
147 check(pthread_mutex_unlock(&me
));
154 // CountingMutex implementation.
155 // Note that this is a generic implementation based on a specific Mutex type.
156 // In other words, it should work no matter how Mutex is implemented.
157 // Also note that CountingMutex is expected to interlock properly with Mutex,
158 // so you canNOT just use an AtomicCounter here.
160 void CountingMutex::enter()
164 secdebug("mutex", "%p up to %d", this, mCount
);
168 bool CountingMutex::tryEnter()
173 secdebug("mutex", "%p up to %d (was try)", this, mCount
);
178 void CountingMutex::exit()
183 secdebug("mutex", "%p down to %d", this, mCount
);
187 void CountingMutex::finishEnter()
190 secdebug("mutex", "%p finish up to %d", this, mCount
);
194 void CountingMutex::finishExit()
198 secdebug("mutex", "%p finish down to %d", this, mCount
);
205 // Threads implementation
207 #if _USE_THREADS == _USE_PTHREADS
215 if (int err
= pthread_create(&self
.mIdent
, NULL
, runner
, this))
216 UnixError::throwMe(err
);
217 secdebug("thread", "%p created", self
.mIdent
);
220 void *Thread::runner(void *arg
)
222 Thread
*me
= static_cast<Thread
*>(arg
);
223 if (int err
= pthread_detach(me
->self
.mIdent
))
224 UnixError::throwMe(err
);
225 secdebug("thread", "%p starting", me
->self
.mIdent
);
227 secdebug("thread", "%p terminating", me
->self
.mIdent
);
239 // Make a more-or-less unique string representation of a thread id.
240 // This is meant FOR DEBUGGING ONLY. Don't use this in production code.
242 void Thread::Identity::getIdString(char id
[idLength
])
244 pthread_t current
= pthread_self();
245 // We're not supposed to know what a pthread_t is. Just print the first few bytes...
246 // (On MacOS X, it's a pointer to a pthread_t internal structure, so this works fine.)
248 memcpy(&ids
, ¤t
, sizeof(ids
));
249 snprintf(id
, idLength
, "%lx", ids
);
257 // ThreadRunner implementation
259 ThreadRunner::ThreadRunner(Action
*todo
)
265 void ThreadRunner::action()
273 // This implementation uses mWait as a "sloppy" wait blocker (only).
274 // It should be a semaphore of course, but we don't have a semaphore
275 // abstraction right now. The authoritative locking protocol is based on mLock.
277 NestingMutex::NestingMutex() : mCount(0)
280 void NestingMutex::lock()
288 bool NestingMutex::tryLock()
290 StLock
<Mutex
> _(mLock
);
291 if (mCount
== 0) { // initial lock
293 mIdent
= Thread::Identity::current();
296 } else if (mIdent
== Thread::Identity::current()) { // recursive lock
299 } else { // locked by another thread
304 void NestingMutex::unlock()
306 StLock
<Mutex
> _(mLock
);
307 assert(mCount
> 0 && mIdent
== Thread::Identity::current());
308 if (--mCount
== 0) // last recursive unlock