From 3515c7a0207144ff99fd48d4e75b82113d2a090a Mon Sep 17 00:00:00 2001 From: Jeff Lenk Date: Wed, 28 Jul 2010 20:44:45 -0500 Subject: [PATCH] FSCORE-643 Windows: Add start parameter -monotonic-clock, replaces build flag WIN32_MONOTONIC --- src/include/switch_types.h | 3 ++- src/switch.c | 14 ++++++++++++++ src/switch_time.c | 17 +++++++++++------ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 7752c4fbef..1808aa167e 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -251,7 +251,8 @@ typedef enum { SCF_CALIBRATE_CLOCK = (1 << 8), SCF_USE_HEAVY_TIMING = (1 << 9), SCF_USE_CLOCK_RT = (1 << 10), - SCF_VERBOSE_EVENTS = (1 << 11) + SCF_VERBOSE_EVENTS = (1 << 11), + SCF_USE_WIN32_MONOTONIC = (1 << 12) } switch_core_flag_enum_t; typedef uint32_t switch_core_flag_t; diff --git a/src/switch.c b/src/switch.c index 77aea8dc00..a28baba6f0 100644 --- a/src/switch.c +++ b/src/switch.c @@ -62,6 +62,7 @@ static char *pfile = PIDFILE; #define SERVICENAME_DEFAULT "FreeSWITCH" #define SERVICENAME_MAXLEN 256 static char service_name[SERVICENAME_MAXLEN]; +static switch_core_flag_t service_flags = SCF_NONE; #include #include @@ -174,6 +175,11 @@ void WINAPI service_main(DWORD numArgs, char **args) { switch_core_flag_t flags = SCF_USE_SQL | SCF_USE_AUTO_NAT | SCF_CALIBRATE_CLOCK | SCF_USE_CLOCK_RT; const char *err = NULL; /* error value for return from freeswitch initialization */ + + /* Override flags if they have been set earlier */ + if (service_flags != SCF_NONE) + flags = service_flags; + /* we have to initialize the service-specific stuff */ memset(&status, 0, sizeof(SERVICE_STATUS)); status.dwServiceType = SERVICE_WIN32; @@ -319,6 +325,7 @@ int main(int argc, char *argv[]) "\t-service [name] -- start freeswitch as a service, cannot be used if loaded as a console app\n" "\t-install [name] -- install freeswitch as a service, with optional service name\n" "\t-uninstall -- remove freeswitch as a service\n" + "\t-monotonic-clock -- use monotonic clock as timer source\n" #else "\t-nf -- no forking\n" "\t-u [user] -- specify user to switch to\n" "\t-g [group] -- specify group to switch to\n" @@ -427,6 +434,11 @@ int main(int argc, char *argv[]) } } } + + if (local_argv[x] && !strcmp(local_argv[x], "-monotonic-clock")) { + flags |= SCF_USE_WIN32_MONOTONIC; + known_opt++; + } #else if (local_argv[x] && !strcmp(local_argv[x], "-u")) { x++; @@ -732,6 +744,8 @@ int main(int argc, char *argv[]) , {NULL, NULL} }; + service_flags = flags; /* copy parsed flags for service startup */ + if (StartServiceCtrlDispatcher(dispatchTable) == 0) { /* Not loaded as a service */ fprintf(stderr, "Error Freeswitch loaded as a console app with -service option\n"); diff --git a/src/switch_time.c b/src/switch_time.c index 5e93d59261..5c07409d93 100644 --- a/src/switch_time.c +++ b/src/switch_time.c @@ -48,10 +48,11 @@ #define MAX_ELEMENTS 3600 #define IDLE_SPEED 100 -/* For now enable WIN32_MONOTONIC on Windows 2003 Server and Windows XP systems for improved timer support */ -/* GetSystemTimeAsFileTime does not update on timeBeginPeriod on these OS */ -/* we leave the normal timer support as the default for now */ -#if (defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)) || defined(WIN32_MONOTONIC) +/* In Windows, enable the montonic timer for better timer accuracy on Windows 2003 Server, XP and older */ +/* GetSystemTimeAsFileTime does not update on timeBeginPeriod on these OS. */ +/* Flag SCF_USE_WIN32_MONOTONIC must be enabled to activate it (start parameter -monotonic-clock) */ + +#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) static int MONO = 1; #else static int MONO = 0; @@ -344,7 +345,7 @@ static switch_time_t time_now(int64_t offset) { switch_time_t now; -#if (defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)) || defined(WIN32_MONOTONIC) +#if (defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)) || defined(WIN32) if (MONO) { #ifndef WIN32 struct timespec ts; @@ -375,7 +376,7 @@ static switch_time_t time_now(int64_t offset) #endif now = switch_time_now(); -#if (defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)) || defined(WIN32_MONOTONIC) +#if (defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)) || defined(WIN32) } #endif @@ -1074,6 +1075,10 @@ SWITCH_MODULE_LOAD_FUNCTION(softtimer_load) switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Clock calibration disabled.\n"); } + if (switch_test_flag((&runtime), SCF_USE_WIN32_MONOTONIC)) { + MONO = 1; + } + /* indicate that the module should continue to be loaded */ return SWITCH_STATUS_SUCCESS; }