From 9571f7942ca9174b29573c1cac804d6ad3f3c293 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 12 Nov 2008 13:55:22 +0000 Subject: [PATCH] posturepedics for better sleep git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@10357 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_apr.h | 1 + .../applications/mod_commands/mod_commands.c | 31 ++++++++ src/switch_time.c | 72 ++++++------------- 3 files changed, 54 insertions(+), 50 deletions(-) diff --git a/src/include/switch_apr.h b/src/include/switch_apr.h index a35b1d2810..080c4e4bf1 100644 --- a/src/include/switch_apr.h +++ b/src/include/switch_apr.h @@ -337,6 +337,7 @@ SWITCH_DECLARE(switch_status_t) switch_time_exp_tz(switch_time_exp_t *result, sw * @warning May sleep for longer than the specified time. */ SWITCH_DECLARE(void) switch_sleep(switch_interval_time_t t); +SWITCH_DECLARE(void) switch_micro_sleep(switch_interval_time_t t); /** @} */ diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index f0e4a48146..5d43fa27c3 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -42,6 +42,36 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load); SWITCH_MODULE_DEFINITION(mod_commands, mod_commands_load, NULL, NULL); +SWITCH_STANDARD_API(time_test_function) +{ + switch_time_t now, then; + int x; + long mss = atol(cmd); + uint32_t total = 0; + int diff; + int max = 10; + char *p; + + if ((p = strchr(cmd, ' '))) { + max = atoi(p+1); + if (max < 0) { + max = 10; + } + } + + for (x = 0; x < max; x++) { + then = switch_time_now(); + switch_yield(mss); + now = switch_time_now(); + diff = (int) now - then; + stream->write_function(stream, "test %d sleep %ld %d\n", x+1, mss, diff); + total += diff; + } + stream->write_function(stream, "avg %d\n", total / x); + + return SWITCH_STATUS_SUCCESS; +} + SWITCH_STANDARD_API(user_data_function) { switch_xml_t x_domain, xml = NULL, x_user = NULL, x_param, x_params; @@ -2939,6 +2969,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load) SWITCH_ADD_API(commands_api_interface, "uuid_send_dtmf", "send dtmf digits", uuid_send_dtmf_function, UUID_SEND_DTMF_SYNTAX); SWITCH_ADD_API(commands_api_interface, "eval", "eval (noop)", eval_function, ""); SWITCH_ADD_API(commands_api_interface, "system", "Execute a system command", system_function, SYSTEM_SYNTAX); + SWITCH_ADD_API(commands_api_interface, "time_test", "time_test", time_test_function, ""); /* indicate that the module should continue to be loaded */ return SWITCH_STATUS_NOUNLOAD; diff --git a/src/switch_time.c b/src/switch_time.c index 63b9e185e4..daa284fe37 100644 --- a/src/switch_time.c +++ b/src/switch_time.c @@ -106,25 +106,6 @@ static int MONO = 0; #endif -static void do_yield(switch_interval_time_t t) -{ -#if defined(HAVE_CLOCK_NANOSLEEP) && defined(SWITCH_USE_CLOCK_FUNCS) - struct timespec ts; - ts.tv_sec = t / APR_USEC_PER_SEC; - ts.tv_nsec = (t % APR_USEC_PER_SEC) * 1000; - - clock_nanosleep(CLOCK_REALTIME, 0, &ts, NULL); - -#elif defined(HAVE_USLEEP) - usleep(t); -#elif defined(WIN32) - Sleep((DWORD) ((t) / 1000)); -#else - apr_sleep(t); -#endif - -} - SWITCH_DECLARE(void) switch_time_set_monotonic(switch_bool_t enable) { MONO = enable ? 1 : 0; @@ -158,39 +139,29 @@ SWITCH_DECLARE(void) switch_time_sync(void) runtime.reference = time_now(runtime.offset); } +SWITCH_DECLARE(void) switch_micro_sleep(switch_interval_time_t t) +{ + apr_sleep(t); +} + SWITCH_DECLARE(void) switch_sleep(switch_interval_time_t t) { + if (t <= 1000) { +#if defined(WIN32) + Sleep(1); +#else + apr_sleep(t); +#endif + return; + } + if (globals.use_cond_yield == 1) { switch_cond_yield((uint32_t)(t / 1000)); return; } - -#if defined(HAVE_USLEEP) - usleep(t); -#elif defined(WIN32) - Sleep((DWORD) ((t) / 1000)); -#else + apr_sleep(t); -#endif - -#if 0 -#if defined(HAVE_CLOCK_NANOSLEEP) && defined(SWITCH_USE_CLOCK_FUNCS) - struct timespec ts; - ts.tv_sec = t / APR_USEC_PER_SEC; - ts.tv_nsec = (t % APR_USEC_PER_SEC) * 1000; - - clock_nanosleep(CLOCK_REALTIME, 0, &ts, NULL); - -#elif defined(HAVE_USLEEP) - usleep(t); -#elif defined(WIN32) - Sleep((DWORD) ((t) / 1000)); -#else - apr_sleep(t); -#endif -#endif - } @@ -199,7 +170,8 @@ SWITCH_DECLARE(void) switch_cond_yield(uint32_t ms) if (!ms) return; if (globals.use_cond_yield != 1) { - do_yield(ms * 1000); + apr_sleep(ms * 1000); + return; } switch_mutex_lock(TIMER_MATRIX[1].mutex); @@ -216,7 +188,7 @@ static switch_status_t timer_init(switch_timer_t *timer) int sanity = 0; while (globals.STARTED == 0) { - do_yield(100000); + apr_sleep(100000); if (++sanity == 10) { break; } @@ -312,7 +284,7 @@ static switch_status_t timer_next(switch_timer_t *timer) #else while (globals.RUNNING == 1 && private_info->ready && TIMER_MATRIX[timer->interval].tick < private_info->reference) { check_roll(); - do_yield(1000); + apr_sleep(1000); } #endif @@ -396,7 +368,7 @@ SWITCH_MODULE_RUNTIME_FUNCTION(softtimer_runtime) runtime.initiated = runtime.reference; break; } - do_yield(STEP_MIC); + apr_sleep(STEP_MIC); last = ts; } } @@ -431,7 +403,7 @@ SWITCH_MODULE_RUNTIME_FUNCTION(softtimer_runtime) } else { rev_errs = 0; } - do_yield(STEP_MIC); + apr_sleep(STEP_MIC); last = ts; } @@ -739,7 +711,7 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(softtimer_shutdown) switch_mutex_unlock(globals.mutex); while (globals.RUNNING == -1) { - do_yield(10000); + apr_sleep(10000); } } #if defined(WIN32)