8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-23 17:23:04 +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 BOOLEAN initialized;
static DWORD specific_key; // for thread LS access
static BOOLEAN shutdown; // server shutting down
ThreadPriorityScheduler * next; // next thread in list
union {
@ -76,6 +77,7 @@ public:
static void Exit(void);
static thdd *Get(void);
static void Set(thdd *val);
static BOOLEAN Boosted(void);
static void Cleanup(void);
static void Init(void);
static void Attach(HANDLE tHandle, DWORD thread_id, int &p);
@ -96,6 +98,7 @@ public:
#define THPS_FINI() ThreadPriorityScheduler::Cleanup()
#define THPS_ATTACH(handle, thread_id, priority) \
ThreadPriorityScheduler::Attach(handle, thread_id, priority)
#define THPS_BOOSTDONE() ThreadPriorityScheduler::Boosted()
#else // THREAD_PSCHED
@ -106,6 +109,7 @@ public:
#define THPS_INIT()
#define THPS_FINI()
#define THPS_ATTACH(handle, thread_id, priority)
#define THPS_BOOSTDONE() FALSE
#endif // THREAD_PSCHED

View File

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