Implement a RecursiveMutex class which is an explicit Recursive Mutex. Change the db mutex to a recursive Mutex
This commit is contained in:
parent
405b1f92ed
commit
316dbb5eb8
|
@ -24,7 +24,7 @@
|
|||
#include "zm_db.h"
|
||||
|
||||
MYSQL dbconn;
|
||||
Mutex db_mutex;
|
||||
RecursiveMutex db_mutex;
|
||||
|
||||
bool zmDbConnected = false;
|
||||
|
||||
|
@ -98,8 +98,8 @@ MYSQL_RES * zmDbFetch(const char * query) {
|
|||
db_mutex.lock();
|
||||
|
||||
if ( mysql_query(&dbconn, query) ) {
|
||||
Error("Can't run query: %s", mysql_error(&dbconn));
|
||||
db_mutex.unlock();
|
||||
Error("Can't run query: %s", mysql_error(&dbconn));
|
||||
return NULL;
|
||||
}
|
||||
Debug(4, "Success running query: %s", query);
|
||||
|
|
|
@ -41,7 +41,7 @@ class zmDbRow {
|
|||
};
|
||||
|
||||
extern MYSQL dbconn;
|
||||
extern Mutex db_mutex;
|
||||
extern RecursiveMutex db_mutex;
|
||||
|
||||
bool zmDbConnect();
|
||||
void zmDbClose();
|
||||
|
|
|
@ -95,6 +95,15 @@ bool Mutex::locked() {
|
|||
return( state == EBUSY );
|
||||
}
|
||||
|
||||
RecursiveMutex::RecursiveMutex() {
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
|
||||
if ( pthread_mutex_init(&mMutex, &attr) < 0 )
|
||||
Error("Unable to create pthread mutex: %s", strerror(errno));
|
||||
}
|
||||
|
||||
Condition::Condition( Mutex &mutex ) : mMutex( mutex ) {
|
||||
if ( pthread_cond_init( &mCondition, NULL ) < 0 )
|
||||
throw ThreadException( stringtf( "Unable to create pthread condition: %s", strerror(errno) ) );
|
||||
|
|
|
@ -70,7 +70,7 @@ public:
|
|||
|
||||
private:
|
||||
pthread_mutex_t *getMutex() {
|
||||
return( &mMutex );
|
||||
return &mMutex;
|
||||
}
|
||||
|
||||
public:
|
||||
|
@ -82,6 +82,13 @@ public:
|
|||
bool locked();
|
||||
};
|
||||
|
||||
class RecursiveMutex : public Mutex {
|
||||
private:
|
||||
pthread_mutex_t mMutex;
|
||||
public:
|
||||
RecursiveMutex();
|
||||
};
|
||||
|
||||
class ScopedMutex {
|
||||
private:
|
||||
Mutex &mMutex;
|
||||
|
|
Loading…
Reference in New Issue