1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
* clock.c - Fast-lookup 1Hz clock
*/
/***********************************************************************
* Copyright © 2006 Rémi Denis-Courmont. *
* This program is free software; you can redistribute and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation; version 2 of the license, or (at *
* your option) any later version. *
* *
* This program 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 this program; if not, you can get it from: *
* http://www.gnu.org/copyleft/gpl.html *
***********************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <time.h>
#include <signal.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h> // DELAYTIMER_MAX
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h> // _POSIX_*
#include <pthread.h>
#include "clock.h"
#include "debug.h"
#ifdef HAVE_TIMER_CREATE
typedef struct clock_data_t
{
timer_t handle;
teredo_clock_t value;
bool active;
} clock_data_t;
static void clock_tick (union sigval val)
{
clock_data_t *context = (clock_data_t *)(val.sival_ptr);
int orun = timer_getoverrun (context->handle);
context->value += 1 + orun;
#ifdef DELAYTIMER_MAX
if (orun == DELAYTIMER_MAX)
/* We have a big problem, let next caller fix it */
context->active = false;
#endif
if (!context->active)
{
struct itimerspec it =
{
.it_value = { .tv_sec = 0, .tv_nsec = 0 },
};
timer_settime (context->handle, 0, &it, NULL);
}
context->active = false;
}
unsigned long teredo_clock (void)
{
static clock_data_t clk =
{
.value = 0,
.active = false
};
static struct
{
pthread_mutex_t lock;
clockid_t id;
bool present;
} priv = { PTHREAD_MUTEX_INITIALIZER, CLOCK_REALTIME, false };
teredo_clock_t value;
pthread_mutex_lock (&priv.lock);
if (!priv.present)
{
struct sigevent ev;
memset (&ev, 0, sizeof (ev));
ev.sigev_notify = SIGEV_THREAD;
ev.sigev_value.sival_ptr = &clk;
ev.sigev_notify_function = clock_tick;
#if (_POSIX_CLOCK_SELECTION - 0 >= 0) && (_POSIX_MONOTONIC_CLOCK - 0 >= 0)
/* Run-time POSIX monotonic clock detection */
struct timespec res;
if (clock_getres (CLOCK_MONOTONIC, &res) == 0)
priv.id = CLOCK_MONOTONIC;
#endif
if (timer_create (priv.id, &ev, &clk.handle) == 0)
priv.present = true;
}
if (!clk.active)
{
struct itimerspec it;
clock_gettime (priv.id, &it.it_value);
clk.value = it.it_value.tv_sec;
if (priv.present)
{
it.it_value.tv_sec++;
it.it_value.tv_nsec = 0;
it.it_interval.tv_sec = 1;
it.it_interval.tv_nsec = 0;
clk.active = true;
timer_settime (clk.handle, TIMER_ABSTIME, &it, NULL);
}
}
value = clk.value;
pthread_mutex_unlock (&priv.lock);
return value;
}
#else
unsigned long teredo_clock (void)
{
struct timespec ts;
clock_gettime (CLOCK_REALTIME, &ts);
return ts.tv_sec;
}
#endif
|