extracted clock_gettime in order to support MACH architecture

This commit is contained in:
john30
2015-11-07 11:56:39 +01:00
parent b4f89ab5dc
commit 0045d1cec4
6 changed files with 89 additions and 4 deletions
+3 -1
View File
@@ -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:
+45
View File
@@ -0,0 +1,45 @@
/*
* Copyright (C) John Baier 2015 <ebusd@ebusd.eu>
*
* 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 <mach/clock.h>
# include <mach/mach.h>
#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
}
+33
View File
@@ -0,0 +1,33 @@
/*
* Copyright (C) John Baier 2015 <ebusd@ebusd.eu>
*
* 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 <time.h>
/** \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_
+2 -1
View File
@@ -23,6 +23,7 @@
#include <time.h>
#include <sys/time.h>
#include <stdarg.h>
#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;
+2 -1
View File
@@ -23,6 +23,7 @@
#include <list>
#include <pthread.h>
#include <errno.h>
#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)
+4 -1
View File
@@ -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);