]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/threading.cpp
d187a8e369e457b9fd0105af2dede826080735c2
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 #if !defined(THREAD_CLEAN_NDEBUG)
29 # define THREAD_MAKE_STUBS
31 #include <Security/threading.h>
35 // Thread-local storage primitive
37 #if _USE_THREADS == _USE_PTHREADS
39 ThreadStoreSlot::ThreadStoreSlot(Destructor
*destructor
)
41 if (int err
= pthread_key_create(&mKey
, destructor
))
42 UnixError::throwMe(err
);
45 ThreadStoreSlot::~ThreadStoreSlot()
47 //@@@ if we wanted to dispose of pending task objects, we'd have
48 //@@@ to keep a set of them and delete them explicitly here
50 // @@@ bug 2998157 does not clear slots on delete or allocate. Leak them for now
51 pthread_key_delete(mKey
);
59 // Mutex implementation
61 #if _USE_THREADS == _USE_PTHREADS
63 #if !defined(THREAD_CLEAN_NDEBUG)
65 bool Mutex::debugHasInitialized
;
66 bool Mutex::loggingMutexi
;
68 Mutex::Mutex(bool log
)
70 #if !defined(THREAD_NDEBUG)
71 // this debug-setup code isn't interlocked, but it's idempotent
72 // (don't worry, be happy)
73 if (!debugHasInitialized
) {
74 loggingMutexi
= Debug::debugging("mutex");
75 debugHasInitialized
= true;
77 debugLog
= log
&& loggingMutexi
;
78 useCount
= contentionCount
= 0;
81 #endif //THREAD_NDEBUG
82 check(pthread_mutex_init(&me
, NULL
));
87 #if !defined(THREAD_NDEBUG)
88 if (debugLog
&& (useCount
> 100 || contentionCount
> 0))
89 debug("mutex", "%p destroyed after %ld/%ld locks/contentions", this, useCount
, contentionCount
);
90 #endif //THREAD_NDEBUG
91 check(pthread_mutex_destroy(&me
));
96 #if !defined(THREAD_NDEBUG)
99 switch (int err
= pthread_mutex_trylock(&me
)) {
104 debug("mutex", "%p contended (%ld of %ld)", this, ++contentionCount
, useCount
);
105 check(pthread_mutex_lock(&me
));
108 UnixError::throwMe(err
);
110 if (useCount
% 100 == 0)
111 debug("mutex", "%p locked %ld", this, useCount
);
113 debug("mutex", "%p locked", this);
116 #endif //THREAD_NDEBUG
117 check(pthread_mutex_lock(&me
));
120 bool Mutex::tryLock()
123 if (int err
= pthread_mutex_trylock(&me
)) {
125 UnixError::throwMe(err
);
126 #if !defined(THREAD_NDEBUG)
128 debug("mutex", "%p trylock contended (%ld of %ld)",
129 this, ++contentionCount
, useCount
);
130 #endif //THREAD_NDEBUG
133 #if !defined(THREAD_NDEBUG)
135 if (useCount
% 100 == 0)
136 debug("mutex", "%p locked %ld", this, useCount
);
138 debug("mutex", "%p locked", this);
139 #endif //THREAD_NDEBUG
145 #if !defined(MUTEX_NDEBUG)
147 debug("mutex", "%p unlocked", this);
148 #endif //MUTEX_NDEBUG
149 check(pthread_mutex_unlock(&me
));
152 #endif //!THREAD_CLEAN_NDEBUG
157 // CountingMutex implementation.
158 // Note that this is a generic implementation based on a specific Mutex type.
159 // In other words, it should work no matter how Mutex is implemented.
160 // Also note that CountingMutex is expected to interlock properly with Mutex,
161 // so you canNOT just use an AtomicCounter here.
163 void CountingMutex::enter()
167 debug("mutex", "%p up to %d", this, mCount
);
171 bool CountingMutex::tryEnter()
176 debug("mutex", "%p up to %d (was try)", this, mCount
);
181 void CountingMutex::exit()
186 debug("mutex", "%p down to %d", this, mCount
);
190 void CountingMutex::finishEnter()
193 debug("mutex", "%p finish up to %d", this, mCount
);
197 void CountingMutex::finishExit()
201 debug("mutex", "%p finish down to %d", this, mCount
);
208 // Threads implementation
210 #if _USE_THREADS == _USE_PTHREADS
218 if (int err
= pthread_create(&self
.mIdent
, NULL
, runner
, this))
219 UnixError::throwMe(err
);
220 debug("thread", "%p created", self
.mIdent
);
223 void *Thread::runner(void *arg
)
225 Thread
*me
= static_cast<Thread
*>(arg
);
226 if (int err
= pthread_detach(me
->self
.mIdent
))
227 UnixError::throwMe(err
);
228 debug("thread", "%p starting", me
->self
.mIdent
);
230 debug("thread", "%p terminating", me
->self
.mIdent
);
242 #include <Security/memutils.h>
244 void Thread::Identity::getIdString(char id
[idLength
])
246 pthread_t current
= pthread_self();
247 // We're not supposed to know what a pthread_t is. Just print the first few bytes...
248 // (On MacOS X, it's a pointer to a pthread_t internal structure, so this works fine.)
250 memcpy(&p
, ¤t
, sizeof(p
));
251 snprintf(id
, idLength
, "%lx", long(p
));
260 // ThreadRunner implementation
262 ThreadRunner::ThreadRunner(Action
*todo
)
268 void ThreadRunner::action()
276 // This implementation uses mWait as a "sloppy" wait blocker (only).
277 // It should be a semaphore of course, but we don't have a semaphore
278 // abstraction right now. The authoritative locking protocol is based on mLock.
280 NestingMutex::NestingMutex() : mCount(0)
283 void NestingMutex::lock()
291 bool NestingMutex::tryLock()
293 StLock
<Mutex
> _(mLock
);
294 if (mCount
== 0) { // initial lock
296 mIdent
= Thread::Identity::current();
299 } else if (mIdent
== Thread::Identity::current()) { // recursive lock
302 } else { // locked by another thread
307 void NestingMutex::unlock()
309 StLock
<Mutex
> _(mLock
);
310 assert(mCount
> 0 && mIdent
== Thread::Identity::current());
311 if (--mCount
== 0) // last recursive unlock