}
for (x = 1; x <= max; x++) {
- then = switch_time_now();
+ then = switch_time_ref();
switch_yield(mss);
- now = switch_time_now();
+ now = switch_time_ref();
diff = (int) (now - then);
stream->write_function(stream, "test %d sleep %ld %d\n", x, mss, diff);
total += diff;
goto end;
}
- start = switch_time_now();
+ start = switch_time_ref();
for (x = 1; x <= max; x++) {
- then = switch_time_now();
+ then = switch_time_ref();
switch_core_timer_next(&timer);
- now = switch_time_now();
+ now = switch_time_ref();
diff = (int) (now - then);
//stream->write_function(stream, "test %d sleep %ld %d\n", x, mss, diff);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Timer Test: %d sleep %d %d\n", x, mss, diff);
total += diff;
}
- end = switch_time_now();
+ end = switch_time_ref();
switch_yield(250000);
#define MAX_ELEMENTS 3600
#define IDLE_SPEED 100
-#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
+/* 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)
static int MONO = 1;
#else
static int MONO = 0;
static int MATRIX = 1;
+#ifdef WIN32
+static switch_time_t win32_tick_time_since_start = -1;
+static DWORD win32_last_get_time_tick = 0;
+CRITICAL_SECTION timer_section;
+#endif
+
#define ONEMS
#ifdef ONEMS
static int STEP_MS = 1;
switch_time_t start, stop, sum = 0;
for (x = 0; x < reps; x++) {
- start = switch_time_now();
+ start = switch_time_ref();
do_sleep(t);
- stop = switch_time_now();
+ stop = switch_time_ref();
sum += (stop - start);
}
{
switch_time_t now;
-#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
+#if (defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)) || defined(WIN32_MONOTONIC)
if (MONO) {
+#ifndef WIN32
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
now = ts.tv_sec * APR_USEC_PER_SEC + (ts.tv_nsec / 1000) + offset;
+#else
+ DWORD tick_now;
+ DWORD tick_diff;
+
+ tick_now = timeGetTime();
+ if (win32_tick_time_since_start != -1) {
+ EnterCriticalSection(&timer_section);
+ /* just add diff (to make it work more than 50 days). */
+ tick_diff = tick_now - win32_last_get_time_tick;
+ win32_tick_time_since_start += tick_diff;
+
+ win32_last_get_time_tick = tick_now;
+ now = (win32_tick_time_since_start * 1000) + offset;
+ LeaveCriticalSection(&timer_section);
+ } else {
+ /* If someone is calling us before timer is initialized,
+ * return the current tick + offset
+ */
+ now = (tick_now * 1000) + offset;
+ }
+#endif
} else {
#endif
now = switch_time_now();
-#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
+#if (defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)) || defined(WIN32_MONOTONIC)
}
#endif
return now;
}
+SWITCH_DECLARE(switch_time_t) switch_time_ref(void)
+{
+ return time_now(0);
+}
+
SWITCH_DECLARE(void) switch_time_sync(void)
{
runtime.reference = switch_time_now();
#if defined(WIN32)
timeBeginPeriod(1);
+ InitializeCriticalSection(&timer_section);
+ win32_last_get_time_tick = timeGetTime();
+ win32_tick_time_since_start = win32_last_get_time_tick;
#endif
memset(&globals, 0, sizeof(globals));
}
#if defined(WIN32)
timeEndPeriod(1);
+ win32_tick_time_since_start = -1; /* we are not initialized anymore */
+ DeleteCriticalSection(&timer_section);
#endif
if (TIMEZONES_LIST.hash) {