Merge pull request #2111 from connortechnology/recursive_mutex
Implement a RecursiveMutex class which is an explicit Recursive Mutex…pull/2114/head
commit
b071aaad7b
|
@ -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;
|
||||||
|
|
||||||
|
@ -98,8 +98,8 @@ MYSQL_RES * zmDbFetch(const char * query) {
|
||||||
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) ) );
|
||||||
|
|
|
@ -70,7 +70,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
pthread_mutex_t *getMutex() {
|
pthread_mutex_t *getMutex() {
|
||||||
return( &mMutex );
|
return &mMutex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -82,6 +82,13 @@ public:
|
||||||
bool locked();
|
bool locked();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class RecursiveMutex : public Mutex {
|
||||||
|
private:
|
||||||
|
pthread_mutex_t mMutex;
|
||||||
|
public:
|
||||||
|
RecursiveMutex();
|
||||||
|
};
|
||||||
|
|
||||||
class ScopedMutex {
|
class ScopedMutex {
|
||||||
private:
|
private:
|
||||||
Mutex &mMutex;
|
Mutex &mMutex;
|
||||||
|
|
Loading…
Reference in New Issue