diff options
Diffstat (limited to 'include/LockSmith.h')
| -rw-r--r-- | include/LockSmith.h | 45 | 
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  | 
