summaryrefslogtreecommitdiff
path: root/include/LockSmith.h
blob: fe2dc02c4dfa21784582e91125fee1d7a7ecfdb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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