Implement a RecursiveMutex class which is an explicit Recursive Mutex. Change the db mutex to a recursive Mutex

This commit is contained in:
Isaac Connor 2018-06-01 11:27:35 -04:00
parent 405b1f92ed
commit 316dbb5eb8
4 changed files with 37 additions and 21 deletions

View File

@ -24,7 +24,7 @@
#include "zm_db.h"
MYSQL dbconn;
Mutex db_mutex;
RecursiveMutex db_mutex;
bool zmDbConnected = false;
@ -91,15 +91,15 @@ void zmDbClose() {
}
MYSQL_RES * zmDbFetch(const char * query) {
if ( ! zmDbConnected ) {
if ( !zmDbConnected ) {
Error("Not connected.");
return NULL;
}
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);

View File

@ -41,7 +41,7 @@ class zmDbRow {
};
extern MYSQL dbconn;
extern Mutex db_mutex;
extern RecursiveMutex db_mutex;
bool zmDbConnect();
void zmDbClose();

View File

@ -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) ) );

View File

@ -59,27 +59,34 @@ public:
};
class Mutex {
friend class Condition;
friend class Condition;
private:
pthread_mutex_t mMutex;
private:
pthread_mutex_t mMutex;
public:
Mutex();
~Mutex();
public:
Mutex();
~Mutex();
private:
pthread_mutex_t *getMutex() {
return( &mMutex );
}
private:
pthread_mutex_t *getMutex() {
return &mMutex;
}
public:
int trylock();
void lock();
void lock( int secs );
void lock( double secs );
void unlock();
bool locked();
public:
int trylock();
void lock();
void lock( int secs );
void lock( double secs );
void unlock();
bool locked();
};
class RecursiveMutex : public Mutex {
private:
pthread_mutex_t mMutex;
public:
RecursiveMutex();
};
class ScopedMutex {