summaryrefslogtreecommitdiff
path: root/include/nw4r/ut/ut_lock.h
blob: 03db02f8449563bb9d22b0fdfcad98d2fa8e3c6b (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
46
47
48
#ifndef NW4R_UT_LOCK_H
#define NW4R_UT_LOCK_H
#include "nw4r/types_nw4r.h"
#include "nw4r/ut/ut_NonCopyable.h"
#include "rvl/OS.h" // IWYU pragma: export

namespace nw4r {
namespace ut {
namespace detail {

inline void Lock(OSMutex &mutex) {
    OSLockMutex(&mutex);
}
inline void Unlock(OSMutex &mutex) {
    OSUnlockMutex(&mutex);
}

template <typename T>
class AutoLock : private NonCopyable {
public:
    AutoLock(T &obj) : mMutex(obj) {
        Lock(mMutex);
    }
    ~AutoLock() {
        Unlock(mMutex);
    }

private:
    T &mMutex; // at 0x0
};

} // namespace detail

class AutoInterruptLock : private NonCopyable {
public:
    AutoInterruptLock() : mEnabled(OSDisableInterrupts()) {}
    ~AutoInterruptLock() {
        OSRestoreInterrupts(mEnabled);
    }

private:
    BOOL mEnabled; // at 0x0
};

} // namespace ut
} // namespace nw4r

#endif