8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-24 00:03:03 +01:00

Increase time interval, given to thread with high priority in JRD_reschedule

Cleanup
This commit is contained in:
alexpeshkoff 2002-11-11 18:09:01 +00:00
parent 0798f44283
commit 2dab094729
2 changed files with 86 additions and 70 deletions

View File

@ -53,6 +53,7 @@ private:
static ThreadPriorityScheduler * news; // where starts new thps chain static ThreadPriorityScheduler * news; // where starts new thps chain
static BOOLEAN initialized; static BOOLEAN initialized;
static DWORD specific_key; // for thread LS access static DWORD specific_key; // for thread LS access
static BOOLEAN shutdown; // server shutting down
ThreadPriorityScheduler * next; // next thread in list ThreadPriorityScheduler * next; // next thread in list
union { union {
@ -76,6 +77,7 @@ public:
static void Exit(void); static void Exit(void);
static thdd *Get(void); static thdd *Get(void);
static void Set(thdd *val); static void Set(thdd *val);
static BOOLEAN Boosted(void);
static void Cleanup(void); static void Cleanup(void);
static void Init(void); static void Init(void);
static void Attach(HANDLE tHandle, DWORD thread_id, int &p); static void Attach(HANDLE tHandle, DWORD thread_id, int &p);
@ -96,6 +98,7 @@ public:
#define THPS_FINI() ThreadPriorityScheduler::Cleanup() #define THPS_FINI() ThreadPriorityScheduler::Cleanup()
#define THPS_ATTACH(handle, thread_id, priority) \ #define THPS_ATTACH(handle, thread_id, priority) \
ThreadPriorityScheduler::Attach(handle, thread_id, priority) ThreadPriorityScheduler::Attach(handle, thread_id, priority)
#define THPS_BOOSTDONE() ThreadPriorityScheduler::Boosted()
#else // THREAD_PSCHED #else // THREAD_PSCHED
@ -106,6 +109,7 @@ public:
#define THPS_INIT() #define THPS_INIT()
#define THPS_FINI() #define THPS_FINI()
#define THPS_ATTACH(handle, thread_id, priority) #define THPS_ATTACH(handle, thread_id, priority)
#define THPS_BOOSTDONE() FALSE
#endif // THREAD_PSCHED #endif // THREAD_PSCHED

View File

@ -46,6 +46,8 @@ ThreadPriorityScheduler * ThreadPriorityScheduler::chain = 0;
ThreadPriorityScheduler * ThreadPriorityScheduler::news = 0; ThreadPriorityScheduler * ThreadPriorityScheduler::news = 0;
BOOLEAN ThreadPriorityScheduler::initialized = FALSE; BOOLEAN ThreadPriorityScheduler::initialized = FALSE;
DWORD ThreadPriorityScheduler::specific_key = -1; DWORD ThreadPriorityScheduler::specific_key = -1;
BOOLEAN ThreadPriorityScheduler::shutdown = FALSE;
//____________________________________________________________ //____________________________________________________________
// //
// Shutdown // Shutdown
@ -54,13 +56,7 @@ void ThreadPriorityScheduler::Cleanup(void) {
if (initialized) if (initialized)
{ {
initialized = FALSE; initialized = FALSE;
TlsFree(specific_key); shutdown = TRUE;
#pragma FB_COMPILER_MESSAGE("Fix! Adjust cleanup !!!")
// we must stop Scheduler, clear all intances
// and then
// delete pool;
THD_mutex_destroy(&mutex);
} }
} }
@ -118,8 +114,7 @@ void ThreadPriorityScheduler::Init(void)
HANDLE real_handle = (HANDLE)_beginthreadex(NULL, 0, HANDLE real_handle = (HANDLE)_beginthreadex(NULL, 0,
Scheduler, 0, 0, &thread_id); Scheduler, 0, 0, &thread_id);
if (! real_handle) if (! real_handle)
#pragma FB_COMPILER_MESSAGE("Fix! Replace with MORE appropriate handling.") Firebird::system_call_failed::raise();
Firebird::memory_corrupt::raise();
SetThreadPriority(real_handle, THREAD_PRIORITY_TIME_CRITICAL); SetThreadPriority(real_handle, THREAD_PRIORITY_TIME_CRITICAL);
CloseHandle(real_handle); CloseHandle(real_handle);
} }
@ -132,7 +127,7 @@ void ThreadPriorityScheduler::Init(void)
void ThreadPriorityScheduler::Attach(HANDLE tHandle, DWORD thread_id, int &p) { void ThreadPriorityScheduler::Attach(HANDLE tHandle, DWORD thread_id, int &p) {
#ifdef DEV_BUILD #ifdef DEV_BUILD
if (! pool) if (! pool)
Firebird::memory_corrupt::raise(); Firebird::fatal_exception::raise("Missing pool in ThreadPriorityScheduler");
#endif #endif
UCHAR flags = 0; UCHAR flags = 0;
@ -162,8 +157,7 @@ void ThreadPriorityScheduler::Attach(HANDLE tHandle, DWORD thread_id, UCHAR flag
if (! DuplicateHandle(process, tHandle, if (! DuplicateHandle(process, tHandle,
process, &m->handle, 0, process, &m->handle, 0,
FALSE, DUPLICATE_SAME_ACCESS)) FALSE, DUPLICATE_SAME_ACCESS))
#pragma FB_COMPILER_MESSAGE("Fix! Replace with MORE appropriate handling.") Firebird::system_call_failed::raise();
Firebird::memory_corrupt::raise();
THD_mutex_lock(&mutex); THD_mutex_lock(&mutex);
m->next = news; m->next = news;
@ -195,7 +189,7 @@ ThreadPriorityScheduler * ThreadPriorityScheduler::Attach(void) {
return m; return m;
} }
THD_mutex_unlock(&mutex); THD_mutex_unlock(&mutex);
Firebird::memory_corrupt::raise(); Firebird::fatal_exception::raise("Unknown thread tried to attach to ThreadPriorityScheduler");
// Never get - avoid warnings // Never get - avoid warnings
return 0; return 0;
} }
@ -221,6 +215,14 @@ void ThreadPriorityScheduler::Exit(void) {
t->goneout = 1; t->goneout = 1;
} }
//____________________________________________________________
//
// Check whether current thread has high priority
//
BOOLEAN ThreadPriorityScheduler::Boosted(void) {
return InternalGet()->flags & THPS_BOOSTED ? TRUE : FALSE;
}
//____________________________________________________________ //____________________________________________________________
// //
// Scheduler Thread // Scheduler Thread
@ -243,41 +245,44 @@ unsigned int __stdcall ThreadPriorityScheduler::Scheduler(LPVOID) {
t->goneout = 0; t->goneout = 0;
t->flags &= ~(THPS_UP | THPS_LOW); // clean them t->flags &= ~(THPS_UP | THPS_LOW); // clean them
#pragma FB_COMPILER_MESSAGE("Fix! May have problems with long running UDFs.") #pragma FB_COMPILER_MESSAGE("Fix! May have problems with long running UDFs.")
// 1. thread went out of single thread zone if ((! gonein) && (! (p_flags & THPS_BOOSTED))) {
// and didn't return into it since last cycle: if (p_flags & THPS_UP) {
// increase priority // 1. thread exited single thread zone and didn't
if ((p_flags & THPS_UP) && (! gonein) && // return into it since this &last cycle:
(! (p_flags & THPS_BOOSTED))) { // increase priority
SetThreadPriority(t->handle, THREAD_PRIORITY_HIGHEST); if (! SetThreadPriority(t->handle,
THREAD_PRIORITY_HIGHEST))
Firebird::system_call_failed::raise();
#ifdef THREAD_PSCHED_DEBUG #ifdef THREAD_PSCHED_DEBUG
gds__log("+ handle=%p priority=%d", t->handle, THREAD_PRIORITY_HIGHEST); gds__log("+ handle=%p priority=%d", t->handle, THREAD_PRIORITY_HIGHEST);
#endif #endif
t->flags |= THPS_BOOSTED; t->flags |= THPS_BOOSTED;
continue; continue;
} }
// 2. thread exited single thread zone // 2. thread exited single thread zone
// and never returned there during this cycle: // and never returned there during this cycle:
// candidate for priority increase // candidate for priority increase
if ((! gonein) && (! (p_flags & THPS_BOOSTED))) {
t->flags |= THPS_UP; t->flags |= THPS_UP;
continue; continue;
} }
if ((gonein || t->inside) && (p_flags & THPS_BOOSTED)) {
if (p_flags & THPS_LOW) {
// 3. thread entered single thread zone // 3. thread entered single thread zone
// last cycle and didn't leave it completely // last cycle and didn't leave it completely
// this cycle: // this cycle:
// decrease priority // decrease priority
if ((p_flags & THPS_LOW) && (gonein || t->inside) if (! SetThreadPriority(t->handle,
&& (p_flags & THPS_BOOSTED)) { THREAD_PRIORITY_NORMAL))
SetThreadPriority(t->handle, THREAD_PRIORITY_NORMAL); Firebird::system_call_failed::raise();
#ifdef THREAD_PSCHED_DEBUG #ifdef THREAD_PSCHED_DEBUG
gds__log("- handle=%p priority=%d", t->handle, THREAD_PRIORITY_NORMAL); gds__log("- handle=%p priority=%d", t->handle, THREAD_PRIORITY_NORMAL);
#endif #endif
t->flags &= ~THPS_BOOSTED; t->flags &= ~THPS_BOOSTED;
continue; continue;
} }
// 4. thread entered single thread zone this cycle: // 4. thread entered single thread zone this cycle:
// candidate for priority decrease // candidate for priority decrease
if (gonein && (p_flags & THPS_BOOSTED)) {
t->flags |= THPS_LOW; t->flags |= THPS_LOW;
continue; continue;
} }
@ -294,46 +299,53 @@ unsigned int __stdcall ThreadPriorityScheduler::Scheduler(LPVOID) {
t->ticks--; t->ticks--;
} }
} }
if (GlobalTicks-- > 0) if (! shutdown) {
continue; if (GlobalTicks-- > 0)
GlobalTicks = THPS_TICKS;
if (StateCloseHandles == 0)
continue;
for (ThreadPriorityScheduler ** pt = &chain; *pt; pt = &(*pt)->next) {
start_label:
if ((*pt)->inside)
continue; continue;
if ((*pt)->ticks > 0) GlobalTicks = THPS_TICKS;
continue;
DWORD ExitCode;
if (! GetExitCodeThread((*pt)->handle, &ExitCode))
#pragma FB_COMPILER_MESSAGE("Fix! Replace with MORE appropriate handling.")
Firebird::memory_corrupt::raise();
if (ExitCode == STILL_ACTIVE) {
(*pt)->ticks = THPS_TICKS;
continue;
}
// thread exited - close handle and do cleanup
if (StateCloseHandles == 1) {
THD_mutex_lock(&mutex);
StateCloseHandles = 2;
}
ThreadPriorityScheduler *m = *pt;
*pt = m->next;
#ifdef THREAD_PSCHED_DEBUG
gds__log("~ handle=%p", m->handle);
#endif
CloseHandle(m->handle);
delete m;
if (*pt)
goto start_label;
else
break;
} }
if (StateCloseHandles == 2)
THD_mutex_unlock(&mutex); if (StateCloseHandles > 0) {
for (ThreadPriorityScheduler ** pt = &chain; *pt; pt = &(*pt)->next) {
start_label:
if ((*pt)->inside)
continue;
if ((*pt)->ticks > 0)
continue;
DWORD ExitCode;
if (! GetExitCodeThread((*pt)->handle, &ExitCode))
Firebird::system_call_failed::raise();
if (ExitCode == STILL_ACTIVE) {
(*pt)->ticks = THPS_TICKS;
continue;
}
// thread exited - close handle and do cleanup
if (StateCloseHandles == 1) {
THD_mutex_lock(&mutex);
StateCloseHandles = 2;
}
ThreadPriorityScheduler *m = *pt;
*pt = m->next;
#ifdef THREAD_PSCHED_DEBUG
gds__log("~ handle=%p", m->handle);
#endif
CloseHandle(m->handle);
delete m;
if (*pt)
goto start_label;
else
break;
}
if (StateCloseHandles == 2)
THD_mutex_unlock(&mutex);
}
if (shutdown && (! chain) && (! news))
break;
} }
// never get // cleanup
delete pool;
THD_mutex_destroy(&mutex);
TlsFree(specific_key);
return 0; return 0;
} }