aboutsummaryrefslogtreecommitdiff
path: root/src/experimental/debugmutex.cpp
blob: 2b61ae2b497d749789c533869122d870c5bae7b8 (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
#include "bu/debugmutex.h"

#include "bu/exceptionbase.h"

Bu::DebugMutex::DebugMutex()
{
}

Bu::DebugMutex::~DebugMutex()
{
}

int Bu::DebugMutex::lock()
{
    pthread_t self = pthread_self();
    mState.lock();
    bool bFound = false;
    for( ThreadList::iterator i = lThreads.begin(); i; i++ )
    {
        if( (*i) == self )
        {
            bFound = true;
            if( (*i).bLocked == true )
            {
                throw Bu::ExceptionBase( Bu::String("Double lock in thread: %1").arg( (*i).sName ).end().getStr() );

            }
            else
            {
                (*i).bLocked = true;
            }
            break;
        }
    }
    if( bFound == false )
    {
        lThreads.append( ThreadInfo( true ) );
    }
    mState.unlock();
    return Bu::Mutex::lock();
}

int Bu::DebugMutex::unlock()
{
    pthread_t self = pthread_self();
    mState.lock();
    bool bFound = false;
    for( ThreadList::iterator i = lThreads.begin(); i; i++ )
    {
        if( (*i) == self )
        {
            bFound = true;
            if( (*i).bLocked == false )
            {
                throw Bu::ExceptionBase( Bu::String("Unlock in thread that did not lock: %1").arg( (*i).sName ).end().getStr() );

            }
            else
            {
                (*i).bLocked = false;
            }
            break;
        }
    }
    if( bFound == false )
    {
        ThreadInfo info( false );
        throw Bu::ExceptionBase( Bu::String("Unlock in thread that never locked mutex: %1").arg( info.sName ).end().getStr() );
    }
    mState.unlock();
    return Bu::Mutex::unlock();
}

int Bu::DebugMutex::trylock()
{
    return Bu::Mutex::trylock();
}