]> git.saurik.com Git - apple/security.git/blobdiff - OSX/libsecurity_utilities/lib/threading.cpp
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / threading.cpp
index 9e0bed6454d8b2aa7bbe68593ec23bbd79f0222b..4472c7f7c5ce09435ec34e4bde9ace779f596f86 100644 (file)
@@ -28,6 +28,7 @@
 #include <security_utilities/threading.h>
 #include <security_utilities/globalizer.h>
 #include <security_utilities/memutils.h>
+#include <utilities/debugging.h>
 
 #include <unistd.h>     // WWDC 2007 thread-crash workaround
 #include <syslog.h>     // WWDC 2007 thread-crash workaround
@@ -91,6 +92,9 @@ Mutex::Mutex(Type type)
 Mutex::~Mutex()
 {
     int result = pthread_mutex_destroy(&me);
+    if(result) {
+        secerror("Probable bug: error destroying Mutex: %d", result);
+    }
        check(result);
 }
 
@@ -156,7 +160,6 @@ void CountingMutex::enter()
 {
     lock();
     mCount++;
-    secdebug("cmutex", "%p up to %d", this, mCount);
     unlock();
 }
 
@@ -165,7 +168,6 @@ bool CountingMutex::tryEnter()
     if (!tryLock())
         return false;
     mCount++;
-    secdebug("cmutex", "%p up to %d (was try)", this, mCount);
     unlock();
     return true;
 }
@@ -175,14 +177,12 @@ void CountingMutex::exit()
     lock();
     assert(mCount > 0);
     mCount--;
-    secdebug("cmutex", "%p down to %d", this, mCount);
     unlock();
 }
 
 void CountingMutex::finishEnter()
 {
     mCount++;
-    secdebug("cmutex", "%p finish up to %d", this, mCount);
     unlock();
 }
 
@@ -190,10 +190,60 @@ void CountingMutex::finishExit()
 {
     assert(mCount > 0);
     mCount--; 
-    secdebug("cmutex", "%p finish down to %d", this, mCount);
     unlock();
 }
 
+//
+// ReadWriteLock implementation
+//
+ReadWriteLock::ReadWriteLock() {
+    check(pthread_rwlock_init(&mLock, NULL));
+}
+
+bool ReadWriteLock::lock() {
+    check(pthread_rwlock_rdlock(&mLock));
+    return true;
+}
+
+bool ReadWriteLock::tryLock() {
+    return (pthread_rwlock_tryrdlock(&mLock) == 0);
+}
+
+bool ReadWriteLock::writeLock() {
+    check(pthread_rwlock_wrlock(&mLock));
+    return true;
+}
+
+bool ReadWriteLock::tryWriteLock() {
+    return (pthread_rwlock_trywrlock(&mLock) == 0);
+}
+
+void ReadWriteLock::unlock() {
+    check(pthread_rwlock_unlock(&mLock));
+}
+
+//
+// StReadWriteLock implementation
+//
+bool StReadWriteLock::lock() {
+    switch(mType) {
+        case Read:     mIsLocked = mRWLock.lock(); break;
+        case TryRead:  mIsLocked = mRWLock.tryLock(); break;
+        case Write:    mIsLocked = mRWLock.writeLock(); break;
+        case TryWrite: mIsLocked = mRWLock.tryWriteLock(); break;
+    }
+    return mIsLocked;
+}
+
+void StReadWriteLock::unlock() {
+    mRWLock.unlock();
+    mIsLocked = false;
+}
+
+bool StReadWriteLock::isLocked() {
+    return mIsLocked;
+}
+
 
 
 //
@@ -205,6 +255,7 @@ Thread::~Thread()
 
 void Thread::run()
 {
+    pthread_t pt;
     pthread_attr_t ptattrs;
     int err, ntries = 10;       // 10 is arbitrary
 
@@ -213,7 +264,7 @@ void Thread::run()
     {
         syslog(LOG_ERR, "error %d setting thread detach state", err);
     }
-    while ((err = pthread_create(&self.mIdent, &ptattrs, runner, this) && 
+    while ((err = pthread_create(&pt, &ptattrs, runner, this) &&
            --ntries))
     {
         syslog(LOG_ERR, "pthread_create() error %d", err);
@@ -224,7 +275,7 @@ void Thread::run()
         syslog(LOG_ERR, "too many failed pthread_create() attempts");
     }
     else
-        secdebug("thread", "%p created", self.mIdent);
+        secinfo("thread", "%p created", pt);
 }
 
 void *Thread::runner(void *arg)
@@ -233,9 +284,9 @@ void *Thread::runner(void *arg)
         // otherwise it will crash if something underneath throws.
     {
         Thread *me = static_cast<Thread *>(arg);
-        secdebug("thread", "%p starting", me->self.mIdent);
+        secinfo("thread", "%p starting", pthread_self());
         me->action();
-        secdebug("thread", "%p terminating", me->self.mIdent);
+        secinfo("thread", "%p terminating", pthread_self());
         delete me;
         return NULL;
     }