summaryrefslogtreecommitdiffstats
path: root/K2LABI/Common/SystemUtilities/SLock.h
blob: d08ddb3b71d0f4fa3d43d1634ab527146c15d6b9 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef S_LOCK_H
#define S_LOCK_H

#include "windows-adapter.h"

//-----------------------------------------------------------------------------
class CLock
{
public:
    //-----------------------------------------------------------------------------
    CLock(bool bCreate = true)
    {
        m_bCreated = false;
        if (bCreate)
        {
            create();
        }
    }
    //-----------------------------------------------------------------------------
    ~CLock()
    {
        destroy();
    }
    //-----------------------------------------------------------------------------
    virtual int create()
    {
        ::InitializeCriticalSection(&m_CS);
        m_bCreated = true;
        return 0;
    }
    //-----------------------------------------------------------------------------
    virtual int destroy()
    {
        if (m_bCreated)
        {
            ::DeleteCriticalSection(&m_CS);
            m_bCreated = false;
        }
        return 0;
    }
    //-----------------------------------------------------------------------------
    virtual int lock()
    {
        ::EnterCriticalSection(&m_CS);
        return 0;
    }
    //-----------------------------------------------------------------------------
    virtual int unlock()
    {
        ::LeaveCriticalSection(&m_CS);
        return 0;
    }
    //-----------------------------------------------------------------------------
private:
    CRITICAL_SECTION m_CS;
    bool m_bCreated;
};
//-----------------------------------------------------------------------------
class CSafeLock
{
public:
    CSafeLock(CLock* p) : m_pLock(p)
    {
        m_pLock->lock();
    }

    ~CSafeLock()
    {
        m_pLock->unlock();
        m_pLock = 0;
    }

private:
    CLock* m_pLock;
};
//-----------------------------------------------------------------------------

#endif // S_LOCK_H