extracted clock_gettime in order to support MACH architecture
This commit is contained in:
@@ -11,7 +11,9 @@ libutils_a_SOURCES = log.cpp \
|
|||||||
tcpsocket.h \
|
tcpsocket.h \
|
||||||
thread.cpp \
|
thread.cpp \
|
||||||
thread.h \
|
thread.h \
|
||||||
wqueue.h \
|
clock.h \
|
||||||
|
clock.cpp \
|
||||||
|
queue.h \
|
||||||
notify.h
|
notify.h
|
||||||
|
|
||||||
distclean-local:
|
distclean-local:
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -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_
|
||||||
@@ -23,6 +23,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include "clock.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -120,7 +121,7 @@ void logWrite(const LogFacility facility, const LogLevel level, const char* mess
|
|||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
struct tm* tm;
|
struct tm* tm;
|
||||||
|
|
||||||
clock_gettime(CLOCK_REALTIME, &ts);
|
clockGettime(&ts);
|
||||||
tm = localtime(&ts.tv_sec);
|
tm = localtime(&ts.tv_sec);
|
||||||
|
|
||||||
char* buf;
|
char* buf;
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
#include <list>
|
#include <list>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include "clock.h"
|
||||||
|
|
||||||
/** \file queue.h */
|
/** \file queue.h */
|
||||||
|
|
||||||
@@ -88,7 +89,7 @@ public:
|
|||||||
pthread_mutex_lock(&m_mutex);
|
pthread_mutex_lock(&m_mutex);
|
||||||
if (timeout>0) {
|
if (timeout>0) {
|
||||||
struct timespec t;
|
struct timespec t;
|
||||||
clock_gettime(CLOCK_REALTIME, &t);
|
clockGettime(&t);
|
||||||
t.tv_sec += timeout;
|
t.tv_sec += timeout;
|
||||||
while (m_queue.empty()) {
|
while (m_queue.empty()) {
|
||||||
if (pthread_cond_timedwait(&m_cond, &m_mutex, &t)==ETIMEDOUT)
|
if (pthread_cond_timedwait(&m_cond, &m_mutex, &t)==ETIMEDOUT)
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
#include "clock.h"
|
||||||
|
|
||||||
void* Thread::runThread(void* arg)
|
void* Thread::runThread(void* arg)
|
||||||
{
|
{
|
||||||
@@ -46,7 +47,9 @@ bool Thread::start(const char* name)
|
|||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
|
|
||||||
#ifdef HAVE_PTHREAD_SETNAME_NP
|
#ifdef HAVE_PTHREAD_SETNAME_NP
|
||||||
|
#ifndef __MACH__
|
||||||
pthread_setname_np(m_threadid, name);
|
pthread_setname_np(m_threadid, name);
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
m_started = true;
|
m_started = true;
|
||||||
@@ -112,7 +115,7 @@ bool WaitThread::join()
|
|||||||
bool WaitThread::Wait(int seconds)
|
bool WaitThread::Wait(int seconds)
|
||||||
{
|
{
|
||||||
struct timespec t;
|
struct timespec t;
|
||||||
clock_gettime(CLOCK_REALTIME, &t);
|
clockGettime(&t);
|
||||||
t.tv_sec += seconds;
|
t.tv_sec += seconds;
|
||||||
pthread_mutex_lock(&m_mutex);
|
pthread_mutex_lock(&m_mutex);
|
||||||
pthread_cond_timedwait(&m_cond, &m_mutex, &t);
|
pthread_cond_timedwait(&m_cond, &m_mutex, &t);
|
||||||
|
|||||||
Reference in New Issue
Block a user