diff --git a/src/include/switch_core.h b/src/include/switch_core.h index 522ebec1f8..8f9220d0b9 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -2328,7 +2328,7 @@ SWITCH_DECLARE(switch_status_t) switch_say_file_handle_create(switch_say_file_ha SWITCH_DECLARE(void) switch_say_file(switch_say_file_handle_t *sh, const char *fmt, ...); SWITCH_DECLARE(int) switch_max_file_desc(void); SWITCH_DECLARE(void) switch_close_extra_files(int *keep, int keep_ttl); - +SWITCH_DECLARE(switch_status_t) switch_core_thread_set_cpu_affinity(int cpu); SWITCH_END_EXTERN_C #endif /* For Emacs: diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index df28e346ae..d7dac7581b 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -1363,15 +1363,7 @@ void *SWITCH_THREAD_FUNC sofia_msg_thread_run(switch_thread_t *thread, void *obj switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "MSG Thread %d Started\n", my_id); -#ifdef HAVE_CPU_SET_MACROS - { - cpu_set_t set; - CPU_ZERO(&set); - CPU_SET(my_id, &set); - sched_setaffinity(0, sizeof(set), &set); - } -#endif - + switch_core_thread_set_cpu_affinity(my_id); while(switch_queue_pop(q, &pop) == SWITCH_STATUS_SUCCESS && pop) { sofia_dispatch_event_t *de = (sofia_dispatch_event_t *) pop; diff --git a/src/switch_core.c b/src/switch_core.c index f957a7ae5e..8139f8ccf8 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -1325,6 +1325,35 @@ SWITCH_DECLARE(uint32_t) switch_core_min_dtmf_duration(uint32_t duration) return runtime.min_dtmf_duration; } +SWITCH_DECLARE(switch_status_t) switch_core_thread_set_cpu_affinity(int cpu) +{ + switch_status_t status = SWITCH_STATUS_FALSE; + + if (cpu > -1) { + +#ifdef HAVE_CPU_SET_MACROS + cpu_set_t set; + + CPU_ZERO(&set); + CPU_SET(cpu, &set); + + if (!sched_setaffinity(0, sizeof(set), &set)) { + status = SWITCH_STATUS_SUCCESS; + } + +#else +#if WIN32 + if (SetThreadAffinityMask(GetCurrentThread(), (DWORD_PTR) cpu)) { + status = SWITCH_STATUS_SUCCESS; + } +#endif +#endif + } + + return status; +} + + static void switch_core_set_serial(void) { char buf[13] = ""; diff --git a/src/switch_event.c b/src/switch_event.c index fa864526c8..23670fac93 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -253,16 +253,8 @@ static void *SWITCH_THREAD_FUNC switch_event_dispatch_thread(switch_thread_t *th EVENT_DISPATCH_QUEUE_RUNNING[my_id] = 1; switch_mutex_unlock(EVENT_QUEUE_MUTEX); - -#ifdef HAVE_CPU_SET_MACROS - { - cpu_set_t set; - CPU_ZERO(&set); - CPU_SET(my_id, &set); - sched_setaffinity(0, sizeof(set), &set); - } -#endif - + + switch_core_thread_set_cpu_affinity(my_id); for (;;) { void *pop = NULL; diff --git a/src/switch_time.c b/src/switch_time.c index 17a0a50675..e7256538f3 100644 --- a/src/switch_time.c +++ b/src/switch_time.c @@ -795,14 +795,9 @@ SWITCH_MODULE_RUNTIME_FUNCTION(softtimer_runtime) runtime.profile_timer = switch_new_profile_timer(); switch_get_system_idle_time(runtime.profile_timer, &runtime.profile_time); -#ifdef HAVE_CPU_SET_MACROS - if (runtime.timer_affinity > -1) { - cpu_set_t set; - CPU_ZERO(&set); - CPU_SET(runtime.timer_affinity, &set); - sched_setaffinity(0, sizeof(set), &set); + if (runtime.timer_affinity > -1) { + switch_core_thread_set_cpu_affinity(runtime.timer_affinity); } -#endif switch_time_sync(); time_sync = runtime.time_sync;