File: Mutex.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 (125 lines) | stat: -rw-r--r-- 3,918 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/***************************************************************************
                mt/Mutex.cpp  -  Synchronization by Mutal Exclusion
                             -------------------
    begin                : Tue Oct 3 2000
    copyright            : (C) 2000 by Thomas Eschenbacher
    email                : 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 "config.h"
#include <pthread.h>
#include <errno.h>
#include <error.h>
#include <string.h>  // for strerror()
#include <qglobal.h> // for qWarning()

#ifdef DEBUG
#include <execinfo.h> // for backtrace()
#endif

#include "mt/Mutex.h"

//***************************************************************************
Mutex::Mutex(const char *name)
    :m_name(name), m_locked_by(0)
{
    int ret = pthread_mutex_init(&m_mutex, 0);
    if (ret) qWarning("Mutex::Mutex(): mutex creation failed: %s",
	strerror(ret));
}

//***************************************************************************
Mutex::~Mutex()
{
    if (locked()) {
	qWarning("Mutex::~Mutex(): destroying locked mutex!");

#ifdef DEBUG
	qDebug("pthread_self()=%08X", (unsigned int)pthread_self());
	void *buf[256];
	size_t n = backtrace(buf, 256);
	backtrace_symbols_fd(buf, n, 2);
#endif

	unlock();
    }

    int ret = pthread_mutex_destroy(&m_mutex);
    if (ret) qWarning("Mutex::~Mutex(): mutex destruction failed: %s",
	strerror(ret));
}

//***************************************************************************
int Mutex::lock()
{
    if (pthread_self() == m_locked_by) {
	qWarning("Mutex::lock() RECURSIVE CALL => DANGER OF DEADLOCK !!!");
#ifdef DEBUG
	qDebug("pthread_self()=%08X", (unsigned int)pthread_self());
	void *buf[256];
	size_t n = backtrace(buf, 256);
	backtrace_symbols_fd(buf, n, 2);
#endif
	return -EBUSY;
    }

    int ret = pthread_mutex_lock(&m_mutex);
    if (ret) qWarning("Mutex::lock(): lock of mutex failed: %s",
	strerror(ret));
    m_locked_by = pthread_self();
    return ret;
}

//***************************************************************************
int Mutex::unlock()
{
    int ret = pthread_mutex_unlock(&m_mutex);
    if (ret) qWarning("Mutex::unlock(): unlock of mutex failed: %s",
	strerror(ret));
    m_locked_by = (pthread_t)-1;
    return ret;
}

//***************************************************************************
bool Mutex::locked()
{
    // try to lock the mutex
    int ret = pthread_mutex_trylock(&m_mutex);

    // if it failed, it is busy
    if (ret == EBUSY) return true;
    if (ret) qWarning("Mutex::locked(): getting mutex state failed: %s",
	strerror(ret));

    // if it succeeded, unlock the mutex, we didn't really want to
    // lock it !
    ret = pthread_mutex_unlock(&m_mutex);
    if (ret) qWarning("Mutex::locked(): unlock of mutex failed: %s",
	strerror(ret));

    return false;
}

//***************************************************************************
void Mutex::setName(const char *name)
{
    m_name = name;
}

//***************************************************************************
const char *Mutex::name()
{
    return m_name;
}

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