Merge pull request #3142 from Carbenium/log-db-lock

Logger: Try harder to obtain the DB lock
pull/3147/head
Isaac Connor 2021-02-08 15:30:22 -05:00 committed by GitHub
commit a7da2e9b8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 12 deletions

View File

@ -534,7 +534,7 @@ void Logger::logPrint(bool hex, const char * const filepath, const int line, con
} // end if level <= mFileLevel
if ( level <= mDatabaseLevel ) {
if ( !db_mutex.trylock() ) {
if (db_mutex.try_lock_for(1)) {
char escapedString[(strlen(syslogStart)*2)+1];
mysql_real_escape_string(&dbconn, escapedString, syslogStart, strlen(syslogStart));
@ -556,7 +556,7 @@ void Logger::logPrint(bool hex, const char * const filepath, const int line, con
} else {
Level tempDatabaseLevel = mDatabaseLevel;
databaseLevel(NOLOG);
Error("Can't insert log entry: sql(%s) error(%s)", syslogStart, mysql_error(&dbconn));
Error("Can't insert log entry since the DB lock could not be obtained. Message: %s", syslogStart);
databaseLevel(tempDatabaseLevel);
}
} // end if level <= mDatabaseLevel

View File

@ -59,7 +59,7 @@ Mutex::~Mutex() {
Error("Unable to destroy pthread mutex: %s", strerror(errno));
}
int Mutex::trylock() {
int Mutex::try_lock() {
return pthread_mutex_trylock(&mMutex);
}
void Mutex::lock() {
@ -68,16 +68,14 @@ void Mutex::lock() {
//Debug(3, "Lock");
}
void Mutex::lock( int secs ) {
bool Mutex::try_lock_for(int secs) {
struct timespec timeout = getTimeout(secs);
if ( pthread_mutex_timedlock(&mMutex, &timeout) < 0 )
throw ThreadException(stringtf("Unable to timedlock pthread mutex: %s", strerror(errno)));
return pthread_mutex_timedlock(&mMutex, &timeout) == 0;
}
void Mutex::lock( double secs ) {
bool Mutex::try_lock_for(double secs) {
struct timespec timeout = getTimeout(secs);
if ( pthread_mutex_timedlock(&mMutex, &timeout) < 0 )
throw ThreadException(stringtf("Unable to timedlock pthread mutex: %s", strerror(errno)));
return pthread_mutex_timedlock(&mMutex, &timeout) == 0;
}
void Mutex::unlock() {

View File

@ -78,10 +78,10 @@ class Mutex {
}
public:
int trylock();
int try_lock();
void lock();
void lock( int secs );
void lock( double secs );
bool try_lock_for(int secs);
bool try_lock_for(double secs);
void unlock();
bool locked();
};