File: SharedLock.h

package info (click to toggle)
kwave 0.7.2-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,048 kB
  • ctags: 4,906
  • sloc: cpp: 31,275; ansic: 13,111; sh: 9,511; perl: 2,724; makefile: 786; asm: 145
file content (108 lines) | stat: -rw-r--r-- 3,322 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/***************************************************************************
           SharedLock.h  -  Threadsafe lock for shared and exclusive access
			     -------------------
    begin                : Jun 14 2001
    copyright            : (C) 2001 by Thomas Eschenbacher
    email                : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef _SHARED_LOCK_H_
#define _SHARED_LOCK_H_

#include "config.h"
#include "mt/TSS_Object.h"
#include "mt/Mutex.h"
#include "mt/ThreadCondition.h"

/**
 * @class SharedLock
 * A SharedLock allows controlling the access to a resource in shared and
 * exclusive mode. In comparism to a Mutex, it is allowed to lock the
 * resource concurrently multiple times with shared access, but only once
 * at a time with exclusive access.
 * This is useful for implementing read/write access control - use shared
 * mode for read-only access and exclusive access for read/write access.
 * @note This class should not be used directly, it is safet to use it
 *       via a SharedLockGuard !
 * @see SharedLockGuard
 * @see Mutex
 */
class SharedLock: public TSS_Object
{
public:

    /** Constructor. */
    SharedLock();

    /** Destructor. */
    virtual ~SharedLock();

    /**
     * Locks in shared or in exclusive mode. This blocks the current
     * thread until the resource is available / unlocked.
     * @param exclusive if true locks in exclusive mode,
     *                  or shared mode if false
     */
    inline void lock(bool exclusive) {
        exclusive ? lock_exclusive() : lock_shared();
    };

    /**
     * Unlocks the resource from shared or exclusive mode.
     * @param exclusive if true unlocks from exclusive mode,
     *                  or shared mode if false
     */
    inline void unlock(bool exclusive) {
        exclusive ? unlock_exclusive() : unlock_shared();
    };

private:

    /**
     * Locks in exclusive mode.
     * @internal
     */
    void lock_exclusive();

    /**
     * Unlocks from shared mode.
     * @internal
     */
    void lock_shared();

    /**
     * Unlocks from exclusive mode.
     * @internal
     */
    void unlock_exclusive();

    /**
     * Unlocks from shared mode.
     * @internal
     */
    void unlock_shared();

    /** Number of concurrent shared locks */
    unsigned int m_shared_count;

    /** Mutex for exclusively locking and some internal critical regions */
    Mutex m_lock_exclusive;

    /**
     * ThreadCondition for synchronization of threads that are waiting
     * for shared or exclusive access.
     */
    ThreadCondition m_lock_changed;

};

#endif /* _SHARED_LOCK_H_ */