diff options
author | Mike Buland <mike@xagasoft.com> | 2024-11-06 16:01:36 -0800 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2024-11-06 16:01:36 -0800 |
commit | 700d4bbcbf59c4447becbab21a6aa7204a8da2f4 (patch) | |
tree | ae68c40a5af2d5bc84ac4bbb192fbbda893d64cc /src/experimental/debugmutex.cpp | |
parent | 6403224b6fe50dfc28d3c25725b6d0910b7eb6c3 (diff) | |
download | libbu++-700d4bbcbf59c4447becbab21a6aa7204a8da2f4.tar.gz libbu++-700d4bbcbf59c4447becbab21a6aa7204a8da2f4.tar.bz2 libbu++-700d4bbcbf59c4447becbab21a6aa7204a8da2f4.tar.xz libbu++-700d4bbcbf59c4447becbab21a6aa7204a8da2f4.zip |
I believe MyriadFs is now threadsafe.myriad
It could probably be more optimized, but it does work.
Diffstat (limited to 'src/experimental/debugmutex.cpp')
-rw-r--r-- | src/experimental/debugmutex.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/experimental/debugmutex.cpp b/src/experimental/debugmutex.cpp new file mode 100644 index 0000000..2b61ae2 --- /dev/null +++ b/src/experimental/debugmutex.cpp | |||
@@ -0,0 +1,78 @@ | |||
1 | #include "bu/debugmutex.h" | ||
2 | |||
3 | #include "bu/exceptionbase.h" | ||
4 | |||
5 | Bu::DebugMutex::DebugMutex() | ||
6 | { | ||
7 | } | ||
8 | |||
9 | Bu::DebugMutex::~DebugMutex() | ||
10 | { | ||
11 | } | ||
12 | |||
13 | int Bu::DebugMutex::lock() | ||
14 | { | ||
15 | pthread_t self = pthread_self(); | ||
16 | mState.lock(); | ||
17 | bool bFound = false; | ||
18 | for( ThreadList::iterator i = lThreads.begin(); i; i++ ) | ||
19 | { | ||
20 | if( (*i) == self ) | ||
21 | { | ||
22 | bFound = true; | ||
23 | if( (*i).bLocked == true ) | ||
24 | { | ||
25 | throw Bu::ExceptionBase( Bu::String("Double lock in thread: %1").arg( (*i).sName ).end().getStr() ); | ||
26 | |||
27 | } | ||
28 | else | ||
29 | { | ||
30 | (*i).bLocked = true; | ||
31 | } | ||
32 | break; | ||
33 | } | ||
34 | } | ||
35 | if( bFound == false ) | ||
36 | { | ||
37 | lThreads.append( ThreadInfo( true ) ); | ||
38 | } | ||
39 | mState.unlock(); | ||
40 | return Bu::Mutex::lock(); | ||
41 | } | ||
42 | |||
43 | int Bu::DebugMutex::unlock() | ||
44 | { | ||
45 | pthread_t self = pthread_self(); | ||
46 | mState.lock(); | ||
47 | bool bFound = false; | ||
48 | for( ThreadList::iterator i = lThreads.begin(); i; i++ ) | ||
49 | { | ||
50 | if( (*i) == self ) | ||
51 | { | ||
52 | bFound = true; | ||
53 | if( (*i).bLocked == false ) | ||
54 | { | ||
55 | throw Bu::ExceptionBase( Bu::String("Unlock in thread that did not lock: %1").arg( (*i).sName ).end().getStr() ); | ||
56 | |||
57 | } | ||
58 | else | ||
59 | { | ||
60 | (*i).bLocked = false; | ||
61 | } | ||
62 | break; | ||
63 | } | ||
64 | } | ||
65 | if( bFound == false ) | ||
66 | { | ||
67 | ThreadInfo info( false ); | ||
68 | throw Bu::ExceptionBase( Bu::String("Unlock in thread that never locked mutex: %1").arg( info.sName ).end().getStr() ); | ||
69 | } | ||
70 | mState.unlock(); | ||
71 | return Bu::Mutex::unlock(); | ||
72 | } | ||
73 | |||
74 | int Bu::DebugMutex::trylock() | ||
75 | { | ||
76 | return Bu::Mutex::trylock(); | ||
77 | } | ||
78 | |||