diff options
Diffstat (limited to 'libglusterfs/src/timer.c')
| -rw-r--r-- | libglusterfs/src/timer.c | 107 |
1 files changed, 52 insertions, 55 deletions
diff --git a/libglusterfs/src/timer.c b/libglusterfs/src/timer.c index a84fa8cdf..a059cc212 100644 --- a/libglusterfs/src/timer.c +++ b/libglusterfs/src/timer.c @@ -1,20 +1,11 @@ /* - Copyright (c) 2007-2009 Z RESEARCH, Inc. <http://www.zresearch.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 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 - General Public License for more details. - - You should have received a copy of the GNU 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 @@ -25,45 +16,43 @@ #include "timer.h" #include "logging.h" #include "common-utils.h" - -#define TS(tv) ((((unsigned long long) tv.tv_sec) * 1000000) + (tv.tv_usec)) +#include "globals.h" +#include "timespec.h" gf_timer_t * gf_timer_call_after (glusterfs_ctx_t *ctx, - struct timeval delta, - gf_timer_cbk_t cbk, + 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 = CALLOC (1, sizeof (*event)); + 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->cbk = cbk; + event->callbk = callbk; event->data = data; + event->xl = THIS; pthread_mutex_lock (®->lock); { trav = reg->active.prev; @@ -87,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 = ®->stale; @@ -106,16 +95,17 @@ 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"); + GF_FREE (event); return 0; } @@ -126,7 +116,7 @@ gf_timer_call_cancel (glusterfs_ctx_t *ctx, } pthread_mutex_unlock (®->lock); - FREE (event); + GF_FREE (event); return 0; } @@ -134,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"); @@ -148,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 (®->lock); @@ -168,13 +159,15 @@ gf_timer_proc (void *ctx) } } pthread_mutex_unlock (®->lock); + if (event->xl) + THIS = event->xl; if (need_cbk) - event->cbk (event->data); + event->callbk (event->data); else break; } - usleep (1000000); + nanosleep (&sleepts, NULL); } pthread_mutex_lock (®->lock); @@ -189,7 +182,7 @@ gf_timer_proc (void *ctx) } pthread_mutex_unlock (®->lock); pthread_mutex_destroy (®->lock); - FREE (((glusterfs_ctx_t *)ctx)->timer); + GF_FREE (((glusterfs_ctx_t *)ctx)->timer); return NULL; } @@ -197,24 +190,28 @@ gf_timer_proc (void *ctx) gf_timer_registry_t * gf_timer_registry_init (glusterfs_ctx_t *ctx) { - if (ctx == NULL) - { - gf_log ("timer", GF_LOG_ERROR, "invalid argument"); + if (ctx == NULL) { + gf_log_callingfn ("timer", GF_LOG_ERROR, "invalid argument"); return NULL; } - + if (!ctx->timer) { gf_timer_registry_t *reg = NULL; - ctx->timer = reg = CALLOC (1, sizeof (*reg)); - ERR_ABORT (reg); + reg = GF_CALLOC (1, sizeof (*reg), + gf_common_mt_gf_timer_registry_t); + if (!reg) + goto out; + pthread_mutex_init (®->lock, NULL); reg->active.next = ®->active; reg->active.prev = ®->active; reg->stale.next = ®->stale; reg->stale.prev = ®->stale; - pthread_create (®->th, NULL, gf_timer_proc, ctx); + ctx->timer = reg; + gf_thread_create (®->th, NULL, gf_timer_proc, ctx); } +out: return ctx->timer; } |
