From 5254d7c3a14177dffdf43c3ec7e5235b066d986e Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Tue, 4 Mar 2008 14:45:19 +0000 Subject: Ticket #500: Added function to set thread priority in PJLIB git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1841 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib/include/pj/os.h | 40 ++++++++++++++++++ pjlib/src/pj/os_core_symbian.cpp | 42 +++++++++++++++++++ pjlib/src/pj/os_core_unix.c | 87 ++++++++++++++++++++++++++++++++++++++++ pjlib/src/pj/os_core_win32.c | 55 +++++++++++++++++++++++++ 4 files changed, 224 insertions(+) diff --git a/pjlib/include/pj/os.h b/pjlib/include/pj/os.h index a5c10df8..f5503c98 100644 --- a/pjlib/include/pj/os.h +++ b/pjlib/include/pj/os.h @@ -137,6 +137,46 @@ PJ_DECL(pj_status_t) pj_thread_register ( const char *thread_name, PJ_DECL(pj_bool_t) pj_thread_is_registered(void); +/** + * Get thread priority value for the thread. + * + * @param thread Thread handle. + * + * @return Thread priority value, or -1 on error. + */ +PJ_DECL(int) pj_thread_get_prio(pj_thread_t *thread); + + +/** + * Set the thread priority. The priority value must be in the priority + * value range, which can be retrieved with #pj_thread_get_prio_min() and + * #pj_thread_get_prio_max() functions. + * + * @param thread Thread handle. + * @param prio New priority to be set to the thread. + * + * @return PJ_SUCCESS on success or the error code. + */ +PJ_DECL(pj_status_t) pj_thread_set_prio(pj_thread_t *thread, int prio); + +/** + * Get the lowest priority value available for this thread. + * + * @param thread Thread handle. + * @return Minimum thread priority value, or -1 on error. + */ +PJ_DECL(int) pj_thread_get_prio_min(pj_thread_t *thread); + + +/** + * Get the highest priority value available for this thread. + * + * @param thread Thread handle. + * @return Minimum thread priority value, or -1 on error. + */ +PJ_DECL(int) pj_thread_get_prio_max(pj_thread_t *thread); + + /** * Return native handle from pj_thread_t for manipulation using native * OS APIs. diff --git a/pjlib/src/pj/os_core_symbian.cpp b/pjlib/src/pj/os_core_symbian.cpp index c5707bb1..258a8a8b 100644 --- a/pjlib/src/pj/os_core_symbian.cpp +++ b/pjlib/src/pj/os_core_symbian.cpp @@ -484,6 +484,48 @@ PJ_DEF(pj_bool_t) pj_thread_is_registered(void) return PJ_FALSE; } + +/* + * Get thread priority value for the thread. + */ +PJ_DEF(int) pj_thread_get_prio(pj_thread_t *thread) +{ + PJ_UNUSED_ARG(thread); + return 1; +} + + +/* + * Set the thread priority. + */ +PJ_DEF(pj_status_t) pj_thread_set_prio(pj_thread_t *thread, int prio) +{ + PJ_UNUSED_ARG(thread); + PJ_UNUSED_ARG(prio); + return PJ_SUCCESS; +} + + +/* + * Get the lowest priority value available on this system. + */ +PJ_DEF(int) pj_thread_get_prio_min(pj_thread_t *thread) +{ + PJ_UNUSED_ARG(thread); + return 1; +} + + +/* + * Get the highest priority value available on this system. + */ +PJ_DEF(int) pj_thread_get_prio_max(pj_thread_t *thread) +{ + PJ_UNUSED_ARG(thread); + return 1; +} + + /* * pj_thread_get_os_handle() */ diff --git a/pjlib/src/pj/os_core_unix.c b/pjlib/src/pj/os_core_unix.c index 1fe5253b..31cbb2e9 100644 --- a/pjlib/src/pj/os_core_unix.c +++ b/pjlib/src/pj/os_core_unix.c @@ -238,6 +238,93 @@ PJ_DEF(pj_bool_t) pj_thread_is_registered(void) #endif } + +/* + * Get thread priority value for the thread. + */ +PJ_DEF(int) pj_thread_get_prio(pj_thread_t *thread) +{ +#if PJ_HAS_THREADS + sched_param param; + int policy; + int rc; + + rc = pthread_getschedparam (thread->thread, &policy, ¶m); + if (rc != 0) + return -1; + + return param.sched_priority; +#else + PJ_UNUSED_ARG(thread); + return 1; +#endif +} + + +/* + * Set the thread priority. + */ +PJ_DEF(pj_status_t) pj_thread_set_prio(pj_thread_t *thread, int prio) +{ +#if PJ_HAS_THREADS + sched_param param; + int policy; + int rc; + + rc = pthread_getschedparam (thread->thread, &policy, ¶m); + if (rc != 0) + return PJ_RETURN_OS_ERROR(rc); + + param.sched_priority = prio; + + rc = pthread_setschedparam(tid, policy, ¶m); + if (rc != 0) + return PJ_RETURN_OS_ERROR(rc); + + return PJ_SUCCESS; +#else + PJ_UNUSED_ARG(thread); + PJ_UNUSED_ARG(prio); + pj_assert("pj_thread_set_prio() called in non-threading mode!"); + return 1; +#endif +} + + +/* + * Get the lowest priority value available on this system. + */ +PJ_DEF(int) pj_thread_get_prio_min(pj_thread_t *thread) +{ + sched_param param; + int policy; + int rc; + + rc = pthread_getschedparam(thread->thread, &policy, ¶m); + if (rc != 0) + return -1; + + return sched_get_priority_min(policy); +} + + +/* + * Get the highest priority value available on this system. + */ +PJ_DEF(int) pj_thread_get_prio_max(pj_thread_t *thread) +{ + sched_param param; + int policy; + int rc; + + rc = pthread_getschedparam(thread->thread, &policy, ¶m); + if (rc != 0) + return -1; + + return sched_get_priority_max(policy); +} + + /* * Get native thread handle */ diff --git a/pjlib/src/pj/os_core_win32.c b/pjlib/src/pj/os_core_win32.c index 4bc8a970..80c24604 100644 --- a/pjlib/src/pj/os_core_win32.c +++ b/pjlib/src/pj/os_core_win32.c @@ -253,6 +253,61 @@ PJ_DEF(pj_bool_t) pj_thread_is_registered(void) return pj_thread_local_get(thread_tls_id) != 0; } + +/* + * Get thread priority value for the thread. + */ +PJ_DEF(int) pj_thread_get_prio(pj_thread_t *thread) +{ + return GetThreadPriority(thread->hthread); +} + + +/* + * Set the thread priority. + */ +PJ_DEF(pj_status_t) pj_thread_set_prio(pj_thread_t *thread, int prio) +{ +#if PJ_HAS_THREADS + PJ_ASSERT_RETURN(thread, PJ_EINVAL); + PJ_ASSERT_RETURN(prio>=THREAD_PRIORITY_IDLE && + prio<=THREAD_PRIORITY_TIME_CRITICAL, + PJ_EINVAL); + + if (SetThreadPriority(thread->hthread, prio) == FALSE) + return PJ_RETURN_OS_ERROR(GetLastError()); + + return PJ_SUCCESS; + +#else + PJ_UNUSED_ARG(thread); + PJ_UNUSED_ARG(prio); + pj_assert("pj_thread_set_prio() called in non-threading mode!"); + return PJ_EINVALIDOP; +#endif +} + + +/* + * Get the lowest priority value available on this system. + */ +PJ_DEF(int) pj_thread_get_prio_min(pj_thread_t *thread) +{ + PJ_UNUSED_ARG(thread); + return THREAD_PRIORITY_IDLE; +} + + +/* + * Get the highest priority value available on this system. + */ +PJ_DEF(int) pj_thread_get_prio_max(pj_thread_t *thread) +{ + PJ_UNUSED_ARG(thread); + return THREAD_PRIORITY_TIME_CRITICAL; +} + + /* * Get native thread handle */ -- cgit v1.2.3