danderson@0: // vim: set sts=8 ts=2 sw=2 tw=99 et: danderson@0: // danderson@0: // Copyright (C) 2013, David Anderson and AlliedModders LLC danderson@0: // All rights reserved. danderson@0: // danderson@0: // Redistribution and use in source and binary forms, with or without danderson@0: // modification, are permitted provided that the following conditions are met: danderson@0: // danderson@0: // * Redistributions of source code must retain the above copyright notice, this danderson@0: // list of conditions and the following disclaimer. danderson@0: // * Redistributions in binary form must reproduce the above copyright notice, danderson@0: // this list of conditions and the following disclaimer in the documentation danderson@0: // and/or other materials provided with the distribution. danderson@0: // * Neither the name of AlliedModders LLC nor the names of its contributors danderson@0: // may be used to endorse or promote products derived from this software danderson@0: // without specific prior written permission. danderson@0: // danderson@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" danderson@0: // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE danderson@0: // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE danderson@0: // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE danderson@0: // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR danderson@0: // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF danderson@0: // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS danderson@0: // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN danderson@0: // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) danderson@0: // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE danderson@0: // POSSIBILITY OF SUCH DAMAGE. danderson@0: danderson@0: #ifndef _include_amtl_threads_ danderson@0: #define _include_amtl_threads_ danderson@0: danderson@0: #include danderson@0: #if defined(_MSC_VER) danderson@0: # include danderson@0: # include danderson@0: #else danderson@0: # include danderson@0: #endif danderson@0: #include danderson@0: danderson@0: // Thread primitives for SourcePawn. danderson@0: // danderson@0: // Linking Requirements: danderson@0: // danderson@0: // OS X: None (-lpthread and -ldl are optional) danderson@0: // Windows: None danderson@0: // Linux: -lpthread -lrt required danderson@0: // danderson@0: // -- Mutexes -- danderson@0: // danderson@0: // A Lockable is a mutual exclusion primitive. It can be owned by at most one danderson@0: // thread at a time, and ownership blocks any other thread from taking taking danderson@0: // ownership. Ownership must be acquired and released on the same thread. danderson@0: // Lockables are not re-entrant. danderson@0: // danderson@0: // While a few classes support the Lockable interface, the simplest Lockable danderson@0: // object that can be instantiated is a Mutex. danderson@0: // danderson@0: // -- Condition Variables -- danderson@0: // danderson@0: // A ConditionVariable provides mutually exclusive access based on a danderson@0: // condition ocurring. CVs provide two capabilities: Wait(), which will block danderson@0: // until the condition is triggered, and Notify(), which signals any blocking danderson@0: // thread that the condition has occurred. danderson@0: // danderson@0: // Condition variables have an underlying mutex lock. This lock must be danderson@0: // acquired before calling Wait() or Notify(). It is automatically released danderson@0: // once Wait begins blocking. This operation is atomic with respect to other danderson@0: // threads and the mutex. For example, it is not possible for the lock to be danderson@0: // acquired by another thread in between unlocking and blocking. Since Notify danderson@0: // also requires the lock to be acquired, there is no risk of an event danderson@0: // accidentally dissipating into thin air because it was sent before the other danderson@0: // thread began blocking. danderson@0: // danderson@0: // When Wait() returns, the lock is automatically re-acquired. This operation danderson@0: // is NOT atomic. In between waking up and re-acquiring the lock, another danderson@0: // thread may steal the lock and issue another event. Applications must danderson@0: // account for this. For example, a message pump should check that there are danderson@0: // no messages left to process before blocking again. danderson@0: // danderson@0: // Likewise, it is also not defined whether a Signal() will have any effect danderson@0: // while a thread is not waiting on the monitor. This is yet another reason danderson@0: // the above paragraph is so important - applications should, under a lock of danderson@0: // the condition variable - check for state changes before waiting. danderson@0: // danderson@0: // -- Threads -- danderson@0: // danderson@0: // A Thread object, when created, spawns a new thread with the given callback danderson@0: // (the callbacks must implement IRunnable). Threads have one method of danderson@0: // interest, Join(), which will block until the thread's execution finishes. danderson@0: // Deleting a thread object will free any operating system resources associated danderson@0: // with that thread, if the thread has finished executing. danderson@0: // danderson@0: // Threads can fail to spawn; make sure to check Succeeded(). danderson@0: // danderson@0: danderson@0: namespace ke { danderson@0: danderson@0: // Abstraction for getting a unique thread identifier. Debug-only. danderson@0: #if defined(_MSC_VER) danderson@0: typedef DWORD ThreadId; danderson@0: danderson@0: static inline ThreadId GetCurrentThreadId() danderson@0: { danderson@0: return ::GetCurrentThreadId(); danderson@0: } danderson@0: #else danderson@0: typedef pthread_t ThreadId; danderson@0: danderson@0: static inline ThreadId GetCurrentThreadId() danderson@0: { danderson@0: return pthread_self(); danderson@0: } danderson@0: #endif danderson@0: danderson@0: // Classes which use non-reentrant, same-thread lock/unlock semantics should danderson@0: // inherit from this and implement DoLock/DoUnlock. danderson@0: class Lockable danderson@0: { danderson@0: public: danderson@0: Lockable() danderson@0: { danderson@0: #if !defined(NDEBUG) danderson@0: owner_ = 0; danderson@0: #endif danderson@0: } danderson@0: virtual ~Lockable() { danderson@0: } danderson@0: danderson@0: bool TryLock() { danderson@0: if (DoTryLock()) { danderson@0: DebugSetLocked(); danderson@0: return true; danderson@0: } danderson@0: return false; danderson@0: } danderson@0: danderson@0: void Lock() { danderson@0: assert(Owner() != GetCurrentThreadId()); danderson@0: DoLock(); danderson@0: DebugSetLocked(); danderson@0: } danderson@0: danderson@0: void Unlock() { danderson@0: assert(Owner() == GetCurrentThreadId()); danderson@0: DebugSetUnlocked(); danderson@0: DoUnlock(); danderson@0: } danderson@0: danderson@0: void AssertCurrentThreadOwns() const { danderson@0: assert(Owner() == GetCurrentThreadId()); danderson@0: } danderson@0: #if !defined(NDEBUG) danderson@0: bool Locked() const { danderson@0: return owner_ != 0; danderson@0: } danderson@0: ThreadId Owner() const { danderson@0: return owner_; danderson@0: } danderson@0: #endif danderson@0: danderson@0: virtual bool DoTryLock() = 0; danderson@0: virtual void DoLock() = 0; danderson@0: virtual void DoUnlock() = 0; danderson@0: danderson@0: protected: danderson@0: void DebugSetUnlocked() { danderson@0: #if !defined(NDEBUG) danderson@0: owner_ = 0; danderson@0: #endif danderson@0: } danderson@0: void DebugSetLocked() { danderson@0: #if !defined(NDEBUG) danderson@0: owner_ = GetCurrentThreadId(); danderson@0: #endif danderson@0: } danderson@0: danderson@0: protected: danderson@0: #if !defined(NDEBUG) danderson@0: ThreadId owner_; danderson@0: #endif danderson@0: }; danderson@0: danderson@0: // RAII for automatically locking and unlocking an object. danderson@0: class AutoLock danderson@0: { danderson@0: public: danderson@0: AutoLock(Lockable *lock) danderson@0: : lock_(lock) danderson@0: { danderson@0: lock_->Lock(); danderson@0: } danderson@0: ~AutoLock() { danderson@0: lock_->Unlock(); danderson@0: } danderson@0: danderson@0: private: danderson@0: Lockable *lock_; danderson@0: }; danderson@0: danderson@0: class AutoTryLock danderson@0: { danderson@0: public: danderson@0: AutoTryLock(Lockable *lock) danderson@0: { danderson@0: lock_ = lock->TryLock() ? lock : NULL; danderson@0: } danderson@0: ~AutoTryLock() { danderson@0: if (lock_) danderson@0: lock_->Unlock(); danderson@0: } danderson@0: danderson@0: private: danderson@0: Lockable *lock_; danderson@0: }; danderson@0: danderson@0: // RAII for automatically unlocking and relocking an object. danderson@0: class AutoUnlock danderson@0: { danderson@0: public: danderson@0: AutoUnlock(Lockable *lock) danderson@0: : lock_(lock) danderson@0: { danderson@0: lock_->Unlock(); danderson@0: } danderson@0: ~AutoUnlock() { danderson@0: lock_->Lock(); danderson@0: } danderson@0: danderson@0: private: danderson@0: Lockable *lock_; danderson@0: }; danderson@0: danderson@0: enum WaitResult { danderson@0: // Woke up because something happened. danderson@0: Wait_Signaled, danderson@0: danderson@0: // Woke up because nothing happened and a timeout was specified. danderson@0: Wait_Timeout, danderson@0: danderson@0: // Woke up, but because of an error. danderson@0: Wait_Error danderson@0: }; danderson@0: danderson@0: // This must be implemented in order to spawn a new thread. danderson@0: class IRunnable danderson@0: { danderson@0: public: danderson@0: virtual ~IRunnable() { danderson@0: } danderson@0: danderson@0: virtual void Run() = 0; danderson@0: }; danderson@0: danderson@0: } // namespace ke danderson@0: danderson@0: // Include the actual thread implementations. danderson@0: #if defined(_MSC_VER) danderson@0: # include "am-thread-windows.h" danderson@0: #else danderson@0: # include "am-thread-posix.h" danderson@0: #endif danderson@0: danderson@0: #endif // _include_amtl_threads_