summaryrefslogtreecommitdiff
path: root/src/thread
diff options
context:
space:
mode:
authorfonsinchen <fonsinchen@openttd.org>2014-02-16 16:24:41 +0000
committerfonsinchen <fonsinchen@openttd.org>2014-02-16 16:24:41 +0000
commitdc0f89b7e9bae8486771088e88b574c567b0dddc (patch)
treef513d6b7c1c49d1082ca7f4c588eb2ad8b83f115 /src/thread
parent2945e762691740993e0b62c54aa0f978ce15dbe9 (diff)
downloadopenttd-dc0f89b7e9bae8486771088e88b574c567b0dddc.tar.xz
(svn r26342) -Add: A mutex locker class.
Diffstat (limited to 'src/thread')
-rw-r--r--src/thread/thread.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/thread/thread.h b/src/thread/thread.h
index edb7d474e..a98dfb223 100644
--- a/src/thread/thread.h
+++ b/src/thread/thread.h
@@ -89,6 +89,28 @@ public:
};
/**
+ * Simple mutex locker to keep a mutex locked until the locker goes out of scope.
+ */
+class ThreadMutexLocker {
+public:
+ /**
+ * Lock the mutex and keep it locked for the life time of this object.
+ * @param mutex Mutex to be locked.
+ */
+ ThreadMutexLocker(ThreadMutex *mutex) : mutex(mutex) { mutex->BeginCritical(); }
+
+ /**
+ * Unlock the mutex.
+ */
+ ~ThreadMutexLocker() { this->mutex->EndCritical(); }
+
+private:
+ ThreadMutexLocker(const ThreadMutexLocker &) { NOT_REACHED(); }
+ ThreadMutexLocker &operator=(const ThreadMutexLocker &) { NOT_REACHED(); return *this; }
+ ThreadMutex *mutex;
+};
+
+/**
* Get number of processor cores in the system, including HyperThreading or similar.
* @return Total number of processor cores.
*/