summaryrefslogtreecommitdiff
path: root/include/LockSmith.h
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2009-11-13 16:28:15 -0800
committerPixel <pixel@nobis-crew.org>2009-11-13 16:28:15 -0800
commit8c415c3658e7f344dc803431726b50942e432702 (patch)
tree5fda308ffe4420e6d1ebd9df67e7535b8069ee5e /include/LockSmith.h
parentc12806450806909177058eb8e7f85dcbd24cbf1c (diff)
BigClean: Input & Archive are a little bit more threadsafe now.
Diffstat (limited to 'include/LockSmith.h')
-rw-r--r--include/LockSmith.h45
1 files changed, 45 insertions, 0 deletions
diff --git a/include/LockSmith.h b/include/LockSmith.h
new file mode 100644
index 0000000..fe2dc02
--- /dev/null
+++ b/include/LockSmith.h
@@ -0,0 +1,45 @@
+#ifndef __LOCKSMITH_H__
+#define __LOCKSMITH_H__
+
+#include <Exceptions.h>
+#include <Atomic.h>
+
+/*!
+ The locker class, if it exists, will be used to produce interlocking
+ inside the nested classes when needed. Define it if you're going to build
+ a real threaded software with Baltisot.
+*/
+
+class locker_t : public Base {
+ public:
+ virtual void lock() {}
+ virtual void unlock() {}
+};
+
+class LockSmith : public Base {
+ public:
+ LockSmith() { GlobalLockSmith = this; }
+ static locker_t * SpawnLock() { return GlobalLockSmith->SpawnSpecificLock(); }
+ protected:
+ virtual locker_t * SpawnSpecificLock() { return new locker_t(); }
+ static LockSmith * GlobalLockSmith;
+};
+
+class iLock : public Base {
+ public:
+ iLock() { }
+ ~iLock() { if (locker) delete(locker); }
+ void lock() {
+ if (LIKELY(locker)) {
+ locker->lock();
+ } else {
+ locker = LockSmith::SpawnLock();
+ locker->lock();
+ }
+ }
+ void unlock() { locker->unlock(); }
+ private:
+ locker_t * locker;
+};
+
+#endif