Mercurial > hg > openttd
changeset 12177:11f0c0521003 draft
Allow adjusting the priority of threads.
author | Michael Lutz <michi@icosahedron.de> |
---|---|
date | Sat, 10 May 2008 23:04:52 +0200 |
parents | a8461c552ea5 |
children | 08d76f3dda8b |
files | src/thread.h src/thread_pthread.cpp src/thread_win32.cpp |
diffstat | 3 files changed, 45 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/thread.h +++ b/src/thread.h @@ -14,6 +14,12 @@ */ class ThreadObject { public: + enum Priority { + THREAD_PRIO_LOW, + THREAD_PRIO_NORMAL, + THREAD_PRIO_HIGH + }; + /** * Virtual destructor to allow 'delete' operator to work properly. */ @@ -30,6 +36,12 @@ virtual void Join() = 0; /** + * Set the thread priority to a specific level. May be a no-op. + * @param priority the priority level. + */ + virtual void SetThreadPriority(Priority priority) {} + + /** * Create a thread; proc will be called as first function inside the thread, * with optinal params. * @param proc The procedure to call inside the thread.
--- a/src/thread_pthread.cpp +++ b/src/thread_pthread.cpp @@ -43,6 +43,33 @@ pthread_join(this->thread, NULL); this->thread = 0; } + + /* virtual */ void SetThreadPriority(Priority priority) + { + /* Query current scheduling policy. */ + int policy; + sched_param param; + pthread_getschedparam(m_thr, &policy, ¶m); + + int max_prio = sched_get_priority_max(policy); + int min_prio = sched_get_priority_min(policy); + + switch (priority) { + case THREAD_PRIO_LOW: + param.sched_priority = min_prio; + break; + case THREAD_PRIO_NORMAL: + param.sched_priority = (min_prio + max_prio) / 2; + break; + case THREAD_PRIO_HIGH: + param.sched_priority = max_prio; + break; + default: + NOT_REACHED(); + } + pthread_setschedparam(m_thr, policy, ¶m); + } + private: /** * On thread creation, this function is called, which calls the real startup
--- a/src/thread_win32.cpp +++ b/src/thread_win32.cpp @@ -59,6 +59,12 @@ WaitForSingleObject(this->thread, INFINITE); } + /* virtual */ void SetThreadPriority(Priority priority) + { + static const int prio_map[] = {THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_HIGHEST}; + ::SetThreadPriority(m_h_thr, prio_map[priority]); + } + private: /** * On thread creation, this function is called, which calls the real startup