X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/e3d460c9de4426da6c630c3ae3f46173a99f82d8..7e6b461318c8a779d91381531435a68ee4e8b6ed:/OSX/libsecurity_utilities/lib/threading.h diff --git a/OSX/libsecurity_utilities/lib/threading.h b/OSX/libsecurity_utilities/lib/threading.h index 60168536..5886f047 100644 --- a/OSX/libsecurity_utilities/lib/threading.h +++ b/OSX/libsecurity_utilities/lib/threading.h @@ -152,6 +152,13 @@ public: ~RecursiveMutex() {} }; +class NormalMutex : public Mutex +{ +public: + NormalMutex() : Mutex(normal) {} + ~NormalMutex() {} +}; + // // Condition variables // @@ -248,6 +255,33 @@ protected: bool mActive; }; +// +// This class behaves exactly as StLock above, but accepts a pointer to a mutex instead of a reference. +// If the pointer is NULL, this class does nothing. Otherwise, it behaves as StLock. +// Try not to use this. +// +template +class StMaybeLock { +public: + StMaybeLock(Lock *lck) : me(lck), mActive(false) + { if(me) { (me->*_lock)(); mActive = true; } } + StMaybeLock(Lock *lck, bool option) : me(lck), mActive(option) { } + ~StMaybeLock() { if (me) { if(mActive) (me->*_unlock)(); } else {mActive = false;} } + + bool isActive() const { return mActive; } + void lock() { if(me) { if(!mActive) { (me->*_lock)(); mActive = true; }}} + void unlock() { if(me) { if(mActive) { (me->*_unlock)(); mActive = false; }}} + void release() { if(me) { assert(mActive); mActive = false; } } + + operator const Lock &() const { return me; } + +protected: + Lock *me; + bool mActive; +}; + // Note: if you use the TryRead or TryWrite modes, you must check if you // actually have the lock before proceeding class StReadWriteLock { @@ -260,7 +294,7 @@ public: }; StReadWriteLock(ReadWriteLock &lck, Type type) : mType(type), mIsLocked(false), mRWLock(lck) { lock(); } - ~StReadWriteLock() { if(mIsLocked) mRWLock.unlock(); } + ~StReadWriteLock() { if(mIsLocked) unlock(); } bool lock(); void unlock(); @@ -346,25 +380,6 @@ public: // class Thread { NOCOPY(Thread) -public: - class Identity { - friend class Thread; - - Identity(pthread_t id) : mIdent(id) { } - public: - Identity() { } - - static Identity current() { return pthread_self(); } - - bool operator == (const Identity &other) const - { return pthread_equal(mIdent, other.mIdent); } - - bool operator != (const Identity &other) const - { return !(*this == other); } - - private: - pthread_t mIdent; - }; public: Thread() { } // constructor @@ -378,8 +393,6 @@ protected: virtual void action() = 0; // the action to be performed private: - Identity self; // my own identity (instance constant) - static void *runner(void *); // argument to pthread_create };