From 8d9134f93ab80f34256d33b1affa220c1aebf3c5 Mon Sep 17 00:00:00 2001 From: crs Date: Sat, 3 May 2003 12:37:03 +0000 Subject: [PATCH] Boosted priority of main synergy threads to be very high (highest realtime priority). After some testing it appears that anything less than this can starve synergy in some circumstances, preventing it from forwarding messages to clients. This is a rather risky change since synergy can now virtually take over a system if it behaves badly. This change only affects windows systems since lib/arch of other platforms don't yet attempt to boost priority. --- lib/arch/CArchMultithreadWindows.cpp | 67 +++++++++++++++++----------- lib/synergy/CPrimaryScreen.cpp | 2 +- lib/synergy/CSecondaryScreen.cpp | 2 +- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/lib/arch/CArchMultithreadWindows.cpp b/lib/arch/CArchMultithreadWindows.cpp index c847e0ed..4934fc36 100644 --- a/lib/arch/CArchMultithreadWindows.cpp +++ b/lib/arch/CArchMultithreadWindows.cpp @@ -348,35 +348,52 @@ CArchMultithreadWindows::cancelThread(CArchThread thread) void CArchMultithreadWindows::setPriorityOfThread(CArchThread thread, int n) { + struct CPriorityInfo { + public: + DWORD m_class; + int m_level; + }; + static const CPriorityInfo s_pClass[] = { + { IDLE_PRIORITY_CLASS, THREAD_PRIORITY_IDLE }, + { IDLE_PRIORITY_CLASS, THREAD_PRIORITY_LOWEST }, + { IDLE_PRIORITY_CLASS, THREAD_PRIORITY_BELOW_NORMAL }, + { IDLE_PRIORITY_CLASS, THREAD_PRIORITY_NORMAL }, + { IDLE_PRIORITY_CLASS, THREAD_PRIORITY_ABOVE_NORMAL }, + { IDLE_PRIORITY_CLASS, THREAD_PRIORITY_HIGHEST }, + { NORMAL_PRIORITY_CLASS, THREAD_PRIORITY_LOWEST }, + { NORMAL_PRIORITY_CLASS, THREAD_PRIORITY_BELOW_NORMAL }, + { NORMAL_PRIORITY_CLASS, THREAD_PRIORITY_NORMAL }, + { NORMAL_PRIORITY_CLASS, THREAD_PRIORITY_ABOVE_NORMAL }, + { NORMAL_PRIORITY_CLASS, THREAD_PRIORITY_HIGHEST }, + { HIGH_PRIORITY_CLASS, THREAD_PRIORITY_LOWEST }, + { HIGH_PRIORITY_CLASS, THREAD_PRIORITY_BELOW_NORMAL }, + { HIGH_PRIORITY_CLASS, THREAD_PRIORITY_NORMAL }, + { HIGH_PRIORITY_CLASS, THREAD_PRIORITY_ABOVE_NORMAL }, + { HIGH_PRIORITY_CLASS, THREAD_PRIORITY_HIGHEST }, + { REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_IDLE }, + { REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_LOWEST }, + { REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_BELOW_NORMAL }, + { REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_NORMAL }, + { REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_ABOVE_NORMAL }, + { REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_HIGHEST }, + { REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_TIME_CRITICAL} + }; + static const size_t s_pMax = sizeof(s_pClass) / sizeof(s_pClass[0]) - 1; + static const size_t s_pBase = 8; // index of normal priority + assert(thread != NULL); - DWORD pClass = NORMAL_PRIORITY_CLASS; - if (n < 0) { - switch (-n) { - case 1: n = THREAD_PRIORITY_ABOVE_NORMAL; break; - case 2: n = THREAD_PRIORITY_HIGHEST; break; - default: - pClass = HIGH_PRIORITY_CLASS; - switch (-n - 3) { - case 0: n = THREAD_PRIORITY_LOWEST; break; - case 1: n = THREAD_PRIORITY_BELOW_NORMAL; break; - case 2: n = THREAD_PRIORITY_NORMAL; break; - case 3: n = THREAD_PRIORITY_ABOVE_NORMAL; break; - default: n = THREAD_PRIORITY_HIGHEST; break; - } - break; - } + size_t index = s_pBase - n; + if (index < 0) { + // lowest priority + index = 0; } - else { - switch (n) { - case 0: n = THREAD_PRIORITY_NORMAL; break; - case 1: n = THREAD_PRIORITY_BELOW_NORMAL; break; - case 2: n = THREAD_PRIORITY_LOWEST; break; - default: n = THREAD_PRIORITY_IDLE; break; - } + else if (index > s_pMax) { + // highest priority + index = s_pMax; } - SetPriorityClass(thread->m_thread, pClass); - SetThreadPriority(thread->m_thread, n); + SetPriorityClass(thread->m_thread, s_pClass[index].m_class); + SetThreadPriority(thread->m_thread, s_pClass[index].m_level); } void diff --git a/lib/synergy/CPrimaryScreen.cpp b/lib/synergy/CPrimaryScreen.cpp index e2b743d7..79d0384e 100644 --- a/lib/synergy/CPrimaryScreen.cpp +++ b/lib/synergy/CPrimaryScreen.cpp @@ -40,7 +40,7 @@ void CPrimaryScreen::mainLoop() { // change our priority - CThread::getCurrentThread().setPriority(-3); + CThread::getCurrentThread().setPriority(-13); // run event loop try { diff --git a/lib/synergy/CSecondaryScreen.cpp b/lib/synergy/CSecondaryScreen.cpp index 5df455a8..0f8bdbc2 100644 --- a/lib/synergy/CSecondaryScreen.cpp +++ b/lib/synergy/CSecondaryScreen.cpp @@ -38,7 +38,7 @@ void CSecondaryScreen::mainLoop() { // change our priority - CThread::getCurrentThread().setPriority(-7); + CThread::getCurrentThread().setPriority(-13); // run event loop try {