From 3cc4c2e24d41ea9ef34eaf6c6fd36a8428100aef Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 24 Feb 2015 09:22:08 -0500 Subject: [PATCH] fixes to get pid on FreeBSD --- src/zm_logger.cpp | 11 +++++++++++ src/zm_thread.h | 28 +++++++++++++++++++++++++--- src/zm_timer.h | 14 +++++++++++++- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/zm_logger.cpp b/src/zm_logger.cpp index c64f6e7c0..4b37580f7 100644 --- a/src/zm_logger.cpp +++ b/src/zm_logger.cpp @@ -31,6 +31,9 @@ #include #include #include +#ifdef __FreeBSD__ +#include +#endif bool Logger::smInitialised = false; Logger *Logger::smInstance = 0; @@ -527,9 +530,17 @@ void Logger::logPrint( bool hex, const char * const file, const int line, const #endif pid_t tid; +#ifdef __FreeBSD__ + long lwpid; + thr_self(&lwpid); + tid = lwpid; + + if (tid < 0 ) // Thread/Process id +#else #ifdef HAVE_SYSCALL if ( (tid = syscall(SYS_gettid)) < 0 ) // Thread/Process id #endif // HAVE_SYSCALL +#endif tid = getpid(); // Process id char *logPtr = logString; diff --git a/src/zm_thread.h b/src/zm_thread.h index 6a0d169f0..3ba80c09f 100644 --- a/src/zm_thread.h +++ b/src/zm_thread.h @@ -28,12 +28,26 @@ #endif // HAVE_SYS_SYSCALL_H #include "zm_exception.h" #include "zm_utils.h" +#ifdef __FreeBSD__ +#include +#endif class ThreadException : public Exception { +private: +pid_t pid() { +pid_t tid; +#ifdef __FreeBSD__ +long lwpid; +thr_self(&lwpid); +tid = lwpid; +#else +tid=syscall(SYS_gettid); +#endif +return tid; +} public: - ThreadException( const std::string &message ) : Exception( stringtf( "(%d) "+message, (long int)syscall(SYS_gettid) ) ) - { + ThreadException( const std::string &message ) : Exception( stringtf( "(%d) "+message, (long int)pid() ) ) { } }; @@ -205,7 +219,15 @@ protected: pid_t id() const { - return( (pid_t)syscall(SYS_gettid) ); +pid_t tid; +#ifdef __FreeBSD__ +long lwpid; +thr_self(&lwpid); +tid = lwpid; +#else +tid=syscall(SYS_gettid); +#endif +return tid; } void exit( int status = 0 ) { diff --git a/src/zm_timer.h b/src/zm_timer.h index 6c7663b87..80461cf07 100644 --- a/src/zm_timer.h +++ b/src/zm_timer.h @@ -32,8 +32,20 @@ class Timer private: class TimerException : public Exception { + private: + pid_t pid() { + pid_t tid; +#ifdef __FreeBSD__ + long lwpid; + thr_self(&lwpid); + tid = lwpid; +#else + tid=syscall(SYS_gettid); +#endif + return tid; + } public: - TimerException( const std::string &message ) : Exception( stringtf( "(%d) "+message, (long int)syscall(SYS_gettid) ) ) + TimerException( const std::string &message ) : Exception( stringtf( "(%d) "+message, (long int)pid() ) ) { } };