diff --git a/src/lib/utils/Makefile.am b/src/lib/utils/Makefile.am index 6edc3ed4..5f880a01 100644 --- a/src/lib/utils/Makefile.am +++ b/src/lib/utils/Makefile.am @@ -11,7 +11,9 @@ libutils_a_SOURCES = log.cpp \ tcpsocket.h \ thread.cpp \ thread.h \ - wqueue.h \ + clock.h \ + clock.cpp \ + queue.h \ notify.h distclean-local: diff --git a/src/lib/utils/clock.cpp b/src/lib/utils/clock.cpp new file mode 100644 index 00000000..a8474c19 --- /dev/null +++ b/src/lib/utils/clock.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (C) John Baier 2015 + * + * This file is part of ebusd. + * + * ebusd is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ebusd is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ebusd. If not, see http://www.gnu.org/licenses/. + */ + +#include "clock.h" +#ifdef __MACH__ +# include +# include +#endif + +#ifdef __MACH__ +static bool clockInitialized = false; +static clock_serv_t clockServ; +#endif + +void clockGettime(struct timespec* t) +{ +#ifdef __MACH__ + if (!clockInitialized) { + clockInitialized = true; + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &clockServ); + } + mach_timespec_t mts; + clock_get_time(clockServ, &mts); + t->tv_sec = mts.tv_sec; + t->tv_nsec = mts.tv_nsec; +#else + clock_gettime(CLOCK_REALTIME, t); +#endif +} diff --git a/src/lib/utils/clock.h b/src/lib/utils/clock.h new file mode 100644 index 00000000..c3d9c774 --- /dev/null +++ b/src/lib/utils/clock.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) John Baier 2015 + * + * This file is part of ebusd. + * + * ebusd is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ebusd is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ebusd. If not, see http://www.gnu.org/licenses/. + */ + +#ifndef LIBUTILS_CLOCK_H_ +#define LIBUTILS_CLOCK_H_ + +#include + +/** \file clock.h */ + +/** + * Get the real time system clock. + * @param t the @a timespec in which to store the time. + */ +void clockGettime(struct timespec* t); + +#endif // LIBUTILS_CLOCK_H_ diff --git a/src/lib/utils/log.cpp b/src/lib/utils/log.cpp index aebb7445..0843012e 100644 --- a/src/lib/utils/log.cpp +++ b/src/lib/utils/log.cpp @@ -23,6 +23,7 @@ #include #include #include +#include "clock.h" using namespace std; @@ -120,7 +121,7 @@ void logWrite(const LogFacility facility, const LogLevel level, const char* mess struct timespec ts; struct tm* tm; - clock_gettime(CLOCK_REALTIME, &ts); + clockGettime(&ts); tm = localtime(&ts.tv_sec); char* buf; diff --git a/src/lib/utils/queue.h b/src/lib/utils/queue.h index 2c4d5004..30e9715f 100644 --- a/src/lib/utils/queue.h +++ b/src/lib/utils/queue.h @@ -23,6 +23,7 @@ #include #include #include +#include "clock.h" /** \file queue.h */ @@ -88,7 +89,7 @@ public: pthread_mutex_lock(&m_mutex); if (timeout>0) { struct timespec t; - clock_gettime(CLOCK_REALTIME, &t); + clockGettime(&t); t.tv_sec += timeout; while (m_queue.empty()) { if (pthread_cond_timedwait(&m_cond, &m_mutex, &t)==ETIMEDOUT) diff --git a/src/lib/utils/thread.cpp b/src/lib/utils/thread.cpp index 1e20e68f..68918fca 100644 --- a/src/lib/utils/thread.cpp +++ b/src/lib/utils/thread.cpp @@ -23,6 +23,7 @@ #endif #include "thread.h" +#include "clock.h" void* Thread::runThread(void* arg) { @@ -46,7 +47,9 @@ bool Thread::start(const char* name) if (result == 0) { #ifdef HAVE_PTHREAD_SETNAME_NP +#ifndef __MACH__ pthread_setname_np(m_threadid, name); +#endif #endif m_started = true; @@ -112,7 +115,7 @@ bool WaitThread::join() bool WaitThread::Wait(int seconds) { struct timespec t; - clock_gettime(CLOCK_REALTIME, &t); + clockGettime(&t); t.tv_sec += seconds; pthread_mutex_lock(&m_mutex); pthread_cond_timedwait(&m_cond, &m_mutex, &t);