File: SharedLock.cpp

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 (94 lines) | stat: -rw-r--r-- 3,535 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
/***************************************************************************
         SharedLock.cpp  -  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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <qobject.h> // for Q_ASSERT(...)
#include "mt/SharedLock.h"

//***************************************************************************
//***************************************************************************
SharedLock::SharedLock()
    :TSS_Object(), m_shared_count(0), m_lock_exclusive(), m_lock_changed()
{
}

//***************************************************************************
SharedLock::~SharedLock()
{
    Q_ASSERT(!m_shared_count);
}

//***************************************************************************
void SharedLock::lock_exclusive()
{
    // get exclusive access to m_shared_count, also makes sure
    // that we are not in an exclusively locked region
    m_lock_exclusive.lock();

    while (m_shared_count) {
	// still locked for shared access, unlock again and
	// wait for a change
	m_lock_exclusive.unlock();
	m_lock_changed.wait();
	
	// lock exclusively, will be unlocked in unlock_exclusive()
	m_lock_exclusive.lock();
    }
}

//***************************************************************************
void SharedLock::unlock_exclusive()
{
    // unlock from exclusive access and wake up all waiting threads
    m_lock_exclusive.unlock();
    m_lock_changed.notifyAll();
}

//***************************************************************************
void SharedLock::lock_shared()
{
    // get exclusive access to m_shared_count, also makes sure
    // that we are not in an exclusively locked region
    m_lock_exclusive.lock();

    // increase the number of concurrent shared locks
    m_shared_count++;

    // unlock m_shared_count again and allow further lock attempts
    m_lock_exclusive.unlock();
}

//***************************************************************************
void SharedLock::unlock_shared()
{
    // get exclusive access to m_shared_count, also makes sure
    // that we are not in an exclusively locked region
    m_lock_exclusive.lock();

    // reduce the number of concurrent shared locks
    Q_ASSERT(m_shared_count);
    if (m_shared_count) m_shared_count--;

    // wake up all threads that are waiting for exclusive access
    // if this was the last active shared lock
    if (!m_shared_count) m_lock_changed.notifyAll();

    // unlock m_shared_count again and allow further lock attempts
    m_lock_exclusive.unlock();
}

//***************************************************************************
//***************************************************************************