From e1a8c8f76c622b0c31e72b6d457908289a2e47ee Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Sun, 16 May 2010 20:47:33 +0200 Subject: [PATCH] Switch time api from mutex to spinlock. --- src/suricata.c | 2 ++ src/util-time.c | 19 ++++++++++++++----- src/util-time.h | 3 +++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/suricata.c b/src/suricata.c index e4c4709a03..323e6f060e 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -673,6 +673,7 @@ int main(int argc, char **argv) /* Initializations for global vars, queues, etc (memsets, mutex init..) */ GlobalInits(); + TimeInit(); /* Load yaml configuration file if provided. */ if (conf_filename != NULL) { @@ -1132,6 +1133,7 @@ int main(int argc, char **argv) RunModeShutDown(); OutputDeregisterAll(); + TimeDeinit(); #ifdef __SC_CUDA_SUPPORT__ /* all cuda contexts attached to any threads should be free by now. diff --git a/src/util-time.c b/src/util-time.c index e3748016b4..5d7eb489eb 100644 --- a/src/util-time.c +++ b/src/util-time.c @@ -29,9 +29,18 @@ #include "util-debug.h" static struct timeval current_time = { 0, 0 }; -static SCMutex current_time_mutex = PTHREAD_MUTEX_INITIALIZER; +//static SCMutex current_time_mutex = PTHREAD_MUTEX_INITIALIZER; +static SCSpinlock current_time_spinlock; static char live = TRUE; +void TimeInit(void) { + SCSpinInit(¤t_time_spinlock, 0); +} + +void TimeDeinit(void) { + SCSpinDestroy(¤t_time_spinlock); +} + void TimeModeSetLive(void) { live = TRUE; @@ -52,14 +61,14 @@ void TimeSet(struct timeval *tv) if (tv == NULL) return; - SCMutexLock(¤t_time_mutex); + SCSpinLock(¤t_time_spinlock); current_time.tv_sec = tv->tv_sec; current_time.tv_usec = tv->tv_usec; SCLogDebug("time set to %" PRIuMAX " sec, %" PRIuMAX " usec", (uintmax_t)current_time.tv_sec, (uintmax_t)current_time.tv_usec); - SCMutexUnlock(¤t_time_mutex); + SCSpinUnlock(¤t_time_spinlock); } /** \brief set the time to "gettimeofday" meant for testing */ @@ -80,10 +89,10 @@ void TimeGet(struct timeval *tv) if (live == TRUE) { gettimeofday(tv, NULL); } else { - SCMutexLock(¤t_time_mutex); + SCSpinLock(¤t_time_spinlock); tv->tv_sec = current_time.tv_sec; tv->tv_usec = current_time.tv_usec; - SCMutexUnlock(¤t_time_mutex); + SCSpinUnlock(¤t_time_spinlock); } SCLogDebug("time we got is %" PRIuMAX " sec, %" PRIuMAX " usec", diff --git a/src/util-time.h b/src/util-time.h index ed697cf2be..a413407c6b 100644 --- a/src/util-time.h +++ b/src/util-time.h @@ -34,6 +34,9 @@ typedef struct SCTimeval32_ { uint32_t tv_usec; } SCTimeval32; +void TimeInit(void); +void TimeDeinit(void); + void TimeSet(struct timeval *); void TimeGet(struct timeval *);