소스 검색

Make the timer code work on MacOS as well.

master
Richard Nyberg 15 년 전
부모
커밋
ef401997d3
4개의 변경된 파일56개의 추가작업 그리고 12개의 파일을 삭제
  1. +2
    -3
      btpd/main.c
  2. +20
    -3
      configure.ac
  3. +1
    -0
      evloop/evloop.h
  4. +33
    -6
      evloop/timer.c

+ 2
- 3
btpd/main.c 파일 보기

@@ -35,9 +35,8 @@ setup_daemon(int daemonize, const char *dir)
if (snprintf(NULL, 0, "btpd") != 4)
btpd_err("snprintf doesn't work.\n");

if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
btpd_err("clock_gettime(CLOCK_MONOTONIC, ...) failed (%s).\n",
strerror(errno));
if (evtimer_gettime(&ts) != 0)
btpd_err("evtimer_gettime failed (%s).\n", strerror(errno));

if (dir == NULL) {
if ((dir = find_btpd_dir()) == NULL)


+ 20
- 3
configure.ac 파일 보기

@@ -41,15 +41,32 @@ AC_ARG_WITH(evloop-method,

old_LIBS=$LIBS
LIBS=""
AC_SEARCH_LIBS(clock_gettime, rt,,AC_MSG_FAILURE(btpd needs clock_gettime))
AC_SUBST(CLOCKLIB,$LIBS)
LIBS=""
AC_SEARCH_LIBS(inet_ntop, nsl,,AC_MSG_FAILURE(btpd needs inet_ntop))
AC_SEARCH_LIBS(bind, socket,,AC_MSG_FAILURE(btpd needs bind))
AC_SUBST(INETLIBS,$LIBS)
LIBS=$old_LIBS
AC_CHECK_FUNCS(asprintf)

AC_MSG_CHECKING(for CLOCK_MONOTONIC)
AC_COMPILE_IFELSE([
#include <sys/time.h>
#include <time.h>
int main(void) { return clock_gettime(CLOCK_MONOTONIC, (void *)0); }
], clock_gettime=yes, clock_gettime=no)
AC_MSG_RESULT($clock_gettime)
if test $clock_gettime == yes; then
old_LIBS=$LIBS
LIBS=""
AC_SEARCH_LIBS(clock_gettime,rt,clock_gettime=yes,clock_gettime=no)
AC_SUBST(CLOCKLIB,$LIBS)
LIBS=$old_LIBS
AC_DEFINE(HAVE_CLOCK_MONOTONIC)
fi
if test $clock_gettime == no; then
AC_CHECK_FUNCS(mach_absolute_time,,
AC_MSG_FAILURE(no supported time mechanism found))
fi

AC_MSG_CHECKING(whether compiler accepts -Wno-pointer-sign)
CC_ARGS_OK_IFELSE(-Wno-pointer-sign,
AC_SUBST(WARNNPS,"-Wno-pointer-sign")


+ 1
- 0
evloop/evloop.h 파일 보기

@@ -53,5 +53,6 @@ void evtimer_del(struct timeout *);

void evtimers_run(void);
struct timespec evtimer_delay(void);
int evtimer_gettime(struct timespec *);

#endif

+ 33
- 6
evloop/timer.c 파일 보기

@@ -3,12 +3,39 @@
#include "evloop.h"
#include "timeheap.h"

#if defined(CLOCK_MONOTONIC_FAST)
#if defined(HAVE_CLOCK_MONOTONIC)

#ifdef CLOCK_MONOTONIC_FAST
#define TIMER_CLOCK CLOCK_MONOTONIC_FAST
#elif defined(CLOCK_MONOTONIC)
#else
#define TIMER_CLOCK CLOCK_MONOTONIC
#endif

int
evtimer_gettime(struct timespec *ts)
{
return clock_gettime(TIMER_CLOCK, ts);
}

#elif defined(HAVE_MACH_ABSOLUTE_TIME)

#include <mach/mach_time.h>

int
evtimer_gettime(struct timespec *ts)
{
uint64_t nsecs;
static mach_timebase_info_data_t nsratio = { 0, 0 };
if (nsratio.denom == 0)
mach_timebase_info(&nsratio);
nsecs = mach_absolute_time() * nsratio.numer / nsratio.denom;
ts->tv_sec = nsecs / 1000000000;
ts->tv_nsec = nsecs - ts->tv_sec * 1000000000;
return 0;
}

#else
#error CLOCK_MONOTONIC needed!
#error No supported time mechanism
#endif

static struct timespec
@@ -50,7 +77,7 @@ int
evtimer_add(struct timeout *h, struct timespec *t)
{
struct timespec now, sum;
clock_gettime(TIMER_CLOCK, &now);
evtimer_gettime(&now);
sum = addtime(now, *t);
if (h->th.i == -1)
return timeheap_insert(&h->th, &sum);
@@ -73,7 +100,7 @@ void
evtimers_run(void)
{
struct timespec now;
clock_gettime(TIMER_CLOCK, &now);
evtimer_gettime(&now);
while (timeheap_size() > 0) {
struct timespec diff = subtime(timeheap_top(), now);
if (diff.tv_sec < 0) {
@@ -93,7 +120,7 @@ evtimer_delay(void)
diff.tv_sec = -1;
diff.tv_nsec = 0;
} else {
clock_gettime(TIMER_CLOCK, &now);
evtimer_gettime(&now);
diff = subtime(timeheap_top(), now);
if (diff.tv_sec < 0) {
diff.tv_sec = 0;


불러오는 중...
취소
저장