#ifndef __LOCKSMITH_H__ #define __LOCKSMITH_H__ #include #include /*! 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