2002-10-25 18:28:17 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: JRD Access Method
|
|
|
|
* MODULE: thd_priority.h
|
|
|
|
* DESCRIPTION: Thread priorities scheduler
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Interbase Public
|
|
|
|
* License Version 1.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy
|
|
|
|
* of the License at http://www.Inprise.com/IPL.html
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an
|
|
|
|
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
|
|
|
|
* or implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* 2002.10.20 Alexander Peshkoff: Created this scheduler, changing
|
|
|
|
* priorities of Win32 threads. to avoid side effects of Windows
|
|
|
|
* native priorities scheduling.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
2003-10-03 03:53:34 +02:00
|
|
|
#ifndef JRD_OS_THD_PRIORITY_H
|
|
|
|
#define JRD_OS_THD_PRIORITY_H
|
2002-10-25 18:28:17 +02:00
|
|
|
|
|
|
|
#ifdef THREAD_PSCHED
|
|
|
|
|
2004-06-08 15:41:08 +02:00
|
|
|
#include "firebird.h"
|
2003-01-16 18:47:10 +01:00
|
|
|
#include "../common/classes/alloc.h"
|
2004-06-08 15:41:08 +02:00
|
|
|
#include "../common/classes/fb_tls.h"
|
|
|
|
#include "../common/classes/init.h"
|
|
|
|
#include "../common/classes/array.h"
|
|
|
|
#include "../jrd/thd.h"
|
2002-10-25 18:28:17 +02:00
|
|
|
#include <process.h>
|
|
|
|
|
2004-06-08 15:41:08 +02:00
|
|
|
// thps_flags values
|
|
|
|
const UCHAR THPS_PSCHED = 1; // thread controlled by priority scheduler
|
|
|
|
const UCHAR THPS_UP = 2; // candidate for priority boost
|
|
|
|
const UCHAR THPS_LOW = 4; // candidate for priority decrement
|
|
|
|
const UCHAR THPS_BOOSTED = 8; // thread priority raised by priority scheduler
|
|
|
|
|
|
|
|
#endif // THREAD_PSCHED
|
|
|
|
|
2002-10-25 18:28:17 +02:00
|
|
|
class ThreadPriorityScheduler {
|
2004-06-08 15:41:08 +02:00
|
|
|
#ifdef THREAD_PSCHED
|
|
|
|
|
2002-10-25 18:28:17 +02:00
|
|
|
private:
|
2004-06-08 15:41:08 +02:00
|
|
|
typedef Firebird::SortedArray<ThreadPriorityScheduler*,
|
|
|
|
Firebird::InlineStorage<ThreadPriorityScheduler*, 16> > TpsPointers;
|
|
|
|
enum OperationMode {Running, Stopping, ShutdownComplete};
|
|
|
|
|
|
|
|
static TLS_DECLARE (ThreadPriorityScheduler*, currentScheduler);
|
|
|
|
static Firebird::Mutex mutex; // locks modification of thps chains
|
2004-02-20 07:43:27 +01:00
|
|
|
static ThreadPriorityScheduler* chain; // where starts thps chain
|
2004-06-08 15:41:08 +02:00
|
|
|
static Firebird::InitMutex<ThreadPriorityScheduler> initialized;
|
|
|
|
static OperationMode opMode; // current mode
|
|
|
|
static TpsPointers* toDetach; // instances to be detached
|
|
|
|
|
2004-07-02 12:47:03 +02:00
|
|
|
ThreadEntryPoint* routine; // real thread entrypoint
|
2004-06-08 15:41:08 +02:00
|
|
|
void* arg; // arg to pass to it
|
|
|
|
ThreadPriorityScheduler* next; // next thread in list
|
|
|
|
HANDLE handle; // thread handle for SetPriority
|
|
|
|
// use separate bytes for flags in order to guarantee there
|
|
|
|
// modification independence by different threads without
|
|
|
|
// affecting each other
|
2002-10-25 18:28:17 +02:00
|
|
|
UCHAR inside; // executing between ENTER and EXIT
|
|
|
|
UCHAR gonein; // pass through ENTER since last scheduling
|
|
|
|
UCHAR flags; // flags that can't be modified concurrently
|
2004-06-08 15:41:08 +02:00
|
|
|
|
|
|
|
// Scheduler Thread
|
|
|
|
static unsigned int __stdcall schedulerMain(LPVOID);
|
|
|
|
|
|
|
|
// Get instance for current thread
|
|
|
|
static ThreadPriorityScheduler *get()
|
|
|
|
{
|
|
|
|
return TLS_GET(currentScheduler);
|
|
|
|
}
|
|
|
|
|
|
|
|
// process instances, stored in toDetach array
|
|
|
|
static void doDetach();
|
|
|
|
// Add current instance to the chain
|
|
|
|
void attach();
|
|
|
|
~ThreadPriorityScheduler() {
|
|
|
|
CloseHandle(handle);
|
|
|
|
}
|
|
|
|
|
2002-10-25 18:28:17 +02:00
|
|
|
public:
|
2004-07-02 12:47:03 +02:00
|
|
|
ThreadPriorityScheduler(ThreadEntryPoint* r, void* a, UCHAR f)
|
2004-06-09 21:02:19 +02:00
|
|
|
: routine(r), arg(a), inside(0), gonein(0), flags(f) {}
|
2004-06-08 15:41:08 +02:00
|
|
|
// Unregister thread from priorities scheduler
|
|
|
|
void detach();
|
2002-10-25 18:28:17 +02:00
|
|
|
|
2004-06-08 15:41:08 +02:00
|
|
|
// Initialize priority scheduler
|
|
|
|
static void init();
|
|
|
|
// Stop priority scheduler
|
|
|
|
static void cleanup();
|
|
|
|
|
|
|
|
// This CAPITAL_letter-starting methods use InitMutex
|
2004-06-09 21:02:19 +02:00
|
|
|
static void Init()
|
2004-06-08 15:41:08 +02:00
|
|
|
{
|
|
|
|
initialized.init();
|
|
|
|
}
|
|
|
|
static void Cleanup(void*);
|
|
|
|
|
|
|
|
// Goes to low priority zone
|
|
|
|
static void enter()
|
|
|
|
{
|
|
|
|
ThreadPriorityScheduler *t = get();
|
|
|
|
t->inside = 1;
|
|
|
|
t->gonein = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Goes from low priority zone
|
|
|
|
static void exit()
|
|
|
|
{
|
|
|
|
ThreadPriorityScheduler *t = get();
|
|
|
|
t->inside = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether current thread has high priority
|
|
|
|
static bool boosted()
|
|
|
|
{
|
|
|
|
return get()->flags & THPS_BOOSTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks, wheather thread's priority 'p'
|
|
|
|
// should be altered in scheduler
|
|
|
|
static UCHAR adjustPriority(int& p)
|
|
|
|
{
|
|
|
|
UCHAR flags = 0;
|
|
|
|
if (p == THREAD_PRIORITY_NORMAL)
|
|
|
|
{
|
|
|
|
flags = THPS_PSCHED | THPS_BOOSTED;
|
|
|
|
p = THREAD_PRIORITY_HIGHEST;
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starts thread under scheduler control
|
|
|
|
void run();
|
2002-10-25 18:28:17 +02:00
|
|
|
|
|
|
|
#else // THREAD_PSCHED
|
|
|
|
|
2004-06-08 15:41:08 +02:00
|
|
|
public:
|
|
|
|
static void enter() {}
|
|
|
|
static void exit() {}
|
|
|
|
static bool boosted()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2002-10-25 18:28:17 +02:00
|
|
|
|
|
|
|
#endif // THREAD_PSCHED
|
2004-06-08 15:41:08 +02:00
|
|
|
};
|
2002-10-25 18:28:17 +02:00
|
|
|
|
2003-10-03 03:53:34 +02:00
|
|
|
#endif // JRD_OS_THD_PRIORITY_H
|
2004-02-20 07:43:27 +01:00
|
|
|
|