summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'libglusterfs/src/timer.c')
-rw-r--r--libglusterfs/src/timer.c76
1 files changed, 32 insertions, 44 deletions
diff --git a/libglusterfs/src/timer.c b/libglusterfs/src/timer.c
index b926644ff..a059cc212 100644
--- a/libglusterfs/src/timer.c
+++ b/libglusterfs/src/timer.c
@@ -1,20 +1,11 @@
/*
- Copyright (c) 2007-2010 Gluster, Inc. <http://www.gluster.com>
- This file is part of GlusterFS.
-
- GlusterFS is free software; you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as published
- by the Free Software Foundation; either version 3 of the License,
- or (at your option) any later version.
-
- GlusterFS 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
- Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see
- <http://www.gnu.org/licenses/>.
+ Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
+ This file is part of GlusterFS.
+
+ This file is licensed to you under your choice of the GNU Lesser
+ General Public License, version 3 or any later version (LGPLv3 or
+ later), or the GNU General Public License, version 2 (GPLv2), in all
+ cases as published by the Free Software Foundation.
*/
#ifndef _CONFIG_H
@@ -26,42 +17,38 @@
#include "logging.h"
#include "common-utils.h"
#include "globals.h"
-
-#define TS(tv) ((((unsigned long long) tv.tv_sec) * 1000000) + (tv.tv_usec))
+#include "timespec.h"
gf_timer_t *
gf_timer_call_after (glusterfs_ctx_t *ctx,
- struct timeval delta,
+ struct timespec delta,
gf_timer_cbk_t callbk,
void *data)
{
gf_timer_registry_t *reg = NULL;
gf_timer_t *event = NULL;
gf_timer_t *trav = NULL;
- unsigned long long at = 0L;
-
+ uint64_t at = 0;
+
if (ctx == NULL)
{
- gf_log ("timer", GF_LOG_ERROR, "invalid argument");
+ gf_log_callingfn ("timer", GF_LOG_ERROR, "invalid argument");
return NULL;
}
reg = gf_timer_registry_init (ctx);
if (!reg) {
- gf_log ("timer", GF_LOG_ERROR, "!reg");
+ gf_log_callingfn ("timer", GF_LOG_ERROR, "!reg");
return NULL;
}
event = GF_CALLOC (1, sizeof (*event), gf_common_mt_gf_timer_t);
if (!event) {
- gf_log ("timer", GF_LOG_CRITICAL, "Not enough memory");
return NULL;
}
- gettimeofday (&event->at, NULL);
- event->at.tv_usec = ((event->at.tv_usec + delta.tv_usec) % 1000000);
- event->at.tv_sec += ((event->at.tv_usec + delta.tv_usec) / 1000000);
- event->at.tv_sec += delta.tv_sec;
+ timespec_now (&event->at);
+ timespec_adjust_delta (&event->at, delta);
at = TS (event->at);
event->callbk = callbk;
event->data = data;
@@ -89,10 +76,10 @@ gf_timer_call_stale (gf_timer_registry_t *reg,
{
if (reg == NULL || event == NULL)
{
- gf_log ("timer", GF_LOG_ERROR, "invalid argument");
+ gf_log_callingfn ("timer", GF_LOG_ERROR, "invalid argument");
return 0;
}
-
+
event->next->prev = event->prev;
event->prev->next = event->next;
event->next = &reg->stale;
@@ -108,13 +95,13 @@ gf_timer_call_cancel (glusterfs_ctx_t *ctx,
gf_timer_t *event)
{
gf_timer_registry_t *reg = NULL;
-
+
if (ctx == NULL || event == NULL)
{
- gf_log ("timer", GF_LOG_ERROR, "invalid argument");
+ gf_log_callingfn ("timer", GF_LOG_ERROR, "invalid argument");
return 0;
}
-
+
reg = gf_timer_registry_init (ctx);
if (!reg) {
gf_log ("timer", GF_LOG_ERROR, "!reg");
@@ -137,13 +124,14 @@ void *
gf_timer_proc (void *ctx)
{
gf_timer_registry_t *reg = NULL;
-
+ const struct timespec sleepts = {.tv_sec = 1, .tv_nsec = 0, };
+
if (ctx == NULL)
{
- gf_log ("timer", GF_LOG_ERROR, "invalid argument");
+ gf_log_callingfn ("timer", GF_LOG_ERROR, "invalid argument");
return NULL;
}
-
+
reg = gf_timer_registry_init (ctx);
if (!reg) {
gf_log ("timer", GF_LOG_ERROR, "!reg");
@@ -151,14 +139,14 @@ gf_timer_proc (void *ctx)
}
while (!reg->fin) {
- unsigned long long now;
- struct timeval now_tv;
+ uint64_t now;
+ struct timespec now_ts;
gf_timer_t *event = NULL;
- gettimeofday (&now_tv, NULL);
- now = TS (now_tv);
+ timespec_now (&now_ts);
+ now = TS (now_ts);
while (1) {
- unsigned long long at;
+ uint64_t at;
char need_cbk = 0;
pthread_mutex_lock (&reg->lock);
@@ -179,7 +167,7 @@ gf_timer_proc (void *ctx)
else
break;
}
- usleep (1000000);
+ nanosleep (&sleepts, NULL);
}
pthread_mutex_lock (&reg->lock);
@@ -203,7 +191,7 @@ gf_timer_registry_t *
gf_timer_registry_init (glusterfs_ctx_t *ctx)
{
if (ctx == NULL) {
- gf_log ("timer", GF_LOG_ERROR, "invalid argument");
+ gf_log_callingfn ("timer", GF_LOG_ERROR, "invalid argument");
return NULL;
}
@@ -222,7 +210,7 @@ gf_timer_registry_init (glusterfs_ctx_t *ctx)
reg->stale.prev = &reg->stale;
ctx->timer = reg;
- pthread_create (&reg->th, NULL, gf_timer_proc, ctx);
+ gf_thread_create (&reg->th, NULL, gf_timer_proc, ctx);
}
out:
return ctx->timer;