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"
|
#include "zm_db.h"
|
||||||
|
|
||||||
MYSQL dbconn;
|
MYSQL dbconn;
|
||||||
Mutex db_mutex;
|
RecursiveMutex db_mutex;
|
||||||
|
|
||||||
bool zmDbConnected = false;
|
bool zmDbConnected = false;
|
||||||
|
|
||||||
|
@ -91,15 +91,15 @@ void zmDbClose() {
|
||||||
}
|
}
|
||||||
|
|
||||||
MYSQL_RES * zmDbFetch(const char * query) {
|
MYSQL_RES * zmDbFetch(const char * query) {
|
||||||
if ( ! zmDbConnected ) {
|
if ( !zmDbConnected ) {
|
||||||
Error("Not connected.");
|
Error("Not connected.");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
db_mutex.lock();
|
db_mutex.lock();
|
||||||
|
|
||||||
if ( mysql_query(&dbconn, query) ) {
|
if ( mysql_query(&dbconn, query) ) {
|
||||||
Error("Can't run query: %s", mysql_error(&dbconn));
|
|
||||||
db_mutex.unlock();
|
db_mutex.unlock();
|
||||||
|
Error("Can't run query: %s", mysql_error(&dbconn));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
Debug(4, "Success running query: %s", query);
|
Debug(4, "Success running query: %s", query);
|
||||||
|
|
|
@ -41,7 +41,7 @@ class zmDbRow {
|
||||||
};
|
};
|
||||||
|
|
||||||
extern MYSQL dbconn;
|
extern MYSQL dbconn;
|
||||||
extern Mutex db_mutex;
|
extern RecursiveMutex db_mutex;
|
||||||
|
|
||||||
bool zmDbConnect();
|
bool zmDbConnect();
|
||||||
void zmDbClose();
|
void zmDbClose();
|
||||||
|
|
|
@ -95,6 +95,15 @@ bool Mutex::locked() {
|
||||||
return( state == EBUSY );
|
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 ) {
|
Condition::Condition( Mutex &mutex ) : mMutex( mutex ) {
|
||||||
if ( pthread_cond_init( &mCondition, NULL ) < 0 )
|
if ( pthread_cond_init( &mCondition, NULL ) < 0 )
|
||||||
throw ThreadException( stringtf( "Unable to create pthread condition: %s", strerror(errno) ) );
|
throw ThreadException( stringtf( "Unable to create pthread condition: %s", strerror(errno) ) );
|
||||||
|
|
|
@ -59,27 +59,34 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
class Mutex {
|
class Mutex {
|
||||||
friend class Condition;
|
friend class Condition;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
pthread_mutex_t mMutex;
|
pthread_mutex_t mMutex;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Mutex();
|
Mutex();
|
||||||
~Mutex();
|
~Mutex();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
pthread_mutex_t *getMutex() {
|
pthread_mutex_t *getMutex() {
|
||||||
return( &mMutex );
|
return &mMutex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
int trylock();
|
int trylock();
|
||||||
void lock();
|
void lock();
|
||||||
void lock( int secs );
|
void lock( int secs );
|
||||||
void lock( double secs );
|
void lock( double secs );
|
||||||
void unlock();
|
void unlock();
|
||||||
bool locked();
|
bool locked();
|
||||||
|
};
|
||||||
|
|
||||||
|
class RecursiveMutex : public Mutex {
|
||||||
|
private:
|
||||||
|
pthread_mutex_t mMutex;
|
||||||
|
public:
|
||||||
|
RecursiveMutex();
|
||||||
};
|
};
|
||||||
|
|
||||||
class ScopedMutex {
|
class ScopedMutex {
|
||||||
|
|
Loading…
Reference in New Issue