JackMutex.h 1.73 KB
Newer Older
sletz's avatar
sletz committed
1
2
/*
 Copyright (C) 2006 Grame
sletz's avatar
sletz committed
3

sletz's avatar
sletz committed
4
5
6
7
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
sletz's avatar
sletz committed
8

sletz's avatar
sletz committed
9
10
11
12
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
sletz's avatar
sletz committed
13

sletz's avatar
sletz committed
14
15
16
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
sletz's avatar
sletz committed
17

sletz's avatar
sletz committed
18
19
 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
 grame@grame.fr
sletz's avatar
sletz committed
20
21
22
23
24
*/

#ifndef __JackMutex__
#define __JackMutex__

sletz's avatar
sletz committed
25
26
#include <assert.h>
#include "JackError.h"
sletz's avatar
sletz committed
27
28
#include "JackPlatformPlug.h"

sletz's avatar
sletz committed
29

sletz's avatar
sletz committed
30
namespace Jack
sletz's avatar
sletz committed
31
{
sletz's avatar
sletz committed
32
33
34
35
/*!
\brief Base class for "lockable" objects.
*/

sletz's avatar
sletz committed
36
37
38
class JackLockAble
{

39
    protected:
40
41
    
        JackMutex fMutex;
sletz's avatar
sletz committed
42

sletz's avatar
sletz committed
43
44
        JackLockAble()
        {}
45
        ~JackLockAble()
sletz's avatar
sletz committed
46
        {}
sletz's avatar
sletz committed
47

48
49
    public:
     
sletz's avatar
sletz committed
50
51
52
53
        void Lock()
        {
            fMutex.Lock();
        }
sletz's avatar
sletz committed
54

sletz's avatar
sletz committed
55
56
57
58
59
        bool Trylock()
        {
            return fMutex.Trylock();
        }

sletz's avatar
Cleanup    
sletz committed
60
        void Unlock()
sletz's avatar
sletz committed
61
62
63
        {
            fMutex.Unlock();
        }
sletz's avatar
sletz committed
64
65
66
67
68

};

class JackLock
{
sletz's avatar
sletz committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    private:

        JackLockAble* fObj;

    public:

        JackLock(JackLockAble* obj): fObj(obj)
        {
            fObj->Lock();
        }

        JackLock(const JackLockAble* obj): fObj((JackLockAble*)obj)
        {
            fObj->Lock();
        }

85
        ~JackLock()
sletz's avatar
sletz committed
86
87
88
        {
            fObj->Unlock();
        }
sletz's avatar
sletz committed
89
90
91
92
93
94
};


} // namespace

#endif