diff --git a/builds/posix/Makefile.in.client.util b/builds/posix/Makefile.in.client.util index 8b3a9ffe64..947aaa04a9 100644 --- a/builds/posix/Makefile.in.client.util +++ b/builds/posix/Makefile.in.client.util @@ -60,12 +60,12 @@ CREATEDB_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(CREATEDB_So DROP_Other_Sources = common/utils.cpp lock/lock.cpp jrd/isc_sync.cpp jrd/isc_ipc.cpp jrd/isc.cpp \ -common/classes/fb_string.cpp common/classes/alloc.cpp common/fb_exception.cpp \ common/config/config.cpp common/config/config_file.cpp $(OS_SPECIFIC_Sources) DROP_Files= drop.cpp DROP_Sources = $(addprefix utilities/, $(DROP_Files)) $(DROP_Other_Sources) -DROP_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(DROP_Sources)))) +DROP_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(DROP_Sources)))) \ +$(FBCOMMON_ClientObjects) $(FBCLASSES_ClientObjects) GSTAT_Other_Sources = jrd/btn.cpp jrd/db_alias.cpp jrd/ods.cpp common/utils.cpp \ @@ -81,7 +81,7 @@ common/fb_exception.cpp common/utils.cpp FBSVCMGR_Sources = $(addprefix utilities/, $(FBSVCMGR_Files)) $(FBSVCMGR_Other_Sources) FBSVCMGR_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(FBSVCMGR_Sources)))) \ -$(FBCLASSES_MsgObjects) $(CLUMPLETS_Objects) +$(FBCLASSES_MsgObjects) $(CLUMPLETS_Objects) $(FBCOMMON_ClientObjects) FBGUARD_Other_Sources = jrd/isc.cpp jrd/divorce.cpp jrd/path_utils.cpp diff --git a/builds/posix/Makefile.in.embed.util b/builds/posix/Makefile.in.embed.util index b5304b26c6..1372b5d7da 100644 --- a/builds/posix/Makefile.in.embed.util +++ b/builds/posix/Makefile.in.embed.util @@ -67,7 +67,7 @@ common/config/config.cpp common/config/config_file.cpp $(OS_SPECIFIC_Sources) DROP_Files= drop.cpp DROP_Sources = $(addprefix utilities/, $(DROP_Files)) $(DROP_Other_Sources) -DROP_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(DROP_Sources)))) +DROP_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(DROP_Sources)))) $(FBCOMMON_ClientObjects) GSEC_Other_Sources = jrd/enc.cpp jrd/sha.cpp jrd/guid.cpp \ @@ -95,7 +95,7 @@ common/fb_exception.cpp common/utils.cpp FBSVCMGR_Sources = $(addprefix utilities/, $(FBSVCMGR_Files)) $(FBSVCMGR_Other_Sources) FBSVCMGR_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(FBSVCMGR_Sources)))) \ -$(FBCLASSES_MsgObjects) $(CLUMPLETS_Objects) +$(FBCLASSES_MsgObjects) $(CLUMPLETS_Objects) $(FBCOMMON_ClientObjects) IBGUARD_Files = guard.cpp util.cpp diff --git a/builds/posix/make.shared.variables b/builds/posix/make.shared.variables index 4a7deadd3e..c2353f4530 100644 --- a/builds/posix/make.shared.variables +++ b/builds/posix/make.shared.variables @@ -17,7 +17,7 @@ WHY_Sources = why.cpp JRD_ClientFiles = alt.cpp cvt.cpp db_alias.cpp dsc.cpp \ enc.cpp gds.cpp isc.cpp isc_file.cpp isc_ipc.cpp \ isc_sync.cpp perf.cpp sch.cpp sdl.cpp status.cpp \ - thd.cpp ThreadData.cpp ThreadStart.cpp utl.cpp \ + ThreadData.cpp ThreadStart.cpp utl.cpp \ $(WHY_Sources) # These are in the win32 release but not unix one for super? @@ -240,7 +240,7 @@ VULCAN_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(VULCAN_Source # # common -FBCOMMON_ClientFiles = fb_exception.cpp classes/MetaName.cpp StatusHolder.cpp +FBCOMMON_ClientFiles = fb_exception.cpp thd.cpp classes/MetaName.cpp StatusHolder.cpp classes/init.cpp FBCOMMON_ServerFiles = utils.cpp FBCOMMON_ClientObjects = $(addprefix $(OBJ)/common/, $(addsuffix .o, $(basename $(FBCOMMON_ClientFiles)))) diff --git a/src/alice/alice.cpp b/src/alice/alice.cpp index 39707c3577..a9f39dd185 100644 --- a/src/alice/alice.cpp +++ b/src/alice/alice.cpp @@ -52,7 +52,6 @@ #include "../jrd/gds_proto.h" #include "../jrd/svc.h" #include "../jrd/svc_proto.h" -#include "../jrd/thd.h" #include "../alice/alice_proto.h" #include "../common/utils_proto.h" diff --git a/src/alice/alice_meta.epp b/src/alice/alice_meta.epp index c2a322a09c..ac1ab84198 100644 --- a/src/alice/alice_meta.epp +++ b/src/alice/alice_meta.epp @@ -38,7 +38,6 @@ #include "../alice/all.h" #include "../alice/alice_meta.h" #include "../jrd/gds_proto.h" -#include "../jrd/thd.h" #include "../include/fb_exception.h" #include "../common/classes/alloc.h" #include "../alice/alice_proto.h" diff --git a/src/alice/all.cpp b/src/alice/all.cpp index 60c4291858..1b9fbe1487 100644 --- a/src/alice/all.cpp +++ b/src/alice/all.cpp @@ -24,14 +24,13 @@ // //____________________________________________________________ // -// $Id: all.cpp,v 1.29 2004-09-01 14:51:33 alexpeshkoff Exp $ +// $Id: all.cpp,v 1.30 2008-01-23 15:51:31 alexpeshkoff Exp $ // #include "firebird.h" #include "../jrd/common.h" #include "../alice/all.h" #include "../alice/alice.h" -#include "../jrd/thd.h" #include "../common/classes/alloc.h" AliceMemoryPool* AliceMemoryPool::createPool() { diff --git a/src/alice/exe.cpp b/src/alice/exe.cpp index 055d0aafd8..7bdb056602 100644 --- a/src/alice/exe.cpp +++ b/src/alice/exe.cpp @@ -46,7 +46,6 @@ #include "../alice/alice_meta.h" #include "../alice/tdr_proto.h" #include "../jrd/gds_proto.h" -#include "../jrd/thd.h" #include "../jrd/constants.h" #include "../common/classes/ClumpletWriter.h" diff --git a/src/alice/tdr.cpp b/src/alice/tdr.cpp index 2beea3f4ea..ed5a824711 100644 --- a/src/alice/tdr.cpp +++ b/src/alice/tdr.cpp @@ -46,7 +46,6 @@ #include "../jrd/gds_proto.h" #include "../jrd/isc_proto.h" #include "../jrd/svc_proto.h" -#include "../jrd/thd.h" #include "../jrd/constants.h" #include "../common/classes/ClumpletWriter.h" diff --git a/src/burp/backup.epp b/src/burp/backup.epp index e48458c924..c8178c7f05 100644 --- a/src/burp/backup.epp +++ b/src/burp/backup.epp @@ -46,7 +46,6 @@ #include "../jrd/ods.h" #include "../jrd/align.h" #include "../jrd/gdsassert.h" -#include "../jrd/thd.h" #include "../jrd/constants.h" #include "../common/stuff.h" #include "../burp/backu_proto.h" diff --git a/src/burp/burp.cpp b/src/burp/burp.cpp index b6dbbade18..3988d11bf5 100644 --- a/src/burp/burp.cpp +++ b/src/burp/burp.cpp @@ -40,7 +40,6 @@ #include #include "../jrd/ibsetjmp.h" #include "../jrd/msg_encode.h" -#include "../jrd/thd.h" #include "../jrd/ods.h" // to get MAX_PAGE_SIZE #include "../jrd/svc.h" #include "../jrd/constants.h" diff --git a/src/burp/mvol.cpp b/src/burp/mvol.cpp index 158978f06d..6ead5c43be 100644 --- a/src/burp/mvol.cpp +++ b/src/burp/mvol.cpp @@ -44,7 +44,6 @@ #include "../burp/mvol_proto.h" #include "../jrd/gds_proto.h" #include "../jrd/gdsassert.h" -#include "../jrd/thd.h" #include #include diff --git a/src/burp/restore.epp b/src/burp/restore.epp index 0fd54a88aa..3cdc9255c4 100644 --- a/src/burp/restore.epp +++ b/src/burp/restore.epp @@ -37,7 +37,6 @@ #include "../jrd/license.h" #include "../jrd/obj.h" #include "../jrd/ods.h" -#include "../jrd/thd.h" #include "../common/stuff.h" #include "../burp/burp_proto.h" #include "../burp/canon_proto.h" diff --git a/src/common/classes/alloc.cpp b/src/common/classes/alloc.cpp index 0afbbf31af..5df97e93f3 100644 --- a/src/common/classes/alloc.cpp +++ b/src/common/classes/alloc.cpp @@ -48,10 +48,15 @@ #define FREE_PATTERN 0xDEADBEEF #define ALLOC_PATTERN 0xFEEDABED #ifdef DEBUG_GDS_ALLOC -# define PATTERN_FILL(ptr, size, pattern) for (size_t _i = 0; _i < size / sizeof(unsigned int); _i++) \ - ((unsigned int*)(ptr))[_i] = (pattern) +inline void PATTERN_FILL(void *ptr, size_t size, unsigned int pattern) +{ + for (size_t i = 0; i < size / sizeof(unsigned int); i++) + { + ((unsigned int*)ptr)[i] = pattern; + } +} #else -# define PATTERN_FILL(ptr, size, pattern) ((void)0) +inline void PATTERN_FILL(void *, size_t, unsigned int) { } #endif // TODO (in order of importance): @@ -158,8 +163,8 @@ int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */ // Extents cache is not used when DEBUG_GDS_ALLOC or USE_VALGRIND is enabled. // This slows down things a little due to frequent syscalls mapping/unmapping // memory but allows to detect more allocation errors -Firebird::Vector extents_cache; -Mutex cache_mutex; +Vector extents_cache; +Mutex* cache_mutex; // Will be initialized manually in MemoryPool::init // avoid races during initialization size_t map_page_size = 0; @@ -240,28 +245,48 @@ MemoryPool* MemoryPool::getContextPool() return TLS_GET(contextPool); } -// Initialize default stats group +// Default stats group and default pool MemoryStats* MemoryPool::default_stats_group = 0; +MemoryPool* MemoryPool::processMemoryPool = 0; -// Initialize process memory pool to avoid possible race conditions. -// At this point also set contextMemoryPool for main thread (or all -// process in case of no threading). -namespace { - char msBuffer[sizeof(MemoryStats) + ALLOC_ALIGNMENT]; - MemoryPool* createProcessMemoryPool() - { - MemoryPool::default_stats_group = - new((void*)(IPTR) MEM_ALIGN((size_t)(IPTR) msBuffer)) MemoryStats; - MemoryPool* p = MemoryPool::createPool(); - fb_assert(p); -#ifndef SUPERCLIENT - MemoryPool::setContextPool(p); +// Initialize process memory pool (called from InstanceControl). +// At this point also set contextMemoryPool for main thread +// (or all process in case of no threading). + +void MemoryPool::init() +{ +#if defined(WIN_NT) || defined(HAVE_MMAP) + static char mtxBuffer[sizeof(Mutex) + ALLOC_ALIGNMENT]; + cache_mutex = + new((void*)(IPTR) MEM_ALIGN((size_t)(IPTR) mtxBuffer)) Mutex; #endif - return p; - } -} // anonymous namespace -MemoryPool* MemoryPool::processMemoryPool = createProcessMemoryPool(); + static char msBuffer[sizeof(MemoryStats) + ALLOC_ALIGNMENT]; + MemoryPool::default_stats_group = + new((void*)(IPTR) MEM_ALIGN((size_t)(IPTR) msBuffer)) MemoryStats; + + // Now it's safe to actually create MemoryPool + processMemoryPool = MemoryPool::createPool(); + fb_assert(processMemoryPool); + +#ifndef SUPERCLIENT + MemoryPool::setContextPool(processMemoryPool); +#endif +} + +// Should be last routine, called by InstanceControl, +// being therefore the very last routine in firebird module. + +void MemoryPool::cleanup() +{ + deletePool(processMemoryPool); + processMemoryPool = 0; + default_stats_group = 0; + +#if defined(WIN_NT) || defined(HAVE_MMAP) + cache_mutex->~Mutex(); +#endif +} void MemoryPool::setStatsGroup(MemoryStats& statsL) { @@ -374,7 +399,7 @@ void MemoryPool::external_free(void *blk, size_t &size, bool pool_destroying) // In normal case all blocks pass through queue of sufficent length by themselves if (pool_destroying) { // Synchronize delayed free queue using extents mutex - cache_mutex.enter(); + MutexLockGuard guard(*cache_mutex); // Extend circular buffer if possible if (delayedExtentCount < FB_NELEM(delayedExtents)) { @@ -383,7 +408,6 @@ void MemoryPool::external_free(void *blk, size_t &size, bool pool_destroying) item->size = size; item->handle = handle; delayedExtentCount++; - cache_mutex.leave(); return; } @@ -405,8 +429,6 @@ void MemoryPool::external_free(void *blk, size_t &size, bool pool_destroying) delayedExtentsPos++; if (delayedExtentsPos >= FB_NELEM(delayedExtents)) delayedExtentsPos = 0; - - cache_mutex.leave(); } else { // Let Valgrind forget about unmapped block @@ -424,14 +446,13 @@ void* MemoryPool::external_alloc(size_t &size) // This method is assumed to return NULL in case it cannot alloc # if !defined(DEBUG_GDS_ALLOC) && (defined(WIN_NT) || defined(HAVE_MMAP)) if (size == EXTENT_SIZE) { - cache_mutex.enter(); + MutexLockGuard guard(cache_mutex); void *result = NULL; if (extents_cache.getCount()) { // Use most recently used object to encourage caching result = extents_cache[extents_cache.getCount() - 1]; extents_cache.shrink(extents_cache.getCount() - 1); } - cache_mutex.leave(); if (result) { return result; } @@ -494,13 +515,11 @@ void* MemoryPool::external_alloc(size_t &size) void MemoryPool::external_free(void *blk, size_t &size, bool pool_destroying) { # if !defined(DEBUG_GDS_ALLOC) && (defined(WIN_NT) || defined(HAVE_MMAP)) if (size == EXTENT_SIZE) { - cache_mutex.enter(); + MutexLockGuard guard(cache_mutex); if (extents_cache.getCount() < extents_cache.getCapacity()) { extents_cache.add(blk); - cache_mutex.leave(); return; } - cache_mutex.leave(); } # endif # if defined WIN_NT diff --git a/src/common/classes/alloc.h b/src/common/classes/alloc.h index ec7cc7eb46..17f526548c 100644 --- a/src/common/classes/alloc.h +++ b/src/common/classes/alloc.h @@ -262,7 +262,7 @@ private: void removeFreeBlock(MemoryBlock* blk); void free_blk_extent(MemoryBlock* blk); - + // Allocates small block from this pool. Pool must be locked during call void* internal_alloc(size_t size, SSHORT type = 0 #ifdef DEBUG_GDS_ALLOC @@ -370,6 +370,10 @@ public: return result; } + // Initialize and finalize global memory pool + static void init(); + static void cleanup(); + /// Returns the type associated with the allocated memory. static SSHORT blk_type(const void* mem) { return ((MemoryBlock*)((char *)mem - MEM_ALIGN(sizeof(MemoryBlock))))->mbk_type; diff --git a/src/common/classes/init.h b/src/common/classes/init.h index 1a294a0591..285b8f1ce0 100644 --- a/src/common/classes/init.h +++ b/src/common/classes/init.h @@ -1,5 +1,5 @@ /* - * PROGRAM: JRD Access Method + * PROGRAM: Common Access Method * MODULE: init.h * DESCRIPTION: InitMutex, InitInstance - templates to help with initialization * @@ -27,46 +27,118 @@ #ifndef CLASSES_INIT_INSTANCE_H #define CLASSES_INIT_INSTANCE_H +#include "fb_types.h" +#include "../common/classes/alloc.h" + namespace Firebird { +// InstanceControl - interface for almost all global variables + +class InstanceControl +{ +public: + InstanceControl(); + static void destructors(); + static void registerGdsCleanup(FPTR_VOID cleanup); +protected: + virtual void dtor() = 0; +private: + static InstanceControl* instanceList; + static FPTR_VOID gdsCleanup; + InstanceControl* next; +}; + +// GlobalPtr - template to help declaring global varables + +template +class GlobalPtr : private InstanceControl +{ +private: + T* instance; + void dtor() + { + delete instance; + instance = 0; + } +public: + GlobalPtr() + : InstanceControl() + { + instance = FB_NEW(*getDefaultMemoryPool()) T(*getDefaultMemoryPool()); + // This means - for objects with ctors/dtors that want to ;be global, + // provide ctor with MemoryPool& parameter. Even if it is ignored! + } + T* operator->() const throw() + { + return instance; + } + operator T&() const throw() + { + return *instance; + } + T* operator&() const throw() + { + return instance; + } + bool operator!() const throw() + { + return instance ? false : true; + } +}; + +// Support for common mutex for various inits + +class StaticMutex +{ +protected: + static RecursiveMutex* mutex; +public: + static void create(); + static void release(); +}; + // InitMutex - executes static void C::init() once and only once template -class InitMutex : private Mutex { +class InitMutex : private StaticMutex +{ private: volatile bool flag; public: - InitMutex() : flag(false) { } - void init() { + InitMutex() + : flag(false) { } + void init() + { if (!flag) { try { - enter(); + mutex->enter(); if (!flag) { C::init(); flag = true; } } catch (const Firebird::Exception&) { - leave(); + mutex->leave(); throw; } - leave(); + mutex->leave(); } } - void cleanup() { + void cleanup() + { if (flag) { try { - enter(); + mutex->enter(); if (flag) { C::cleanup(); flag = false; } } catch (const Firebird::Exception&) { - leave(); + mutex->leave(); throw; } - leave(); + mutex->leave(); } } }; @@ -75,35 +147,40 @@ public: // DefaultInit uses default memory pool for it. template -class DefaultInit { +class DefaultInit +{ public: - static T* init() { + static T* init() + { return FB_NEW(*getDefaultMemoryPool()) T(*getDefaultMemoryPool()); } }; template > -class InitInstance : private Mutex { + typename I = DefaultInit > +class InitInstance : private StaticMutex +{ private: T* instance; volatile bool flag; public: - InitInstance() : flag(false) { } - T& operator()() { + InitInstance() + : flag(false) { } + T& operator()() + { if (!flag) { try { - enter(); + mutex->enter(); if (!flag) { - instance = C::init(); + instance = I::init(); flag = true; } } catch (const Firebird::Exception&) { - leave(); + mutex->leave(); throw; } - leave(); + mutex->leave(); } return *instance; } diff --git a/src/common/classes/locks.cpp b/src/common/classes/locks.cpp index b790a26066..359cec05f8 100644 --- a/src/common/classes/locks.cpp +++ b/src/common/classes/locks.cpp @@ -27,6 +27,8 @@ #include "../../include/firebird.h" #include "../../common/classes/locks.h" +#include "../../common/thd.h" +#include "../../jrd/common.h" namespace Firebird { @@ -38,7 +40,7 @@ namespace Firebird { tSetCriticalSectionSpinCount* Spinlock::SetCriticalSectionSpinCount = INIT_SPIN_COUNT; -Spinlock::Spinlock() { +Spinlock::init() { if (SetCriticalSectionSpinCount == MISS_SPIN_COUNT) return; if (SetCriticalSectionSpinCount == INIT_SPIN_COUNT) { @@ -60,4 +62,54 @@ Spinlock::Spinlock() { #endif // WIN_NT + +// in some cases recursive mutex's are not protected by try/catch +// therefore keep old (return value) logic + +int RecursiveMutex::enter() +{ + if (threadId == getThreadId()) + { + ++count; + } + else + { + try + { + mutex.enter(); + } + catch (const Firebird::system_call_failed& e) + { + return e.getErrorCode(); + } + threadId = getThreadId(); + count = 1; + } + return 0; +} + + +int RecursiveMutex::leave() +{ + if (threadId != getThreadId()) + { + return FB_FAILURE; + } + + if (--count == 0) + { + threadId = 0; + try + { + mutex.leave(); + } + catch (const Firebird::system_call_failed& e) + { + return e.getErrorCode(); + } + } + return 0; +} + + } // namespace Firebird diff --git a/src/common/classes/locks.h b/src/common/classes/locks.h index 4ff379dc52..02df64c4ad 100644 --- a/src/common/classes/locks.h +++ b/src/common/classes/locks.h @@ -46,6 +46,8 @@ namespace Firebird { +class MemoryPool; // Needed for ctors that must always ignaore it + #ifdef WIN_NT // Generic process-local mutex and spinlock. The latter @@ -60,6 +62,9 @@ public: Mutex() { InitializeCriticalSection(&spinlock); } + explicit Mutex(MemoryPool&) { + InitializeCriticalSection(&spinlock); + } ~Mutex() { DeleteCriticalSection(&spinlock); } @@ -79,8 +84,16 @@ typedef WINBASEAPI DWORD WINAPI tSetCriticalSectionSpinCount ( class Spinlock : public Mutex { private: static tSetCriticalSectionSpinCount* SetCriticalSectionSpinCount; + init(); public: - Spinlock(); + Spinlock() + { + init(); + } + explicit Spinlock(MemoryPool&) + { + init(); + } }; #else //WIN_NT @@ -95,6 +108,10 @@ public: if (mutex_init(&mlock, USYNC_PROCESS, NULL)) system_call_failed::raise("mutex_init"); } + explicit Mutex(MemoryPool&) { + if (mutex_init(&mlock, USYNC_PROCESS, NULL)) + system_call_failed::raise("mutex_init"); + } ~Mutex() { if (mutex_destroy(&mlock)) system_call_failed::raise("mutex_destroy"); @@ -122,6 +139,10 @@ public: if (pthread_mutex_init(&mlock, 0)) system_call_failed::raise("pthread_mutex_init"); } + explicit Mutex(MemoryPool&) { + if (pthread_mutex_init(&mlock, 0)) + system_call_failed::raise("pthread_mutex_init"); + } ~Mutex() { if (pthread_mutex_destroy(&mlock)) system_call_failed::raise("pthread_mutex_destroy"); @@ -145,6 +166,10 @@ public: if (pthread_spin_init(&spinlock, false)) system_call_failed::raise("pthread_spin_init"); } + explicit Spinlock(MemoryPool&) { + if (pthread_spin_init(&spinlock, false)) + system_call_failed::raise("pthread_spin_init"); + } ~Spinlock() { if (pthread_spin_destroy(&spinlock)) system_call_failed::raise("pthread_spin_destroy"); @@ -176,6 +201,25 @@ private: Mutex *lock; }; +// Recursive mutex +class RecursiveMutex { + Firebird::Mutex mutex; + FB_THREAD_ID threadId; + int count; + +public: + RecursiveMutex() + : mutex(), threadId(0), count(0) + {} + + RecursiveMutex(class Firebird::MemoryPool&) + : mutex(), threadId(0), count(0) + {} + + int enter(); + int leave(); +}; + } //namespace Firebird #endif // CLASSES_LOCKS_H diff --git a/src/common/classes/rwlock.h b/src/common/classes/rwlock.h index 3b8f480d85..ce2a23ff27 100644 --- a/src/common/classes/rwlock.h +++ b/src/common/classes/rwlock.h @@ -56,12 +56,11 @@ private: Mutex blockedReadersLock; HANDLE writers_event, readers_semaphore; - // Forbid copy constructor - RWLock(const RWLock& source); - -public: - RWLock() : lock(0), blockedReaders(0), blockedWriters(0) + void init() { + lock = 0; + blockedReaders = 0; + blockedWriters = 0; readers_semaphore = CreateSemaphore(NULL, 0 /*initial count*/, INT_MAX, NULL); if (readers_semaphore == NULL) @@ -70,6 +69,13 @@ public: if (writers_event == NULL) system_call_failed::raise("CreateEvent"); } + + // Forbid copy constructor + RWLock(const RWLock& source); + +public: + RWLock() { init(); } + RWLock(Firebird::MemoryPool&) { init(); } ~RWLock() { if (readers_semaphore && !CloseHandle(readers_semaphore)) @@ -179,14 +185,18 @@ private: rwlock_t lock; // Forbid copy constructor RWLock(const RWLock& source); -public: - RWLock() - { + + void init() + { if (rwlock_init(&lock, USYNC_PROCESS, NULL)) { system_call_failed::raise("rwlock_init"); } } + +public: + RWLock() { init(); } + RWLock(Firebird::MemoryPool&) { init(); } ~RWLock() { if (rwlock_destroy(&lock)) @@ -245,14 +255,17 @@ public: namespace Firebird { +class MemoryPool; + class RWLock { private: pthread_rwlock_t lock; // Forbid copy constructor RWLock(const RWLock& source); -public: - RWLock() { + + void init() + { #if defined(LINUX) && !defined(USE_VALGRIND) pthread_rwlockattr_t attr; if (pthread_rwlockattr_init(&attr)) @@ -268,6 +281,10 @@ public: system_call_failed::raise("pthread_rwlock_init"); #endif } + +public: + RWLock() { init(); } + RWLock(class MemoryPool&) { init(); } ~RWLock() { if (pthread_rwlock_destroy(&lock)) diff --git a/src/common/classes/semaphore.h b/src/common/classes/semaphore.h index fe254d1604..5a8ce117d1 100644 --- a/src/common/classes/semaphore.h +++ b/src/common/classes/semaphore.h @@ -40,27 +40,34 @@ namespace Firebird { +class MemoryPool; + class Semaphore { private: HANDLE hSemaphore; -public: - Semaphore() + void init() { hSemaphore = CreateSemaphore(NULL, 0 /*initial count*/, INT_MAX, NULL); if (hSemaphore == NULL) system_call_failed::raise("CreateSemaphore"); } + +public: + Semaphore() { init(); } + explicit Semaphore(class MemoryPool&) { init(); } + ~Semaphore() { if (hSemaphore && !CloseHandle(hSemaphore)) system_call_failed::raise("CloseHandle"); } - bool tryEnter(int seconds = 0) + bool tryEnter(int seconds = 0, int milliseconds = 0) { + milliseconds += seconds * 1000; DWORD result = WaitForSingleObject( - hSemaphore, seconds >= 0 ? seconds * 1000 : INFINITE); + hSemaphore, milliseconds >= 0 ? milliseconds : INFINITE); if (result == WAIT_FAILED) system_call_failed::raise("WaitForSingleObject"); return result != WAIT_TIMEOUT; @@ -111,13 +118,8 @@ private: SignalSafeSemaphore* next; #endif #endif // WORKING_SEM_INIT - bool init; -public: -#ifdef MIXED_SEMAPHORE_AND_FILE_HANDLE - static bool checkHandle(int n); -#endif - SignalSafeSemaphore() - : init(false) + + void init() { #ifdef WORKING_SEM_INIT if (sem_init(sem, 0, 0) == -1) { @@ -137,12 +139,18 @@ public: linkToInitialList(); #endif #endif - init = true; } +public: + SignalSafeSemaphore() { init(); } + explicit SignalSafeSemaphore(class MemoryPool&) { init(); } + +#ifdef MIXED_SEMAPHORE_AND_FILE_HANDLE + static bool checkHandle(int n); +#endif + ~SignalSafeSemaphore() { - fb_assert(init == true); #ifdef WORKING_SEM_INIT if (sem_destroy(sem) == -1) { system_call_failed::raise("sem_destroy"); @@ -152,13 +160,10 @@ public: system_call_failed::raise("sem_close"); } #endif - init = false; - } void enter() { - fb_assert(init == true); do { if (sem_wait(sem) != -1) return; @@ -168,7 +173,6 @@ public: void release(SLONG count = 1) { - fb_assert(init == true); for (int i = 0; i < count; i++) { if (sem_post(sem) == -1) @@ -181,11 +185,11 @@ public: #ifdef HAVE_SEM_TIMEDWAIT // In case when sem_timedwait() is implemented by host OS, // class SignalSafeSemaphore may have this function: - bool tryEnter(int seconds = 0) + bool tryEnter(int seconds = 0, int milliseconds = 0) { + milliseconds += seconds * 1000; // Return true in case of success - fb_assert(init == true); - if (seconds == 0) + if (milliseconds == 0) { // Instant try do { @@ -196,7 +200,7 @@ public: return false; system_call_failed::raise("sem_trywait"); } - if (seconds < 0) + if (milliseconds < 0) { // Unlimited wait, like enter() do { @@ -207,8 +211,8 @@ public: } // Wait with timeout struct timespec timeout; - timeout.tv_sec = time(NULL) + seconds; - timeout.tv_nsec = 0; + timeout.tv_sec = time(NULL) + milliseconds / 1000; + timeout.tv_nsec = (milliseconds % 1000) * 1000; int errcode = 0; do { int rc = sem_timedwait(sem, &timeout); @@ -260,10 +264,10 @@ private: struct semid_ds* buf; unsigned short* array; }; -public: - Semaphore() - : semId(semget(IPC_PRIVATE, 1, 0600)) + + void init() { + semId = semget(IPC_PRIVATE, 1, 0600); if (semId < 0) system_call_failed::raise("semaphore.h: Semaphore: semget()"); semun arg; @@ -272,6 +276,10 @@ public: system_call_failed::raise("semaphore.h: Semaphore: semctl()"); } +public: + Semaphore() { init(); } + explicit Semaphore(class MemoryPool&) { init(); } + ~Semaphore() { semun arg; @@ -279,11 +287,12 @@ public: system_call_failed::raise("semaphore.h: ~Semaphore: semctl()"); } - bool tryEnter(int seconds = 0) // Returns true in case of success + bool tryEnter(int seconds = 0, int milliseconds = 0) // Returns true in case of success { + milliseconds += seconds * 1000; timespec timeout; - timeout.tv_sec = time(NULL) + seconds; - timeout.tv_nsec = 0; + timeout.tv_sec = time(NULL) + milliseconds / 1000; + timeout.tv_nsec = (milliseconds % 1000) * 1000; timespec* t = &timeout; sembuf sb; @@ -291,11 +300,11 @@ public: sb.sem_op = -1; sb.sem_flg = 0; - if (seconds < 0) { + if (milliseconds < 0) { // Unlimited wait t = 0; } - else if (seconds == 0) { + else if (milliseconds == 0) { // just try t = 0; sb.sem_flg = IPC_NOWAIT; @@ -358,10 +367,8 @@ class Semaphore private: pthread_mutex_t mu; pthread_cond_t cv; - bool init; -public: - Semaphore() - : init(false) + + void init() { int err = pthread_mutex_init(&mu, NULL); if (err != 0) { @@ -373,12 +380,14 @@ public: //gds__log("Error on semaphore.h: constructor"); system_call_failed::raise("pthread_cond_init", err); } - init = true; } +public: + Semaphore() { init(); } + explicit Semaphore(class MemoryPool&) { init(); } + ~Semaphore() { - fb_assert(init == true); int err = pthread_mutex_destroy(&mu); if (err != 0) { //gds__log("Error on semaphore.h: destructor"); @@ -389,21 +398,19 @@ public: //gds__log("Error on semaphore.h: destructor"); //system_call_failed::raise("pthread_cond_destroy", err); } - - init = false; } - bool tryEnter(int seconds = 0) + bool tryEnter(int seconds = 0, int milliseconds = 0) { bool rt = false; + // Return true in case of success int err2 = 0; int err = 0; - // Return true in case of success - fb_assert(init == true); - if (seconds == 0) + milliseconds += seconds * 1000; + + if (milliseconds == 0) { // Instant try - err2 = pthread_mutex_trylock(&mu); if (err2 == 0) { @@ -427,7 +434,7 @@ public: system_call_failed::raise("pthread_mutex_trylock", err2); } - if (seconds < 0) + if (milliseconds < 0) { // Unlimited wait, like enter() err2 = pthread_mutex_lock(&mu); @@ -451,12 +458,12 @@ public: return false; system_call_failed::raise("pthread_mutex_lock", err2); - } //seconds < 0 + } // Wait with timeout timespec timeout; - timeout.tv_sec = time(NULL) + seconds; - timeout.tv_nsec = 0; + timeout.tv_sec = time(NULL) + milliseconds / 1000; + timeout.tv_nsec = (milliseconds % 1000) * 1000; err2 = pthread_mutex_lock(&mu); if (err2 == 0) @@ -485,7 +492,6 @@ public: void enter() { - fb_assert(init == true); int err = 0; int err2 = pthread_mutex_lock(&mu); if (err2 == 0) @@ -506,7 +512,6 @@ public: void release(SLONG count = 1) { int err = 0; - fb_assert(init == true); for (int i = 0; i < count; i++) { err = pthread_mutex_lock(&mu) ; diff --git a/src/common/classes/tree.h b/src/common/classes/tree.h index 8c7736efb8..ca76dcf6de 100644 --- a/src/common/classes/tree.h +++ b/src/common/classes/tree.h @@ -109,6 +109,10 @@ public: : pool(_pool), level(0), root(NULL), defaultAccessor(this) { } + BePlusTree(Allocator& _pool) + : pool(&_pool), level(0), root(NULL), defaultAccessor(this) + { } + BePlusTree(Allocator *_pool, const BePlusTree& from) : pool(_pool), level(0), root(NULL), defaultAccessor(this) { diff --git a/src/common/config/config.cpp b/src/common/config/config.cpp index 5066dc4715..c6fe7657a0 100644 --- a/src/common/config/config.cpp +++ b/src/common/config/config.cpp @@ -25,6 +25,7 @@ #include "../../common/config/config.h" #include "../../common/config/config_impl.h" #include "../../common/config/config_file.h" +#include "../../common/classes/init.h" #ifdef HAVE_STDLIB_H #include @@ -123,31 +124,7 @@ const ConfigImpl::ConfigEntry ConfigImpl::entries[] = * Static instance of the system configuration file */ -// was: const static ConfigImpl sysConfig; - -static ConfigImpl *sys_config = NULL; -static Firebird::Mutex config_init_lock; - -const ConfigImpl& ConfigImpl::instance() -{ - if (!sys_config) - { - try { - config_init_lock.enter(); - if (!sys_config) { - sys_config = FB_NEW(*getDefaultMemoryPool()) ConfigImpl(*getDefaultMemoryPool()); - } - } - catch (const Firebird::Exception&) { - config_init_lock.leave(); - throw; - } - config_init_lock.leave(); - } - return *sys_config; -} - -#define sysConfig ConfigImpl::instance() +static Firebird::InitInstance sysConfig; /****************************************************************************** * @@ -255,7 +232,7 @@ const char* ConfigImpl::asString(const string &value) const char* Config::getInstallDirectory() { - return sysConfig.getInstallDirectory(); + return sysConfig().getInstallDirectory(); } static Firebird::PathName* rootFromCommandLine = 0; @@ -280,44 +257,44 @@ const char* Config::getRootDirectory() return rootFromCommandLine->c_str(); } - const char* result = (char*) sysConfig.values[KEY_ROOT_DIRECTORY]; - return result ? result : sysConfig.root_dir; + const char* result = (char*) sysConfig().values[KEY_ROOT_DIRECTORY]; + return result ? result : sysConfig().root_dir; } int Config::getTempBlockSize() { - return (int) sysConfig.values[KEY_TEMP_BLOCK_SIZE]; + return (int) sysConfig().values[KEY_TEMP_BLOCK_SIZE]; } int Config::getTempCacheLimit() { - int v = (int) sysConfig.values[KEY_TEMP_CACHE_LIMIT]; + int v = (int) sysConfig().values[KEY_TEMP_CACHE_LIMIT]; return v < 0 ? 0 : v; } bool Config::getRemoteFileOpenAbility() { - return (bool) sysConfig.values[KEY_REMOTE_FILE_OPEN_ABILITY]; + return (bool) sysConfig().values[KEY_REMOTE_FILE_OPEN_ABILITY]; } int Config::getGuardianOption() { - return (int) sysConfig.values[KEY_GUARDIAN_OPTION]; + return (int) sysConfig().values[KEY_GUARDIAN_OPTION]; } int Config::getCpuAffinityMask() { - return (int) sysConfig.values[KEY_CPU_AFFINITY_MASK]; + return (int) sysConfig().values[KEY_CPU_AFFINITY_MASK]; } bool Config::getOldParameterOrdering() { - return (bool) sysConfig.values[KEY_OLD_PARAMETER_ORDERING]; + return (bool) sysConfig().values[KEY_OLD_PARAMETER_ORDERING]; } int Config::getTcpRemoteBufferSize() { - int rc = (int) sysConfig.values[KEY_TCP_REMOTE_BUFFER_SIZE]; + int rc = (int) sysConfig().values[KEY_TCP_REMOTE_BUFFER_SIZE]; if (rc < 1448) rc = 1448; if (rc > MAX_SSHORT) @@ -327,57 +304,57 @@ int Config::getTcpRemoteBufferSize() bool Config::getTcpNoNagle() { - return (bool) sysConfig.values[KEY_TCP_NO_NAGLE]; + return (bool) sysConfig().values[KEY_TCP_NO_NAGLE]; } int Config::getDefaultDbCachePages() { - return (int) sysConfig.values[KEY_DEFAULT_DB_CACHE_PAGES]; + return (int) sysConfig().values[KEY_DEFAULT_DB_CACHE_PAGES]; } int Config::getConnectionTimeout() { - return (int) sysConfig.values[KEY_CONNECTION_TIMEOUT]; + return (int) sysConfig().values[KEY_CONNECTION_TIMEOUT]; } int Config::getDummyPacketInterval() { - return (int) sysConfig.values[KEY_DUMMY_PACKET_INTERVAL]; + return (int) sysConfig().values[KEY_DUMMY_PACKET_INTERVAL]; } int Config::getLockMemSize() { - return (int) sysConfig.values[KEY_LOCK_MEM_SIZE]; + return (int) sysConfig().values[KEY_LOCK_MEM_SIZE]; } bool Config::getLockGrantOrder() { - return (bool) sysConfig.values[KEY_LOCK_GRANT_ORDER]; + return (bool) sysConfig().values[KEY_LOCK_GRANT_ORDER]; } int Config::getLockHashSlots() { - return (int) sysConfig.values[KEY_LOCK_HASH_SLOTS]; + return (int) sysConfig().values[KEY_LOCK_HASH_SLOTS]; } int Config::getLockAcquireSpins() { - return (int) sysConfig.values[KEY_LOCK_ACQUIRE_SPINS]; + return (int) sysConfig().values[KEY_LOCK_ACQUIRE_SPINS]; } int Config::getEventMemSize() { - return (int) sysConfig.values[KEY_EVENT_MEM_SIZE]; + return (int) sysConfig().values[KEY_EVENT_MEM_SIZE]; } int Config::getDeadlockTimeout() { - return (int) sysConfig.values[KEY_DEADLOCK_TIMEOUT]; + return (int) sysConfig().values[KEY_DEADLOCK_TIMEOUT]; } int Config::getPrioritySwitchDelay() { - int rc = (int) sysConfig.values[KEY_PRIORITY_SWITCH_DELAY]; + int rc = (int) sysConfig().values[KEY_PRIORITY_SWITCH_DELAY]; if (rc < 1) rc = 1; return rc; @@ -385,7 +362,7 @@ int Config::getPrioritySwitchDelay() int Config::getPriorityBoost() { - int rc = (int) sysConfig.values[KEY_PRIORITY_BOOST]; + int rc = (int) sysConfig().values[KEY_PRIORITY_BOOST]; if (rc < 1) rc = 1; if (rc > 1000) @@ -395,53 +372,53 @@ int Config::getPriorityBoost() bool Config::getUsePriorityScheduler() { - return (bool) sysConfig.values[KEY_USE_PRIORITY_SCHEDULER]; + return (bool) sysConfig().values[KEY_USE_PRIORITY_SCHEDULER]; } const char *Config::getRemoteServiceName() { - return (const char*) sysConfig.values[KEY_REMOTE_SERVICE_NAME]; + return (const char*) sysConfig().values[KEY_REMOTE_SERVICE_NAME]; } unsigned short Config::getRemoteServicePort() { - return (unsigned short) sysConfig.values[KEY_REMOTE_SERVICE_PORT]; + return (unsigned short) sysConfig().values[KEY_REMOTE_SERVICE_PORT]; } const char *Config::getRemotePipeName() { - return (const char*) sysConfig.values[KEY_REMOTE_PIPE_NAME]; + return (const char*) sysConfig().values[KEY_REMOTE_PIPE_NAME]; } const char *Config::getIpcName() { - return (const char*) sysConfig.values[KEY_IPC_NAME]; + return (const char*) sysConfig().values[KEY_IPC_NAME]; } int Config::getMaxUnflushedWrites() { - return (int) sysConfig.values[KEY_MAX_UNFLUSHED_WRITES]; + return (int) sysConfig().values[KEY_MAX_UNFLUSHED_WRITES]; } int Config::getMaxUnflushedWriteTime() { - return (int) sysConfig.values[KEY_MAX_UNFLUSHED_WRITE_TIME]; + return (int) sysConfig().values[KEY_MAX_UNFLUSHED_WRITE_TIME]; } int Config::getProcessPriorityLevel() { - return (int) sysConfig.values[KEY_PROCESS_PRIORITY_LEVEL]; + return (int) sysConfig().values[KEY_PROCESS_PRIORITY_LEVEL]; } bool Config::getCompleteBooleanEvaluation() { - return (bool) sysConfig.values[KEY_COMPLETE_BOOLEAN_EVALUATION]; + return (bool) sysConfig().values[KEY_COMPLETE_BOOLEAN_EVALUATION]; } int Config::getRemoteAuxPort() { #ifdef SUPERSERVER - return (int) sysConfig.values[KEY_REMOTE_AUX_PORT]; + return (int) sysConfig().values[KEY_REMOTE_AUX_PORT]; #else return 0; #endif @@ -449,70 +426,70 @@ int Config::getRemoteAuxPort() const char *Config::getRemoteBindAddress() { - return (const char*) sysConfig.values[KEY_REMOTE_BIND_ADDRESS]; + return (const char*) sysConfig().values[KEY_REMOTE_BIND_ADDRESS]; } const char *Config::getExternalFileAccess() { - return (const char*) sysConfig.values[KEY_EXTERNAL_FILE_ACCESS]; + return (const char*) sysConfig().values[KEY_EXTERNAL_FILE_ACCESS]; } const char *Config::getDatabaseAccess() { - return (const char*) sysConfig.values[KEY_DATABASE_ACCESS]; + return (const char*) sysConfig().values[KEY_DATABASE_ACCESS]; } const char *Config::getUdfAccess() { - return (const char*) sysConfig.values[KEY_UDF_ACCESS]; + return (const char*) sysConfig().values[KEY_UDF_ACCESS]; } const char *Config::getTempDirectories() { - return (const char*) sysConfig.values[KEY_TEMP_DIRECTORIES]; + return (const char*) sysConfig().values[KEY_TEMP_DIRECTORIES]; } bool Config::getBugcheckAbort() { - return (bool) sysConfig.values[KEY_BUGCHECK_ABORT]; + return (bool) sysConfig().values[KEY_BUGCHECK_ABORT]; } bool Config::getLegacyHash() { - return (bool) sysConfig.values[KEY_LEGACY_HASH]; + return (bool) sysConfig().values[KEY_LEGACY_HASH]; } const char *Config::getGCPolicy() { - return (const char *) sysConfig.values[KEY_GC_POLICY]; + return (const char *) sysConfig().values[KEY_GC_POLICY]; } bool Config::getRedirection() { - return (bool) sysConfig.values[KEY_REDIRECTION]; + return (bool) sysConfig().values[KEY_REDIRECTION]; } bool Config::getOldColumnNaming() { - return (bool) sysConfig.values[KEY_OLD_COLUMN_NAMING]; + return (bool) sysConfig().values[KEY_OLD_COLUMN_NAMING]; } const char *Config::getAuthMethod() { - return (const char *) sysConfig.values[KEY_AUTH_METHOD]; + return (const char *) sysConfig().values[KEY_AUTH_METHOD]; } int Config::getDatabaseGrowthIncrement() { - return (int) sysConfig.values[KEY_DATABASE_GROWTH_INCREMENT]; + return (int) sysConfig().values[KEY_DATABASE_GROWTH_INCREMENT]; } int Config::getMaxFileSystemCache() { - return (int) sysConfig.values[KEY_MAX_FILESYSTEM_CACHE]; + return (int) sysConfig().values[KEY_MAX_FILESYSTEM_CACHE]; } bool Config::getRelaxedAliasChecking() { - return (bool) sysConfig.values[KEY_RELAXED_ALIAS_CHECKING]; + return (bool) sysConfig().values[KEY_RELAXED_ALIAS_CHECKING]; } diff --git a/src/common/config/config_impl.h b/src/common/config/config_impl.h index 6970b515e6..e551a8779c 100644 --- a/src/common/config/config_impl.h +++ b/src/common/config/config_impl.h @@ -65,6 +65,7 @@ public: }; public: + explicit ConfigImpl(MemoryPool& p); ~ConfigImpl(); static string getValue(ConfigFile&, const ConfigKey); @@ -73,11 +74,7 @@ public: static bool asBoolean(const string&); static const char* asString(const string&); - inline static const ConfigImpl& instance(); - private: - explicit ConfigImpl(MemoryPool& p); - static const ConfigEntry entries[]; const char *root_dir; ConfigValue *values; diff --git a/src/common/fb_exception.cpp b/src/common/fb_exception.cpp index d4f111c1a8..55faacff85 100644 --- a/src/common/fb_exception.cpp +++ b/src/common/fb_exception.cpp @@ -7,6 +7,7 @@ #include #include "gen/iberror.h" #include "../common/classes/alloc.h" +#include "../common/classes/init.h" namespace { @@ -18,12 +19,12 @@ typedef Firebird::CircularStringsBuffer CircularBuffer; class InterlockedStringsBuffer : public CircularBuffer { public: + InterlockedStringsBuffer(Firebird::MemoryPool&) + : CircularBuffer() { } virtual char* alloc(const char* string, size_t length) { - buffer_lock.enter(); - char* new_string = CircularBuffer::alloc(string, length); - buffer_lock.leave(); - return new_string; + Firebird::MutexLockGuard guard(buffer_lock); + return CircularBuffer::alloc(string, length); } private: Firebird::Mutex buffer_lock; @@ -71,7 +72,7 @@ void fill_status(ISC_STATUS *ptr, ISC_STATUS status, va_list status_args) } } -InterlockedStringsBuffer engine_failures; +Firebird::GlobalPtr engine_failures; } // namespace @@ -353,7 +354,7 @@ const char* status_string(const char* string) const char* status_nstring(const char* string, size_t length) { - return engine_failures.alloc(string, length); + return engine_failures->alloc(string, length); } // Serialize exception into status_vector, put transient strings from exception into given StringsBuffer diff --git a/src/dsql/alld.cpp b/src/dsql/alld.cpp index f96f648616..c9205a0fe9 100644 --- a/src/dsql/alld.cpp +++ b/src/dsql/alld.cpp @@ -58,7 +58,6 @@ nested FOR loops are added. #include "../dsql/alld_proto.h" #include "../dsql/errd_proto.h" #include "../jrd/gds_proto.h" -#include "../jrd/thd.h" #include "../jrd/thread_proto.h" #include "../common/classes/array.h" diff --git a/src/dsql/ddl.cpp b/src/dsql/ddl.cpp index 941a69e0c7..2c65468223 100644 --- a/src/dsql/ddl.cpp +++ b/src/dsql/ddl.cpp @@ -75,7 +75,6 @@ #include "../common/classes/MetaName.h" #include "../dsql/dsql.h" #include "../jrd/ibase.h" -#include "../jrd/thd.h" #include "../jrd/intl.h" #include "../jrd/flags.h" #include "../jrd/constants.h" diff --git a/src/dsql/dsql.cpp b/src/dsql/dsql.cpp index 8c61ae7dbe..4a4bf9dbf1 100644 --- a/src/dsql/dsql.cpp +++ b/src/dsql/dsql.cpp @@ -66,7 +66,6 @@ nested FOR loops are added. #include #include "../dsql/dsql.h" #include "../jrd/ibase.h" -#include "../jrd/thd.h" #include "../jrd/align.h" #include "../jrd/intl.h" #include "../jrd/iberr.h" @@ -88,6 +87,7 @@ nested FOR loops are added. #include "../jrd/y_handle.h" #include "../common/config/config.h" #include "../common/utils_proto.h" +#include "../common/classes/init.h" #include "../jrd/scroll_cursors.h" #ifdef SCROLLABLE_CURSORS @@ -158,8 +158,8 @@ static const UCHAR sql_records_info[] = { isc_info_sql_records }; -static Firebird::Mutex databases_mutex; -static Firebird::Mutex cursors_mutex; +static Firebird::GlobalPtr databases_mutex; +static Firebird::GlobalPtr cursors_mutex; #ifdef DSQL_DEBUG @@ -588,10 +588,10 @@ ISC_STATUS GDS_DSQL_EXECUTE_CPP( request->req_open_cursor = open_cursor; open_cursor->opn_request = request; open_cursor->opn_transaction = *trans_handle; - cursors_mutex.enter(); + cursors_mutex->enter(); open_cursor->opn_next = open_cursors; open_cursors = open_cursor; - cursors_mutex.leave(); + cursors_mutex->leave(); THREAD_EXIT(); ISC_STATUS_ARRAY local_status; gds__transaction_cleanup(local_status, @@ -841,7 +841,7 @@ ISC_STATUS callback_execute_immediate( ISC_STATUS* status, dsql_dbb* database = 0; THREAD_EXIT(); - databases_mutex.enter(); + databases_mutex->enter(); try { // 1. Locate why_db_handle, corresponding to jrd_database_handle @@ -864,14 +864,14 @@ ISC_STATUS callback_execute_immediate( ISC_STATUS* status, } catch (const Firebird::Exception& e) { - databases_mutex.leave(); + databases_mutex->leave(); THREAD_ENTER(); return e.stuff_exception(status); } // 3. Call execute... function - databases_mutex.leave(); + databases_mutex->leave(); THREAD_ENTER(); const ISC_STATUS rc = dsql8_execute_immediate_common(status, &database->dbb_database_handle, &why_trans_handle->public_handle, @@ -887,7 +887,7 @@ YValve::Attachment* GetWhyAttachment(ISC_STATUS* status, Jrd::Attachment* jrd_attachment_handle) { THREAD_EXIT(); - databases_mutex.enter(); + databases_mutex->enter(); dsql_dbb* database; YValve::Attachment* db_handle = 0; for (database = databases; database; database = database->dbb_next) @@ -913,7 +913,7 @@ YValve::Attachment* GetWhyAttachment(ISC_STATUS* status, status[1] = isc_bad_db_handle; status[2] = isc_arg_end; } - databases_mutex.leave(); + databases_mutex->leave(); THREAD_ENTER(); return database ? db_handle : 0; } @@ -2964,7 +2964,7 @@ static void cleanup_database(FB_API_HANDLE* db_handle, void* flag) /* if (flag) THREAD_EXIT();*/ - databases_mutex.enter(); + databases_mutex->enter(); dsql_dbb* dbb; for (dsql_dbb** dbb_ptr = &databases; dbb = *dbb_ptr; dbb_ptr = &dbb->dbb_next) @@ -2997,7 +2997,7 @@ static void cleanup_database(FB_API_HANDLE* db_handle, void* flag) cleanup(0); gds__unregister_cleanup(cleanup, 0); } - databases_mutex.leave(); + databases_mutex->leave(); } @@ -3019,7 +3019,7 @@ static void cleanup_transaction (FB_API_HANDLE tra_handle, void* arg) // find this transaction/request pair in the list of pairs - cursors_mutex.enter(); + cursors_mutex->enter(); dsql_opn** open_cursor_ptr = &open_cursors; dsql_opn* open_cursor; while (open_cursor = *open_cursor_ptr) @@ -3028,7 +3028,7 @@ static void cleanup_transaction (FB_API_HANDLE tra_handle, void* arg) /* Found it, close the cursor but don't remove it from the list. The close routine will have done that. */ - cursors_mutex.leave(); + cursors_mutex->leave(); /* * we are expected to be within the subsystem when we do this * cleanup, for now do a thread_enter/thread_exit here. @@ -3041,14 +3041,14 @@ static void cleanup_transaction (FB_API_HANDLE tra_handle, void* arg) &open_cursor->opn_request, DSQL_close); THREAD_EXIT(); - cursors_mutex.enter(); + cursors_mutex->enter(); open_cursor_ptr = &open_cursors; } else open_cursor_ptr = &open_cursor->opn_next; } - cursors_mutex.leave(); + cursors_mutex->leave(); } @@ -3082,7 +3082,7 @@ static void close_cursor( dsql_req* request) // Remove the open cursor from the list - cursors_mutex.enter(); + cursors_mutex->enter(); dsql_opn** open_cursor_ptr = &open_cursors; dsql_opn* open_cursor; for (; open_cursor = *open_cursor_ptr; @@ -3094,7 +3094,7 @@ static void close_cursor( dsql_req* request) } } - cursors_mutex.leave(); + cursors_mutex->leave(); if (open_cursor) { delete open_cursor; @@ -4228,7 +4228,7 @@ static bool get_rsb_item(SSHORT* explain_length_ptr, static dsql_dbb* init(FB_API_HANDLE* db_handle) { THREAD_EXIT(); - databases_mutex.enter(); + databases_mutex->enter(); THREAD_ENTER(); if (!init_flag) @@ -4247,7 +4247,7 @@ static dsql_dbb* init(FB_API_HANDLE* db_handle) } if (!db_handle) { - databases_mutex.leave(); + databases_mutex->leave(); return NULL; } @@ -4257,7 +4257,7 @@ static dsql_dbb* init(FB_API_HANDLE* db_handle) for (database = databases; database; database = database->dbb_next) { if (database->dbb_database_handle == *db_handle) { - databases_mutex.leave(); + databases_mutex->leave(); return database; } } @@ -4268,7 +4268,7 @@ static dsql_dbb* init(FB_API_HANDLE* db_handle) database->dbb_next = databases; databases = database; database->dbb_database_handle = *db_handle; - databases_mutex.leave(); + databases_mutex->leave(); ISC_STATUS_ARRAY user_status; diff --git a/src/dsql/errd.cpp b/src/dsql/errd.cpp index 173862a0dc..a04a624b56 100644 --- a/src/dsql/errd.cpp +++ b/src/dsql/errd.cpp @@ -58,7 +58,6 @@ //#undef IBERROR #include "../jrd/gds_proto.h" -#include "../jrd/thd.h" #include "../common/utils_proto.h" diff --git a/src/dsql/gen.cpp b/src/dsql/gen.cpp index a134e64ce6..449c516dce 100644 --- a/src/dsql/gen.cpp +++ b/src/dsql/gen.cpp @@ -50,7 +50,6 @@ #include "../dsql/metd_proto.h" #include "../dsql/misc_func.h" #include "../dsql/utld_proto.h" -#include "../jrd/thd.h" #include "../jrd/thread_proto.h" #include "../jrd/dsc_proto.h" #include "../jrd/why_proto.h" diff --git a/src/dsql/hsh.cpp b/src/dsql/hsh.cpp index 43d8bd4ac0..5022a6e48f 100644 --- a/src/dsql/hsh.cpp +++ b/src/dsql/hsh.cpp @@ -30,7 +30,7 @@ #include "../dsql/errd_proto.h" #include "../dsql/hsh_proto.h" #include "../jrd/sch_proto.h" -#include "../jrd/thd.h" +#include "../common/classes/init.h" const int HASH_SIZE = 1021; @@ -39,7 +39,7 @@ static bool remove_symbol(dsql_sym**, dsql_sym*); static bool scompare(const TEXT*, USHORT, const TEXT*, const USHORT); static DSQL_SYM* hash_table; -static Firebird::Mutex hash_mutex; +static Firebird::GlobalPtr hash_mutex; /** diff --git a/src/dsql/make.cpp b/src/dsql/make.cpp index 266011031d..eb5db2ad09 100644 --- a/src/dsql/make.cpp +++ b/src/dsql/make.cpp @@ -56,7 +56,6 @@ #include "../jrd/DataTypeUtil.h" #include "../jrd/ods.h" #include "../jrd/ini.h" -#include "../jrd/thd.h" #include "../jrd/dsc_proto.h" #include "../jrd/cvt_proto.h" #include "../jrd/thread_proto.h" diff --git a/src/dsql/metd.epp b/src/dsql/metd.epp index a2e7c44a4c..9dd60936f4 100644 --- a/src/dsql/metd.epp +++ b/src/dsql/metd.epp @@ -38,7 +38,6 @@ #include "../jrd/ibase.h" #include "../jrd/align.h" #include "../jrd/intl.h" -#include "../jrd/thd.h" #include "../dsql/alld_proto.h" #include "../dsql/ddl_proto.h" #include "../dsql/metd_proto.h" @@ -103,17 +102,17 @@ static dsql_sym* lookup_symbol(dsql_dbb*, const dsql_str*, SYM_TYPE, USHORT = 0) namespace { #ifdef SUPERSERVER - REC_MUTX_T rec_mutex; // Recursive metadata mutex + Firebird::GlobalPtr rec_mutex; // Recursive metadata mutex class RecMutexHolder { public: RecMutexHolder() { THREAD_EXIT(); - THD_rec_mutex_lock(&rec_mutex); + rec_mutex->enter(); THREAD_ENTER(); } ~RecMutexHolder() { - THD_rec_mutex_unlock(&rec_mutex); + rec_mutex->leave(); } }; #else diff --git a/src/dsql/movd.cpp b/src/dsql/movd.cpp index 586ab4b3c7..59281f64c0 100644 --- a/src/dsql/movd.cpp +++ b/src/dsql/movd.cpp @@ -33,7 +33,6 @@ #include "../dsql/errd_proto.h" #include "../dsql/movd_proto.h" #include "../jrd/cvt_proto.h" -#include "../jrd/thd.h" static void post_error(ISC_STATUS, ...); diff --git a/src/dsql/parse.y b/src/dsql/parse.y index b8580491b4..f83ec4e27d 100644 --- a/src/dsql/parse.y +++ b/src/dsql/parse.y @@ -91,7 +91,6 @@ #include "../dsql/keywords.h" #include "../dsql/misc_func.h" #include "../jrd/gds_proto.h" -#include "../jrd/thd.h" #include "../jrd/err_proto.h" #include "../jrd/intlobj_new.h" diff --git a/src/dsql/pass1.cpp b/src/dsql/pass1.cpp index 6db9bfb8cd..f80c0ec0f7 100644 --- a/src/dsql/pass1.cpp +++ b/src/dsql/pass1.cpp @@ -143,7 +143,6 @@ #include #include "../jrd/ibase.h" #include "../dsql/dsql.h" -#include "../jrd/thd.h" #include "../jrd/intl.h" #include "../jrd/blr.h" #include "../jrd/constants.h" diff --git a/src/gpre/movg.cpp b/src/gpre/movg.cpp index 83115b2721..7c14e0a315 100644 --- a/src/gpre/movg.cpp +++ b/src/gpre/movg.cpp @@ -25,7 +25,7 @@ // //____________________________________________________________ // -// $Id: movg.cpp,v 1.8 2004-06-08 13:41:00 alexpeshkoff Exp $ +// $Id: movg.cpp,v 1.9 2008-01-23 15:51:51 alexpeshkoff Exp $ // #include "firebird.h" @@ -43,7 +43,6 @@ #include "../gpre/gpre.h" #include "../gpre/gpre_proto.h" #include "../jrd/cvt_proto.h" -#include "../jrd/thd.h" static void post_error(ISC_STATUS, ...); diff --git a/src/include/fb_exception.h b/src/include/fb_exception.h index 3be9a40d29..70bd27551d 100644 --- a/src/include/fb_exception.h +++ b/src/include/fb_exception.h @@ -36,7 +36,10 @@ namespace Firebird { -class StringsBuffer { +class MemoryPool; + +class StringsBuffer +{ public: virtual char* alloc(const char* string, size_t length) = 0; virtual ~StringsBuffer() {} @@ -45,13 +48,12 @@ public: }; template -class CircularStringsBuffer : public StringsBuffer { +class CircularStringsBuffer : public StringsBuffer +{ public: - CircularStringsBuffer() throw() { - // This is to ensure we have zero at the end of buffer in case of buffer overflow - memset(buffer, 0, BUFFER_SIZE); - buffer_ptr = buffer; - } + CircularStringsBuffer() throw() { init(); } + CircularStringsBuffer(MemoryPool&) throw() { init(); } + virtual char* alloc(const char* string, size_t length) { // fb_assert(length + 1 < BUFFER_SIZE); // If there isn't any more room in the buffer, start at the beginning again @@ -63,7 +65,15 @@ public: buffer_ptr += length + 1; return new_string; } + private: + void init() throw() + { + // This is to ensure we have zero at the end of buffer in case of buffer overflow + memset(buffer, 0, BUFFER_SIZE); + buffer_ptr = buffer; + } + char buffer[BUFFER_SIZE]; char *buffer_ptr; }; diff --git a/src/iscguard/cntl_guard.cpp b/src/iscguard/cntl_guard.cpp index a9dc1c5302..ae0942e643 100644 --- a/src/iscguard/cntl_guard.cpp +++ b/src/iscguard/cntl_guard.cpp @@ -48,8 +48,8 @@ static USHORT report_status(DWORD, DWORD, DWORD, DWORD); static DWORD current_state; static ThreadEntryPoint* main_handler; static SERVICE_STATUS_HANDLE service_handle; -static Firebird::string* service_name = NULL; -static Firebird::string* remote_name = NULL; +static Firebird::GlobalPtr service_name; +static Firebird::GlobalPtr remote_name; static HANDLE stop_event_handle; @@ -67,9 +67,7 @@ void CNTL_init(ThreadEntryPoint* handler, const TEXT* name) main_handler = handler; MemoryPool& pool = *getDefaultMemoryPool(); - service_name = FB_NEW(pool) Firebird::string(pool); service_name->printf(ISCGUARD_SERVICE, name); - remote_name = FB_NEW(pool) Firebird::string(pool); remote_name->printf(REMOTE_SERVICE, name); } diff --git a/src/iscguard/iscguard.cpp b/src/iscguard/iscguard.cpp index bb34ed6c5d..ce1d23c086 100644 --- a/src/iscguard/iscguard.cpp +++ b/src/iscguard/iscguard.cpp @@ -89,9 +89,9 @@ HWND hPSDlg, hWndGbl; static int nRestarts = 0; /* the number of times the server was restarted */ static bool service_flag = true; static TEXT instance[MAXPATHLEN]; -static Firebird::string* service_name = NULL; -static Firebird::string* remote_name = NULL; -static Firebird::string* mutex_name = NULL; +static Firebird::GlobalPtr service_name; +static Firebird::GlobalPtr remote_name; +static Firebird::GlobalPtr mutex_name; /* unsigned short shutdown_flag = FALSE; */ static log_info* log_entry; @@ -124,12 +124,8 @@ int WINAPI WinMain( if (service_flag) { strcpy(instance, FB_DEFAULT_INSTANCE); service_flag = parse_args(lpszCmdLine); - MemoryPool& pool = *getDefaultMemoryPool(); - service_name = FB_NEW(pool) Firebird::string(pool); service_name->printf(ISCGUARD_SERVICE, instance); - remote_name = FB_NEW(pool) Firebird::string(pool); remote_name->printf(REMOTE_SERVICE, instance); - mutex_name = FB_NEW(pool) Firebird::string(pool); mutex_name->printf(GUARDIAN_MUTEX, instance); } diff --git a/src/jrd/DatabaseSnapshot.cpp b/src/jrd/DatabaseSnapshot.cpp index 42b5b2e535..ce1c1adfdc 100644 --- a/src/jrd/DatabaseSnapshot.cpp +++ b/src/jrd/DatabaseSnapshot.cpp @@ -312,7 +312,7 @@ void DatabaseSnapshot::SharedMemory::init(void* arg, SH_MEM_T* shmemData, bool i // DatabaseSnapshot class -Mutex DatabaseSnapshot::initMutex; +GlobalPtr DatabaseSnapshot::initMutex; DatabaseSnapshot::SharedMemory* DatabaseSnapshot::dump = NULL; int DatabaseSnapshot::pid = getpid(); diff --git a/src/jrd/DatabaseSnapshot.h b/src/jrd/DatabaseSnapshot.h index 109f286484..c13dbb4169 100644 --- a/src/jrd/DatabaseSnapshot.h +++ b/src/jrd/DatabaseSnapshot.h @@ -127,7 +127,7 @@ private: static void putCall(const jrd_req*, Firebird::ClumpletWriter&, int); static void putStatistics(const RuntimeStatistics*, Firebird::ClumpletWriter&, int, int); - static Firebird::Mutex initMutex; + static Firebird::GlobalPtr initMutex; static SharedMemory* dump; static int pid; diff --git a/src/jrd/TempSpace.cpp b/src/jrd/TempSpace.cpp index 91800e1913..385d0227d3 100644 --- a/src/jrd/TempSpace.cpp +++ b/src/jrd/TempSpace.cpp @@ -33,7 +33,7 @@ // Static definitions/initializations -Firebird::Mutex TempSpace::initMutex; +Firebird::GlobalPtr TempSpace::initMutex; Firebird::TempDirectoryList* TempSpace::tempDirs = NULL; size_t TempSpace::minBlockSize = 0; offset_t TempSpace::globalCacheUsage = 0; diff --git a/src/jrd/TempSpace.h b/src/jrd/TempSpace.h index a28c3526b4..85a3861da7 100644 --- a/src/jrd/TempSpace.h +++ b/src/jrd/TempSpace.h @@ -28,6 +28,7 @@ #include "../common/classes/array.h" #include "../common/classes/TempFile.h" #include "../common/config/dir_list.h" +#include "../common/classes/init.h" class TempSpace : public Firebird::File { public: @@ -167,7 +168,7 @@ private: Segment* freeSegments; Segment* notUsedSegments; - static Firebird::Mutex initMutex; + static Firebird::GlobalPtr initMutex; static Firebird::TempDirectoryList* tempDirs; static size_t minBlockSize; static offset_t globalCacheUsage; diff --git a/src/jrd/alt.cpp b/src/jrd/alt.cpp index ea7d9c854d..8599bc28fb 100644 --- a/src/jrd/alt.cpp +++ b/src/jrd/alt.cpp @@ -34,6 +34,7 @@ #include #include #include "../jrd/common.h" +#include "../common/classes/init.h" #include #include "../jrd/ibase.h" @@ -1217,13 +1218,15 @@ static ISC_STATUS executeSecurityCommand( input_user_data->server); if (handle) { + static Firebird::GlobalPtr secExecMutex; + static Firebird::GlobalPtr > secExecBuf; + callRemoteServiceManager(status, handle, userInfo, 0, 0); - static Firebird::CircularStringsBuffer<1024> secExecBuf; - static Firebird::Mutex secExecMutex; { Firebird::MutexLockGuard lockMutex(secExecMutex); - secExecBuf.makePermanentVector(status, status); + secExecBuf->makePermanentVector(status, status); } + ISC_STATUS_ARRAY user_status; detachRemoteServiceManager(user_status, handle); } diff --git a/src/jrd/blb.cpp b/src/jrd/blb.cpp index be613a36f8..65d61e58db 100644 --- a/src/jrd/blb.cpp +++ b/src/jrd/blb.cpp @@ -69,7 +69,6 @@ #include "../jrd/mov_proto.h" #include "../jrd/pag_proto.h" #include "../jrd/sdl_proto.h" -#include "../jrd/thd.h" #include "../jrd/dsc_proto.h" #include "../common/classes/array.h" diff --git a/src/jrd/blob_filter.cpp b/src/jrd/blob_filter.cpp index 2365cac16a..ce611b3f0e 100644 --- a/src/jrd/blob_filter.cpp +++ b/src/jrd/blob_filter.cpp @@ -50,7 +50,6 @@ #include "../jrd/err_proto.h" #include "../jrd/common.h" #include "../jrd/ibsetjmp.h" -#include "../jrd/thd.h" #include "../jrd/isc_s_proto.h" #include "gen/iberror.h" diff --git a/src/jrd/btr.cpp b/src/jrd/btr.cpp index 7065833e1f..53a8c40fc3 100644 --- a/src/jrd/btr.cpp +++ b/src/jrd/btr.cpp @@ -61,7 +61,6 @@ #include "../jrd/pag_proto.h" #include "../jrd/pcmet_proto.h" #include "../jrd/sort_proto.h" -#include "../jrd/thd.h" #include "../jrd/tra_proto.h" using namespace Jrd; diff --git a/src/jrd/cmp.cpp b/src/jrd/cmp.cpp index deee93b201..819de63318 100644 --- a/src/jrd/cmp.cpp +++ b/src/jrd/cmp.cpp @@ -75,7 +75,6 @@ #include "../jrd/opt_proto.h" #include "../jrd/par_proto.h" #include "../jrd/scl_proto.h" -#include "../jrd/thd.h" #include "../jrd/met_proto.h" #include "../jrd/mov_proto.h" #include "../jrd/dsc_proto.h" diff --git a/src/jrd/cvt.cpp b/src/jrd/cvt.cpp index f54c43320e..3587eeb581 100644 --- a/src/jrd/cvt.cpp +++ b/src/jrd/cvt.cpp @@ -50,7 +50,6 @@ #include "../jrd/err_proto.h" #include "../jrd/gds_proto.h" #include "../jrd/intl_proto.h" -#include "../jrd/thd.h" #include "../common/classes/timestamp.h" diff --git a/src/jrd/cvt2.cpp b/src/jrd/cvt2.cpp index 90395372c1..a850d03b92 100644 --- a/src/jrd/cvt2.cpp +++ b/src/jrd/cvt2.cpp @@ -40,7 +40,6 @@ #include "../jrd/cvt2_proto.h" #include "../jrd/err_proto.h" #include "../jrd/intl_proto.h" -#include "../jrd/thd.h" #include "../jrd/intl_classes.h" #include "../jrd/gds_proto.h" /* CVC: I needed them here. */ diff --git a/src/jrd/dbg.cpp b/src/jrd/dbg.cpp index fdced91da7..d0b464d0b3 100644 --- a/src/jrd/dbg.cpp +++ b/src/jrd/dbg.cpp @@ -44,7 +44,6 @@ #include "../jrd/err_proto.h" #ifdef SUPERSERVER -#include "../jrd/thd.h" #include "../jrd/err_proto.h" #endif diff --git a/src/jrd/dfw.epp b/src/jrd/dfw.epp index 2c38d5283a..1fd09c0b54 100644 --- a/src/jrd/dfw.epp +++ b/src/jrd/dfw.epp @@ -103,7 +103,6 @@ #include "../jrd/sch_proto.h" #include "../jrd/scl_proto.h" #include "../jrd/sdw_proto.h" -#include "../jrd/thd.h" #include "../jrd/thread_proto.h" #include "../jrd/event_proto.h" #include "../jrd/nbak.h" @@ -4676,7 +4675,7 @@ static bool modify_procedure( thread_db* tdbb, try { THREAD_EXIT(); - if (THD_rec_mutex_lock(&tdbb->getDatabase()->dbb_sp_rec_mutex)) + if (tdbb->getDatabase()->dbb_sp_rec_mutex.enter()) { THREAD_ENTER(); return false; @@ -4717,7 +4716,7 @@ static bool modify_procedure( thread_db* tdbb, true, PRC_being_altered))) { - THD_rec_mutex_unlock(&tdbb->getDatabase()->dbb_sp_rec_mutex); + tdbb->getDatabase()->dbb_sp_rec_mutex.leave(); return false; } procedure->prc_alter_count = ++prc_alter_count; @@ -4771,11 +4770,11 @@ static bool modify_procedure( thread_db* tdbb, catch (const Firebird::Exception& ex) { Firebird::stuff_exception(tdbb->tdbb_status_vector, ex); - THD_rec_mutex_unlock(&tdbb->getDatabase()->dbb_sp_rec_mutex); + tdbb->getDatabase()->dbb_sp_rec_mutex.leave(); ERR_punt(); } - THD_rec_mutex_unlock(&tdbb->getDatabase()->dbb_sp_rec_mutex); + tdbb->getDatabase()->dbb_sp_rec_mutex.leave(); return true; case 5: diff --git a/src/jrd/dmp.cpp b/src/jrd/dmp.cpp index 21f9b9e46f..c8be30c4aa 100644 --- a/src/jrd/dmp.cpp +++ b/src/jrd/dmp.cpp @@ -41,7 +41,6 @@ #include "../jrd/err_proto.h" #include "../jrd/gds_proto.h" #include "../jrd/sqz_proto.h" -#include "../jrd/thd.h" diff --git a/src/jrd/dpm.epp b/src/jrd/dpm.epp index 9a9665d7d4..77edcd7837 100644 --- a/src/jrd/dpm.epp +++ b/src/jrd/dpm.epp @@ -64,7 +64,6 @@ #include "../jrd/mov_proto.h" #include "../jrd/pag_proto.h" #include "../jrd/sqz_proto.h" -#include "../jrd/thd.h" #ifdef DEV_BUILD #include "../jrd/dbg_proto.h" diff --git a/src/jrd/dyn.epp b/src/jrd/dyn.epp index b1787b276b..435179fc99 100644 --- a/src/jrd/dyn.epp +++ b/src/jrd/dyn.epp @@ -71,7 +71,6 @@ #include "../jrd/intl_proto.h" #include "../jrd/isc_f_proto.h" #include "../jrd/met_proto.h" -#include "../jrd/thd.h" #include "../jrd/vio_proto.h" #include "../jrd/sch_proto.h" #include "../jrd/thread_proto.h" diff --git a/src/jrd/dyn_def.epp b/src/jrd/dyn_def.epp index 73eaa2db56..c95646e4ba 100644 --- a/src/jrd/dyn_def.epp +++ b/src/jrd/dyn_def.epp @@ -73,7 +73,6 @@ #include "../jrd/intl_proto.h" #include "../jrd/isc_f_proto.h" #include "../jrd/met_proto.h" -#include "../jrd/thd.h" #include "../jrd/vio_proto.h" #include "../jrd/scl_proto.h" #include "../jrd/gdsassert.h" diff --git a/src/jrd/dyn_del.epp b/src/jrd/dyn_del.epp index a068fa49af..366f91ac08 100644 --- a/src/jrd/dyn_del.epp +++ b/src/jrd/dyn_del.epp @@ -57,7 +57,6 @@ #include "../jrd/intl_proto.h" #include "../jrd/isc_f_proto.h" #include "../jrd/met_proto.h" -#include "../jrd/thd.h" #include "../jrd/vio_proto.h" #include "../common/utils_proto.h" diff --git a/src/jrd/dyn_mod.epp b/src/jrd/dyn_mod.epp index e6f07d22af..bc3f2ec980 100644 --- a/src/jrd/dyn_mod.epp +++ b/src/jrd/dyn_mod.epp @@ -69,7 +69,6 @@ #include "../jrd/intl_proto.h" #include "../jrd/isc_f_proto.h" #include "../jrd/met_proto.h" -#include "../jrd/thd.h" #include "../jrd/vio_proto.h" #include "../jrd/dsc_proto.h" #include "../common/utils_proto.h" diff --git a/src/jrd/dyn_util.epp b/src/jrd/dyn_util.epp index 100a2bc6ac..7e783da052 100644 --- a/src/jrd/dyn_util.epp +++ b/src/jrd/dyn_util.epp @@ -54,7 +54,6 @@ #include "../jrd/inf_proto.h" #include "../jrd/intl_proto.h" #include "../jrd/isc_f_proto.h" -#include "../jrd/thd.h" #include "../jrd/vio_proto.h" #include "../common/utils_proto.h" diff --git a/src/jrd/enc.cpp b/src/jrd/enc.cpp index 33445cf53c..7b59ba6339 100644 --- a/src/jrd/enc.cpp +++ b/src/jrd/enc.cpp @@ -10,6 +10,8 @@ #include "../jrd/enc_proto.h" #include "../jrd/gdsassert.h" #include "../common/classes/locks.h" +#include "../common/classes/alloc.h" +#include "../common/classes/init.h" /* #ifdef HAVE_UNISTD_H @@ -427,7 +429,7 @@ static C_block CF6464[64 / CHUNKBITS][1 << CHUNKBITS]; /* ==================================== */ -static Firebird::Mutex cryptMutex; +static Firebird::GlobalPtr cryptMutex; static C_block constdatablock; /* encryption constant */ const static size_t RESULT_SIZE = (1 + 4 + 4 + 11 + 1); diff --git a/src/jrd/err.cpp b/src/jrd/err.cpp index 6215794c2a..3ab5016e5d 100644 --- a/src/jrd/err.cpp +++ b/src/jrd/err.cpp @@ -47,7 +47,6 @@ #include "../jrd/cch_proto.h" #include "../jrd/met_proto.h" #endif -#include "../jrd/thd.h" #include "../jrd/dbg_proto.h" #include "../jrd/err_proto.h" #include "../jrd/gds_proto.h" diff --git a/src/jrd/event.h b/src/jrd/event.h index b7bda81f0d..401961718b 100644 --- a/src/jrd/event.h +++ b/src/jrd/event.h @@ -31,7 +31,6 @@ #define JRD_EVENT_H #include "../jrd/isc.h" -#include "../jrd/thd.h" #include "../jrd/file_params.h" #include "../jrd/que.h" diff --git a/src/jrd/evl.cpp b/src/jrd/evl.cpp index b1d9bc1925..d764568c0a 100644 --- a/src/jrd/evl.cpp +++ b/src/jrd/evl.cpp @@ -104,7 +104,6 @@ #include "../jrd/rlck_proto.h" #include "../jrd/rse_proto.h" #include "../jrd/scl_proto.h" -#include "../jrd/thd.h" #include "../jrd/sort_proto.h" #include "../jrd/gds_proto.h" #include "../jrd/align.h" diff --git a/src/jrd/exe.cpp b/src/jrd/exe.cpp index 42c5a40a9e..b68c1290ad 100644 --- a/src/jrd/exe.cpp +++ b/src/jrd/exe.cpp @@ -94,7 +94,6 @@ #include "../jrd/rlck_proto.h" #include "../jrd/rse_proto.h" -#include "../jrd/thd.h" #include "../jrd/tra_proto.h" #include "../jrd/vio_proto.h" #include "../jrd/isc_s_proto.h" diff --git a/src/jrd/execute_statement.cpp b/src/jrd/execute_statement.cpp index 1473c7bb85..90deccb42c 100644 --- a/src/jrd/execute_statement.cpp +++ b/src/jrd/execute_statement.cpp @@ -35,7 +35,6 @@ #include "../jrd/jrd.h" #include "../jrd/tra.h" #include "../jrd/dsc.h" -#include "../jrd/thd.h" #include "../jrd/err_proto.h" #include "../jrd/mov_proto.h" #include "../jrd/evl_proto.h" diff --git a/src/jrd/ext.cpp b/src/jrd/ext.cpp index 5d5d9eb9c8..dab3ad4ad6 100644 --- a/src/jrd/ext.cpp +++ b/src/jrd/ext.cpp @@ -52,7 +52,6 @@ #include "../jrd/gds_proto.h" #include "../jrd/met_proto.h" #include "../jrd/mov_proto.h" -#include "../jrd/thd.h" #include "../jrd/vio_proto.h" #include "../common/config/config.h" #include "../common/config/dir_list.h" diff --git a/src/jrd/fil.h b/src/jrd/fil.h index a662412461..c6231a1833 100644 --- a/src/jrd/fil.h +++ b/src/jrd/fil.h @@ -31,8 +31,6 @@ #ifndef JRD_FIL_H #define JRD_FIL_H -#include "../jrd/thd.h" - /* Defined the directory list structures. */ /* Temporary workfile directory list. */ diff --git a/src/jrd/filters.cpp b/src/jrd/filters.cpp index 183972abb3..4e6eb7ead3 100644 --- a/src/jrd/filters.cpp +++ b/src/jrd/filters.cpp @@ -39,7 +39,6 @@ #include "../jrd/filte_proto.h" #include "../jrd/gds_proto.h" #include "../jrd/intl_proto.h" -#include "../jrd/thd.h" using namespace Jrd; diff --git a/src/jrd/flu.cpp b/src/jrd/flu.cpp index 69378fe315..97ea806fe9 100644 --- a/src/jrd/flu.cpp +++ b/src/jrd/flu.cpp @@ -72,7 +72,7 @@ namespace { Firebird::InitInstance loadedModules; - Firebird::Mutex modulesMutex; + Firebird::GlobalPtr modulesMutex; template void terminate_at_space(S& s, const char* psz) diff --git a/src/jrd/fun.epp b/src/jrd/fun.epp index f8a19ceb9f..1d3c911ab1 100644 --- a/src/jrd/fun.epp +++ b/src/jrd/fun.epp @@ -57,7 +57,6 @@ #include "../jrd/fun_proto.h" #include "../jrd/gds_proto.h" #include "../jrd/mov_proto.h" -#include "../jrd/thd.h" #include "../jrd/sch_proto.h" #include "../jrd/thread_proto.h" #include "../jrd/isc_s_proto.h" diff --git a/src/jrd/gds.cpp b/src/jrd/gds.cpp index 43bb71e61c..0217f5a5d0 100644 --- a/src/jrd/gds.cpp +++ b/src/jrd/gds.cpp @@ -106,7 +106,6 @@ static const TEXT gdslogid[] = ""; static const char* FB_PID_FILE = "fb_%d"; #include "gen/sql_code.h" -#include "../jrd/thd.h" #include "gen/iberror.h" #include "../jrd/ibase.h" @@ -226,7 +225,7 @@ typedef clean *CLEAN; static CLEAN cleanup_handlers = NULL; static gds_msg* global_default_msg = NULL; -static bool initialized = false; +static bool volatile initialized = false; void* API_ROUTINE gds__alloc_debug(SLONG size_request, const TEXT* filename, @@ -3226,10 +3225,10 @@ void gds__cleanup(void) (*routine)(arg); } + Firebird::InstanceControl::registerGdsCleanup(0); initialized = false; } - static void init(void) { /************************************** @@ -3245,6 +3244,12 @@ static void init(void) if (initialized) return; + static Firebird::GlobalPtr gdsInitMutex; + Firebird::MutexLockGuard guard(gdsInitMutex); + + if (initialized) + return; + #ifdef UNIX gds_pid = getpid(); #ifdef SUPERSERVER @@ -3278,7 +3283,7 @@ static void init(void) initialized = true; - atexit(gds__cleanup); + Firebird::InstanceControl::registerGdsCleanup(gds__cleanup); gdsPrefixInit(); diff --git a/src/jrd/gdsassert.h b/src/jrd/gdsassert.h index e6f3a0735e..fa878cc5a7 100644 --- a/src/jrd/gdsassert.h +++ b/src/jrd/gdsassert.h @@ -36,20 +36,20 @@ * 1996-Feb-09 David Schnepper */ -#define FB_GDS_ASSERT_FAILURE_STRING "GDS Assertion failure: %s %"LINEFORMAT"\n" +#define FB_GDS_ASSERT_FAILURE_STRING "GDS Assertion (%s) failure: %s %"LINEFORMAT"\n" #ifdef SUPERSERVER #if !defined(fb_assert) -#define fb_assert(ex) {if (!(ex)) {gds__log (FB_GDS_ASSERT_FAILURE_STRING, __FILE__, __LINE__); abort();}} -#define fb_assert_continue(ex) {if (!(ex)) {gds__log (FB_GDS_ASSERT_FAILURE_STRING, __FILE__, __LINE__);}} +#define fb_assert(ex) {if (!(ex)) {gds__log (FB_GDS_ASSERT_FAILURE_STRING, #ex, __FILE__, __LINE__); abort();}} +#define fb_assert_continue(ex) {if (!(ex)) {gds__log (FB_GDS_ASSERT_FAILURE_STRING, #ex, __FILE__, __LINE__);}} #endif #else // !SUPERSERVER #if !defined(fb_assert) -#define fb_assert(ex) {if (!(ex)) {fprintf (stderr, FB_GDS_ASSERT_FAILURE_STRING, __FILE__, __LINE__); abort();}} -#define fb_assert_continue(ex) {if (!(ex)) {fprintf (stderr, FB_GDS_ASSERT_FAILURE_STRING, __FILE__, __LINE__);}} +#define fb_assert(ex) {if (!(ex)) {fprintf (stderr, FB_GDS_ASSERT_FAILURE_STRING, #ex, __FILE__, __LINE__); abort();}} +#define fb_assert_continue(ex) {if (!(ex)) {fprintf (stderr, FB_GDS_ASSERT_FAILURE_STRING, #ex, __FILE__, __LINE__);}} #endif #endif // SUPERSERVER diff --git a/src/jrd/grant.epp b/src/jrd/grant.epp index e67ba2e82c..8848cbfb78 100644 --- a/src/jrd/grant.epp +++ b/src/jrd/grant.epp @@ -52,7 +52,6 @@ #include "../jrd/jrd_proto.h" #include "../jrd/met_proto.h" #include "../jrd/scl_proto.h" -#include "../jrd/thd.h" #include "../common/utils_proto.h" #include "../common/classes/array.h" diff --git a/src/jrd/idx.cpp b/src/jrd/idx.cpp index 00e8af8ded..fe77776919 100644 --- a/src/jrd/idx.cpp +++ b/src/jrd/idx.cpp @@ -63,7 +63,6 @@ #include "../jrd/met_proto.h" #include "../jrd/mov_proto.h" #include "../jrd/sort_proto.h" -#include "../jrd/thd.h" #include "../jrd/vio_proto.h" #include "../jrd/tra_proto.h" diff --git a/src/jrd/inf.cpp b/src/jrd/inf.cpp index 9badf54121..b62b75711f 100644 --- a/src/jrd/inf.cpp +++ b/src/jrd/inf.cpp @@ -57,7 +57,6 @@ #include "../jrd/opt_proto.h" #include "../jrd/pag_proto.h" #include "../jrd/os/pio_proto.h" -#include "../jrd/thd.h" #include "../jrd/tra_proto.h" #include "../jrd/gds_proto.h" #include "../jrd/err_proto.h" diff --git a/src/jrd/ini.epp b/src/jrd/ini.epp index c1d4a26431..ab839a44a0 100644 --- a/src/jrd/ini.epp +++ b/src/jrd/ini.epp @@ -51,7 +51,6 @@ #include "../jrd/ini_proto.h" #include "../jrd/jrd_proto.h" #include "../jrd/met_proto.h" -#include "../jrd/thd.h" #include "../jrd/obj.h" #include "../jrd/acl.h" #include "../jrd/irq.h" diff --git a/src/jrd/intl.cpp b/src/jrd/intl.cpp index 20206ea703..42a1858b06 100644 --- a/src/jrd/intl.cpp +++ b/src/jrd/intl.cpp @@ -118,7 +118,6 @@ #include "../jrd/isc_proto.h" #include "../jrd/lck_proto.h" #include "../jrd/met_proto.h" -#include "../jrd/thd.h" #include "../jrd/intlobj_new.h" #include "../jrd/jrd.h" #include "../jrd/mov_proto.h" diff --git a/src/jrd/isc_sync.cpp b/src/jrd/isc_sync.cpp index 9479c8fa41..f4abcd19c9 100644 --- a/src/jrd/isc_sync.cpp +++ b/src/jrd/isc_sync.cpp @@ -66,7 +66,6 @@ #include "../jrd/jrd.h" #include "../jrd/sch_proto.h" #include "../jrd/err_proto.h" -#include "../jrd/thd.h" #include "../jrd/thread_proto.h" #include "../jrd/jrd_pwd.h" #include "../common/config/config.h" diff --git a/src/jrd/jrd.cpp b/src/jrd/jrd.cpp index e2aa0427a3..a5331797cb 100644 --- a/src/jrd/jrd.cpp +++ b/src/jrd/jrd.cpp @@ -143,7 +143,7 @@ int debug; namespace { - REC_MUTX_T databases_rec_mutex; + Firebird::GlobalPtr databases_rec_mutex; Database* databases = NULL; class EngineStartup @@ -164,16 +164,18 @@ namespace Firebird::InitMutex engineStartup; + // Here we always ignore mutex errors, Possibly not good. + inline void dbMutexLock() { THREAD_EXIT(); - THD_rec_mutex_lock(&databases_rec_mutex); + databases_rec_mutex->enter(); THREAD_ENTER(); } inline void dbMutexUnlock() { - THD_rec_mutex_unlock(&databases_rec_mutex); + databases_rec_mutex->leave(); } } // anonymous @@ -194,7 +196,7 @@ void Jrd::Trigger::compile(thread_db* tdbb) Database* dbb = tdbb->getDatabase(); THREAD_EXIT(); - const int error = THD_rec_mutex_lock(&dbb->dbb_sp_rec_mutex); + const int error = dbb->dbb_sp_rec_mutex.enter(); THREAD_ENTER(); if (error) { @@ -202,7 +204,7 @@ void Jrd::Trigger::compile(thread_db* tdbb) } if (request) { - THD_rec_mutex_unlock(&dbb->dbb_sp_rec_mutex); + dbb->dbb_sp_rec_mutex.leave(); return; } #endif /* SUPERSERVER */ @@ -248,7 +250,7 @@ void Jrd::Trigger::compile(thread_db* tdbb) } #ifdef SUPERSERVER - THD_rec_mutex_unlock(&dbb->dbb_sp_rec_mutex); + dbb->dbb_sp_rec_mutex.leave(); #endif throw; } @@ -267,7 +269,7 @@ void Jrd::Trigger::compile(thread_db* tdbb) compile_in_progress = false; #ifdef SUPERSERVER - THD_rec_mutex_unlock(&dbb->dbb_sp_rec_mutex); + dbb->dbb_sp_rec_mutex.leave(); #endif } } diff --git a/src/jrd/jrd.h b/src/jrd/jrd.h index 641d993276..bb5c65ce8c 100644 --- a/src/jrd/jrd.h +++ b/src/jrd/jrd.h @@ -93,7 +93,7 @@ #include "../jrd/isc.h" // recursive mutexes -#include "../jrd/thd.h" +#include "../common/thd.h" // Definition of block types for data allocation in JRD #include "../jrd/jrd_blks.h" @@ -237,7 +237,7 @@ public: DatabaseModules modules; // external function/filter modules Firebird::Mutex* dbb_mutexes; // Database block mutexes - REC_MUTX_T dbb_sp_rec_mutex; // Recursive mutex for accessing/updating stored procedure metadata + Firebird::RecursiveMutex dbb_sp_rec_mutex; // Recursive mutex for accessing/updating stored procedure metadata //SLONG dbb_sort_size; // Size of sort space per sort, unused for now UATOM dbb_ast_flags; // flags modified at AST level @@ -1093,8 +1093,6 @@ public: * there is no database set up. */ -#include "../jrd/thd.h" - #if defined(DEV_BUILD) #include "../jrd/err_proto.h" diff --git a/src/jrd/jrd_pwd.h b/src/jrd/jrd_pwd.h index dc8e75f576..0579956891 100644 --- a/src/jrd/jrd_pwd.h +++ b/src/jrd/jrd_pwd.h @@ -28,7 +28,6 @@ #define JRD_PWD_H #include "../jrd/ibase.h" -#include "../jrd/thd.h" #include "../jrd/sha.h" #ifdef HAVE_STDLIB_H #include diff --git a/src/jrd/lck.cpp b/src/jrd/lck.cpp index 90c048cae7..675ab0c4b4 100644 --- a/src/jrd/lck.cpp +++ b/src/jrd/lck.cpp @@ -40,7 +40,6 @@ #include "../jrd/lck_proto.h" #include "../lock/lock_proto.h" #include "../jrd/sch_proto.h" -#include "../jrd/thd.h" #include "../jrd/gdsassert.h" #ifdef HAVE_SYS_TYPES_H diff --git a/src/jrd/log.cpp b/src/jrd/log.cpp index 887fdc6fe6..2d3864697d 100644 --- a/src/jrd/log.cpp +++ b/src/jrd/log.cpp @@ -33,7 +33,6 @@ #include "../jrd/gds_proto.h" #include "../jrd/log_proto.h" #include "../jrd/pag_proto.h" -#include "../jrd/thd.h" diff --git a/src/jrd/met.epp b/src/jrd/met.epp index d522aff409..79fbb4a95e 100644 --- a/src/jrd/met.epp +++ b/src/jrd/met.epp @@ -92,7 +92,6 @@ #include "../jrd/os/pio_proto.h" #include "../jrd/scl_proto.h" #include "../jrd/sdw_proto.h" -#include "../jrd/thd.h" #include "../jrd/sch_proto.h" #include "../jrd/thread_proto.h" #include "../jrd/iberr.h" @@ -3008,7 +3007,7 @@ jrd_prc* MET_procedure(thread_db* tdbb, int id, bool noscan, USHORT flags) THREAD_EXIT(); - if (THD_rec_mutex_lock(&dbb->dbb_sp_rec_mutex)) + if (dbb->dbb_sp_rec_mutex.enter()) { THREAD_ENTER(); return NULL; @@ -3048,7 +3047,7 @@ jrd_prc* MET_procedure(thread_db* tdbb, int id, bool noscan, USHORT flags) if ((procedure->prc_flags & PRC_being_scanned) || (procedure->prc_flags & PRC_scanned)) { - THD_rec_mutex_unlock(&dbb->dbb_sp_rec_mutex); + dbb->dbb_sp_rec_mutex.leave(); return procedure; } } @@ -3347,12 +3346,12 @@ jrd_prc* MET_procedure(thread_db* tdbb, int id, bool noscan, USHORT flags) fb_assert(procedure->prc_flags & PRC_being_scanned); procedure->prc_flags &= ~PRC_being_scanned; - THD_rec_mutex_unlock(&dbb->dbb_sp_rec_mutex); + dbb->dbb_sp_rec_mutex.leave(); } // try catch (const Firebird::Exception&) { procedure->prc_flags &= ~(PRC_being_scanned | PRC_scanned); - THD_rec_mutex_unlock(&dbb->dbb_sp_rec_mutex); + dbb->dbb_sp_rec_mutex.leave(); if (procedure->prc_existence_lock) { LCK_release(tdbb, procedure->prc_existence_lock); @@ -3717,7 +3716,7 @@ void MET_scan_relation( thread_db* tdbb, jrd_rel* relation) Database* dbb = tdbb->getDatabase(); THREAD_EXIT(); - if (THD_rec_mutex_lock(&dbb->dbb_sp_rec_mutex)) { + if (dbb->dbb_sp_rec_mutex.enter()) { THREAD_ENTER(); return; } @@ -3726,7 +3725,7 @@ void MET_scan_relation( thread_db* tdbb, jrd_rel* relation) if (relation->rel_flags & REL_scanned || relation->rel_flags & REL_deleted) { - THD_rec_mutex_unlock(&dbb->dbb_sp_rec_mutex); + dbb->dbb_sp_rec_mutex.leave(); return; } @@ -4064,7 +4063,7 @@ void MET_scan_relation( thread_db* tdbb, jrd_rel* relation) } relation->rel_flags &= ~REL_being_scanned; - THD_rec_mutex_unlock(&dbb->dbb_sp_rec_mutex); + dbb->dbb_sp_rec_mutex.leave(); relation->rel_current_format = NULL; @@ -4080,7 +4079,7 @@ void MET_scan_relation( thread_db* tdbb, jrd_rel* relation) if (blob) BLB_close(tdbb, blob); - THD_rec_mutex_unlock(&dbb->dbb_sp_rec_mutex); + dbb->dbb_sp_rec_mutex.leave(); // Some functions inside FOR loop may throw, in which case request // remained active forever. AP: 13-may-05. diff --git a/src/jrd/nav.cpp b/src/jrd/nav.cpp index ae8bbcd01c..1bce2130cd 100644 --- a/src/jrd/nav.cpp +++ b/src/jrd/nav.cpp @@ -47,7 +47,6 @@ #include "../jrd/mov_proto.h" #include "../jrd/nav_proto.h" #include "../jrd/rse_proto.h" -#include "../jrd/thd.h" #include "../jrd/vio_proto.h" using namespace Jrd; diff --git a/src/jrd/nbak.cpp b/src/jrd/nbak.cpp index a7d463875c..c638a07190 100644 --- a/src/jrd/nbak.cpp +++ b/src/jrd/nbak.cpp @@ -38,7 +38,6 @@ #include "err_proto.h" #include "cch_proto.h" #include "isc_proto.h" -#include "thd.h" #include "../jrd/thread_proto.h" #include "os/pio_proto.h" #include "gen/iberror.h" diff --git a/src/jrd/opt.cpp b/src/jrd/opt.cpp index 5e895f8c8b..8bebf4cfba 100644 --- a/src/jrd/opt.cpp +++ b/src/jrd/opt.cpp @@ -58,7 +58,6 @@ #include "../jrd/sort.h" #include "../jrd/rse.h" #include "../jrd/intl.h" -#include "../jrd/thd.h" #include "../jrd/gdsassert.h" #include "../jrd/btr_proto.h" #include "../jrd/cch_proto.h" diff --git a/src/jrd/os/posix/isc_ipc.cpp b/src/jrd/os/posix/isc_ipc.cpp index 41daef8a6f..bbe43a9e85 100644 --- a/src/jrd/os/posix/isc_ipc.cpp +++ b/src/jrd/os/posix/isc_ipc.cpp @@ -47,8 +47,8 @@ #include "../jrd/isc_proto.h" #include "../jrd/os/isc_i_proto.h" #include "../jrd/isc_s_proto.h" -#include "../jrd/thd.h" #include "../common/classes/locks.h" +#include "../common/classes/init.h" #ifdef HAVE_VFORK_H #include @@ -115,7 +115,7 @@ static bool initialized_signals = false; static SIG volatile signals = NULL; static SLONG volatile overflow_count = 0; -static Firebird::Mutex sig_mutex; +static Firebird::GlobalPtr sig_mutex; static int process_id = 0; @@ -295,7 +295,7 @@ static bool isc_signal2( if (!process_id) process_id = getpid(); - sig_mutex.enter(); + Firebird::MutexLockGuard guard(sig_mutex); /* See if this signal has ever been cared about before */ @@ -338,8 +338,6 @@ static bool isc_signal2( que_signal(signal_number, handler, arg, flags, old_sig_w_siginfo); - sig_mutex.leave(); - return rc; } @@ -362,7 +360,7 @@ void ISC_signal_cancel( SIG sig; volatile SIG* ptr; - sig_mutex.enter(); + Firebird::MutexLockGuard guard(sig_mutex); for (ptr = &signals; sig = *ptr;) { if (sig->sig_signal == signal_number && @@ -375,9 +373,6 @@ void ISC_signal_cancel( else ptr = &(*ptr)->sig_next; } - - sig_mutex.leave(); - } diff --git a/src/jrd/os/posix/unix.cpp b/src/jrd/os/posix/unix.cpp index 7958abd220..6f3ee9a830 100644 --- a/src/jrd/os/posix/unix.cpp +++ b/src/jrd/os/posix/unix.cpp @@ -64,7 +64,6 @@ #include "../jrd/mov_proto.h" #include "../jrd/ods_proto.h" #include "../jrd/os/pio_proto.h" -#include "../jrd/thd.h" #include "../common/classes/init.h" using namespace Jrd; diff --git a/src/jrd/os/thd_priority.h b/src/jrd/os/thd_priority.h index eaa75c8f86..d5f336e945 100644 --- a/src/jrd/os/thd_priority.h +++ b/src/jrd/os/thd_priority.h @@ -39,7 +39,6 @@ #include "../common/classes/alloc.h" #include "../common/classes/init.h" #include "../common/classes/array.h" -#include "../jrd/thd.h" #include #include @@ -63,7 +62,7 @@ private: Firebird::InlineStorage > TpsPointers; enum OperationMode {Running, Stopping, ShutdownComplete}; - static Firebird::Mutex mutex; // locks modification of thps chains + static Firebird::GlobalPtr mutex; // locks modification of thps chains static ThreadPriorityScheduler* chain; // where starts thps chain static Firebird::InitMutex initialized; static OperationMode opMode; // current mode diff --git a/src/jrd/os/vms/vmsevent.cpp b/src/jrd/os/vms/vmsevent.cpp index 0694338dcc..c0a0e22365 100644 --- a/src/jrd/os/vms/vmsevent.cpp +++ b/src/jrd/os/vms/vmsevent.cpp @@ -24,7 +24,6 @@ #include "firebird.h" #include "../jrd/common.h" #include "gen/iberror.h" -#include "../jrd/thd.h" #include "../jrd/gdsassert.h" #include "../jrd/event_proto.h" #include "../jrd/gds_proto.h" diff --git a/src/jrd/os/win32/fbsyslog.cpp b/src/jrd/os/win32/fbsyslog.cpp index f937d94bc3..e24be3008d 100644 --- a/src/jrd/os/win32/fbsyslog.cpp +++ b/src/jrd/os/win32/fbsyslog.cpp @@ -46,7 +46,7 @@ private: tReportEvent *fReportEvent; bool InitFlag; public: - SyslogAccess() { + SyslogAccess(Firebird::MemoryPool&) { InitializeCriticalSection(&cs); InitFlag = false; LogHandle = 0; @@ -84,7 +84,7 @@ void SyslogAccess::Record(WORD wType, const Firebird::string& Msg) LeaveCriticalSection(&cs); } -class SyslogAccess iSyslogAccess; +Firebird::InitInstance iSyslogAccess; } // namespace namespace Firebird { @@ -100,6 +100,6 @@ namespace Firebird { wType = EVENTLOG_ERROR_TYPE; break; } - iSyslogAccess.Record(wType, Msg); + iSyslogAccess().Record(wType, Msg); } } // namespace Firebird diff --git a/src/jrd/os/win32/isc_ipc.cpp b/src/jrd/os/win32/isc_ipc.cpp index 6fe03fd3a2..401b7a9514 100644 --- a/src/jrd/os/win32/isc_ipc.cpp +++ b/src/jrd/os/win32/isc_ipc.cpp @@ -45,7 +45,6 @@ #include "../jrd/isc_proto.h" #include "../jrd/os/isc_i_proto.h" #include "../jrd/isc_s_proto.h" -#include "../jrd/thd.h" #include #include @@ -67,8 +66,6 @@ static USHORT initialized_signals = FALSE; static SLONG volatile overflow_count = 0; -static Firebird::Mutex sig_mutex; - static int process_id = 0; const USHORT MAX_OPN_EVENTS = 40; diff --git a/src/jrd/os/win32/thd_priority.cpp b/src/jrd/os/win32/thd_priority.cpp index 2c071a7ec4..d7c1732b59 100644 --- a/src/jrd/os/win32/thd_priority.cpp +++ b/src/jrd/os/win32/thd_priority.cpp @@ -49,7 +49,7 @@ // #define DEBUG_THREAD_PSCHED -Firebird::Mutex ThreadPriorityScheduler::mutex; +Firebird::GlobalPtr ThreadPriorityScheduler::mutex; ThreadPriorityScheduler* ThreadPriorityScheduler::chain = 0; Firebird::InitMutex ThreadPriorityScheduler::initialized; ThreadPriorityScheduler::OperationMode @@ -109,9 +109,8 @@ void ThreadPriorityScheduler::Cleanup(void*) void ThreadPriorityScheduler::cleanup() { - mutex.enter(); + Firebird::MutexLockGuard guard(mutex); opMode = Stopping; - mutex.leave(); } void ThreadPriorityScheduler::attach() @@ -125,10 +124,11 @@ void ThreadPriorityScheduler::attach() { Firebird::system_call_failed::raise("DuplicateHandle", GetLastError()); } - mutex.enter(); - next = chain; - chain = this; - mutex.leave(); + { + Firebird::MutexLockGuard guard(mutex); + next = chain; + chain = this; + } #ifdef DEBUG_THREAD_PSCHED gds__log("^ handle=%p priority=%d", handle, flags & THPS_BOOSTED ? @@ -152,7 +152,7 @@ void ThreadPriorityScheduler::detach() { if (active) { - mutex.enter(); + Firebird::MutexLockGuard guard(mutex); TLS_SET(currentScheduler, 0); if (opMode == ShutdownComplete) { @@ -173,7 +173,6 @@ void ThreadPriorityScheduler::detach() { toDetach->add(this); } - mutex.leave(); } else { @@ -269,9 +268,8 @@ unsigned int __stdcall ThreadPriorityScheduler::schedulerMain(LPVOID) if (toDetach->getCount() > 0) { - mutex.enter(); + Firebird::MutexLockGuard guard(mutex); doDetach(); - mutex.leave(); } } return 0; diff --git a/src/jrd/pag.cpp b/src/jrd/pag.cpp index 96143c559f..ca9059ea04 100644 --- a/src/jrd/pag.cpp +++ b/src/jrd/pag.cpp @@ -96,7 +96,6 @@ #include "../jrd/ods_proto.h" #include "../jrd/pag_proto.h" #include "../jrd/os/pio_proto.h" -#include "../jrd/thd.h" #include "../jrd/thread_proto.h" #include "../jrd/isc_f_proto.h" #include "../jrd/TempSpace.h" diff --git a/src/jrd/par.cpp b/src/jrd/par.cpp index 5f12aad2d5..e6ad805750 100644 --- a/src/jrd/par.cpp +++ b/src/jrd/par.cpp @@ -63,7 +63,6 @@ #include "../jrd/gds_proto.h" #include "../jrd/met_proto.h" #include "../jrd/par_proto.h" -#include "../jrd/thd.h" #include "../common/utils_proto.h" #include "../jrd/SysFunction.h" diff --git a/src/jrd/pcmet.epp b/src/jrd/pcmet.epp index 9f9d14b9e9..05ed303d64 100644 --- a/src/jrd/pcmet.epp +++ b/src/jrd/pcmet.epp @@ -48,7 +48,6 @@ #include "../jrd/met_proto.h" #include "../jrd/mov_proto.h" #include "../jrd/pcmet_proto.h" -#include "../jrd/thd.h" using namespace Jrd; diff --git a/src/jrd/pwd.cpp b/src/jrd/pwd.cpp index e61b270f1b..dccd90acb7 100644 --- a/src/jrd/pwd.cpp +++ b/src/jrd/pwd.cpp @@ -35,7 +35,6 @@ #include "../jrd/err_proto.h" #include "../jrd/gds_proto.h" #include "../jrd/sch_proto.h" -#include "../jrd/thd.h" #include "../jrd/thread_proto.h" #include "../jrd/jrd_proto.h" #include "../jrd/scl.h" diff --git a/src/jrd/qatest.cpp b/src/jrd/qatest.cpp index 9c6789e549..037d04c0a4 100644 --- a/src/jrd/qatest.cpp +++ b/src/jrd/qatest.cpp @@ -101,7 +101,6 @@ defined APIs for this function. #include "../jrd/err_proto.h" #include "../jrd/flu_proto.h" #include "../jrd/sch_proto.h" -#include "../jrd/thd.h" #include "../jrd/thread_proto.h" #ifdef WIN_NT #include diff --git a/src/jrd/rse.cpp b/src/jrd/rse.cpp index 156757718d..5c9243f749 100644 --- a/src/jrd/rse.cpp +++ b/src/jrd/rse.cpp @@ -66,7 +66,6 @@ #include "../jrd/rlck_proto.h" #include "../jrd/rse_proto.h" #include "../jrd/sort_proto.h" -#include "../jrd/thd.h" #include "../jrd/vio_proto.h" #include "../jrd/VirtualTable.h" diff --git a/src/jrd/sch.cpp b/src/jrd/sch.cpp index b0bbff5374..53ec0b750c 100644 --- a/src/jrd/sch.cpp +++ b/src/jrd/sch.cpp @@ -40,6 +40,7 @@ #include "../jrd/gdsassert.h" #include "../jrd/os/thd_priority.h" #include "../common/classes/locks.h" +#include "../common/classes/init.h" #ifdef WIN_NT #include @@ -76,14 +77,14 @@ static void stall_ast(THREAD); static THREAD free_threads = NULL; static THREAD active_thread = NULL; static THREAD ast_thread = NULL; -static Firebird::Mutex thread_mutex; +static Firebird::GlobalPtr thread_mutex; namespace { class SchedulerInit { public: - SchedulerInit() + SchedulerInit(Firebird::MemoryPool&) {} ~SchedulerInit() @@ -123,14 +124,14 @@ public: } // namespace -static SchedulerInit initHolder; +static Firebird::GlobalPtr initHolder; static inline void sch_mutex_lock() { try { - thread_mutex.enter(); + thread_mutex->enter(); } catch (const Firebird::system_call_failed& e) { @@ -142,7 +143,7 @@ static inline void sch_mutex_unlock() { try { - thread_mutex.leave(); + thread_mutex->leave(); } catch (const Firebird::system_call_failed& e) { diff --git a/src/jrd/sch_proto.h b/src/jrd/sch_proto.h index f53566b14d..4a70232ab7 100644 --- a/src/jrd/sch_proto.h +++ b/src/jrd/sch_proto.h @@ -24,8 +24,6 @@ #ifndef JRD_SCH_PROTO_H #define JRD_SCH_PROTO_H -#include "../jrd/isc.h" - extern "C" { /* AST actions taken by SCH_ast() */ @@ -48,6 +46,11 @@ void API_ROUTINE gds__thread_exit(); } // extern "C" +// Thread quanta + +const int QUANTUM = 100; // Default quantum +const int SWEEP_QUANTUM = 10; // Make sweeps less disruptive + struct thread; void SCH_ast(enum ast_t); diff --git a/src/jrd/scl.epp b/src/jrd/scl.epp index d46076ddf2..8b7f311506 100644 --- a/src/jrd/scl.epp +++ b/src/jrd/scl.epp @@ -58,7 +58,6 @@ #include "../jrd/met_proto.h" #include "../jrd/grant_proto.h" #include "../jrd/scl_proto.h" -#include "../jrd/thd.h" #include "../jrd/constants.h" #include "../include/fb_exception.h" #include "../common/utils_proto.h" diff --git a/src/jrd/sdw.cpp b/src/jrd/sdw.cpp index 12e1eba761..12f1fd5e2c 100644 --- a/src/jrd/sdw.cpp +++ b/src/jrd/sdw.cpp @@ -49,7 +49,6 @@ #include "../jrd/pag_proto.h" #include "../jrd/os/pio_proto.h" #include "../jrd/sdw_proto.h" -#include "../jrd/thd.h" using namespace Jrd; diff --git a/src/jrd/shut.cpp b/src/jrd/shut.cpp index 60dafe3697..edd15758af 100644 --- a/src/jrd/shut.cpp +++ b/src/jrd/shut.cpp @@ -36,7 +36,6 @@ #include "../jrd/rlck_proto.h" #include "../jrd/sch_proto.h" #include "../jrd/shut_proto.h" -#include "../jrd/thd.h" #include "../jrd/thread_proto.h" #include "../jrd/tra_proto.h" diff --git a/src/jrd/sqz.cpp b/src/jrd/sqz.cpp index da6cd9e1f7..588b1c77b5 100644 --- a/src/jrd/sqz.cpp +++ b/src/jrd/sqz.cpp @@ -30,7 +30,6 @@ #include "../jrd/err_proto.h" #include "../jrd/gds_proto.h" #include "../jrd/sqz_proto.h" -#include "../jrd/thd.h" using namespace Jrd; diff --git a/src/jrd/svc.cpp b/src/jrd/svc.cpp index 506a5afbfb..f1a79147fb 100644 --- a/src/jrd/svc.cpp +++ b/src/jrd/svc.cpp @@ -34,7 +34,6 @@ #include #include #include "../jrd/common.h" -#include "../jrd/thd.h" #include "../jrd/file_params.h" #include #include "../jrd/jrd.h" @@ -62,6 +61,7 @@ #include "../utilities/gsec/gsecswi.h" #include "../utilities/gstat/dbaswi.h" #include "../common/classes/alloc.h" +#include "../common/classes/init.h" #include "../common/classes/ClumpletWriter.h" #include "../jrd/ibase.h" #include "../common/utils_proto.h" @@ -408,7 +408,7 @@ static ULONG shutdown_param = 0L; const char* const SPB_SEC_USERNAME = "isc_spb_sec_username"; -static Firebird::Mutex svc_mutex, thd_mutex; +static Firebird::GlobalPtr svc_mutex, thd_mutex; /* Service Functions */ #if !defined(BOOT_BUILD) @@ -1780,23 +1780,25 @@ void* SVC_start(Service* service, USHORT spb_length, const SCHAR* spb_data) ERR_post(isc_bad_spb_form, 0); } - thd_mutex.enter(); - if (service->svc_flags & SVC_thd_running) { - thd_mutex.leave(); - ERR_post(isc_svc_in_use, isc_arg_string, - error_string(serv->serv_name, strlen(serv->serv_name)), - 0); + { // scope for locked thd_mutex + Firebird::MutexLockGuard guard(thd_mutex); + + if (service->svc_flags & SVC_thd_running) { + ERR_post(isc_svc_in_use, isc_arg_string, + error_string(serv->serv_name, strlen(serv->serv_name)), + 0); + } + + /* Another service may have been started with this service block. + * If so, we must reset the service flags. + */ + service->svc_switches.erase(); + if (!(service->svc_flags & SVC_detached)) + { + service->svc_flags = 0; + } + service->svc_flags |= SVC_thd_running; } - /* Another service may have been started with this service block. - * If so, we must reset the service flags. - */ - service->svc_switches.erase(); - if (!(service->svc_flags & SVC_detached)) - { - service->svc_flags = 0; - } - service->svc_flags |= SVC_thd_running; - thd_mutex.leave(); thread_db* tdbb = JRD_get_thread_data(); @@ -2319,7 +2321,7 @@ void SVC_finish(Service* service, USHORT flag) * **************************************/ - svc_mutex.enter(); + Firebird::MutexLockGuard guard(svc_mutex); if (service && ((flag == SVC_finished) || (flag == SVC_detached))) { service->svc_flags |= flag; @@ -2338,7 +2340,6 @@ void SVC_finish(Service* service, USHORT flag) service->svc_handle = 0; } } - svc_mutex.leave(); } diff --git a/src/jrd/svc.h b/src/jrd/svc.h index 10855a280c..7c525e1f25 100644 --- a/src/jrd/svc.h +++ b/src/jrd/svc.h @@ -26,7 +26,6 @@ #include -#include "../jrd/thd.h" #include "../jrd/jrd_pwd.h" #include "../jrd/isc.h" #include "../jrd/svc_undoc.h" diff --git a/src/jrd/thread_proto.h b/src/jrd/thread_proto.h index b524444ffa..919546f21d 100644 --- a/src/jrd/thread_proto.h +++ b/src/jrd/thread_proto.h @@ -1,7 +1,7 @@ #ifndef JRD_THREAD_PROTO_H #define JRD_THREAD_PROTO_H -#include "../jrd/thd.h" +#include "../common/thd.h" #include "../jrd/sch_proto.h" inline void THREAD_ENTER() { @@ -22,12 +22,12 @@ class SchedulerContext { public: SchedulerContext() { - THREAD_ENTER(); + SCH_enter(); } ~SchedulerContext() { - THREAD_EXIT(); + SCH_exit(); } private: diff --git a/src/jrd/tpc.cpp b/src/jrd/tpc.cpp index ec133d64ce..9da145b8b6 100644 --- a/src/jrd/tpc.cpp +++ b/src/jrd/tpc.cpp @@ -37,7 +37,6 @@ #include "../jrd/gds_proto.h" #include "../jrd/lck_proto.h" #include "../jrd/mov_proto.h" -#include "../jrd/thd.h" #include "../jrd/tpc_proto.h" #include "../jrd/tra_proto.h" #include "../common/classes/auto.h" diff --git a/src/jrd/unicode_util.cpp b/src/jrd/unicode_util.cpp index 82b9b0c4b6..1fa679ee18 100644 --- a/src/jrd/unicode_util.cpp +++ b/src/jrd/unicode_util.cpp @@ -101,15 +101,22 @@ public: class UnicodeUtil::ICUModules { public: + ICUModules(MemoryPool&) + { + } + ~ICUModules() { for (bool found = modules().getFirst(); found; found = modules().getNext()) delete modules().current()->second; } - InitInstance > > > modules; RWLock lock; -} icuModules; +}; + +namespace { + GlobalPtr icuModules; +} static const char* const COLL_30_VERSION = "41.128.4.4"; // ICU 3.0 collator version @@ -749,10 +756,10 @@ UnicodeUtil::ICU* UnicodeUtil::loadICU(const Firebird::string& icuVersion, if (version != majorVersion + "." + minorVersion) continue; - ReadLockGuard readGuard(icuModules.lock); + ReadLockGuard readGuard(icuModules->lock); ICU* icu; - if (icuModules.modules().get(version, icu)) + if (icuModules->modules().get(version, icu)) return icu; PathName filename; @@ -856,18 +863,18 @@ UnicodeUtil::ICU* UnicodeUtil::loadICU(const Firebird::string& icuVersion, // RWLock don't allow lock upgrade (read->write) so we // release read and acquire a write lock. readGuard.release(); - WriteLockGuard writeGuard(icuModules.lock); + WriteLockGuard writeGuard(icuModules->lock); // In this small amount of time, one may already loaded the // same version, so withing the write lock we verify again. ICU* icu2; - if (icuModules.modules().get(version, icu2)) + if (icuModules->modules().get(version, icu2)) { delete icu; return icu2; } - icuModules.modules().put(version, icu); + icuModules->modules().put(version, icu); return icu; } diff --git a/src/jrd/unicode_util.h b/src/jrd/unicode_util.h index 8e8e019d65..c4c7c2ad7a 100644 --- a/src/jrd/unicode_util.h +++ b/src/jrd/unicode_util.h @@ -37,9 +37,9 @@ class UnicodeUtil { private: struct ICU; - class ICUModules; public: + class ICUModules; // routines semantically equivalent with intlobj_new.h static USHORT utf16KeyLength(USHORT len); // BOCU-1 diff --git a/src/jrd/validation.cpp b/src/jrd/validation.cpp index d5c052df60..1c67950606 100644 --- a/src/jrd/validation.cpp +++ b/src/jrd/validation.cpp @@ -566,7 +566,6 @@ VI. ADDITIONAL NOTES #include "../jrd/gds_proto.h" #include "../jrd/met_proto.h" #include "../jrd/sch_proto.h" -#include "../jrd/thd.h" #include "../jrd/tra_proto.h" #include "../jrd/val_proto.h" #include "../jrd/thread_proto.h" diff --git a/src/jrd/why.cpp b/src/jrd/why.cpp index 50aabc5ec3..f79838238b 100644 --- a/src/jrd/why.cpp +++ b/src/jrd/why.cpp @@ -66,7 +66,6 @@ #include "gen/msg_facs.h" #include "../jrd/acl.h" #include "../jrd/inf_pub.h" -#include "../jrd/thd.h" #include "../jrd/isc.h" #include "../jrd/fil.h" #include "../jrd/flu.h" @@ -203,9 +202,9 @@ namespace YValve { typedef Firebird::BePlusTree HandleMapping; - static Firebird::AutoPtr handleMapping; + static Firebird::GlobalPtr handleMapping; static ULONG handle_sequence_number = 0; - static Firebird::RWLock handleMappingLock; + static Firebird::GlobalPtr handleMappingLock; static Firebird::InitInstance > attachments; @@ -215,12 +214,9 @@ namespace YValve { fb_assert(par || (imp != USHORT(~0))); - handleMappingLock.beginWrite(); - try - { - if (!handleMapping) - handleMapping = FB_NEW(*getDefaultMemoryPool()) - HandleMapping(getDefaultMemoryPool()); + { // scope for write lock on handleMappingLock + Firebird::WriteLockGuard sync(handleMappingLock); + fb_assert(handleMapping); // Loop until we find an empty handle slot. // This is to care of case when counter rolls over do { @@ -234,12 +230,6 @@ namespace YValve temp = ++handle_sequence_number; public_handle = (FB_API_HANDLE)(IPTR)temp; } while (!handleMapping->add(this)); - - handleMappingLock.endWrite(); - } - catch (const Firebird::Exception&) { - handleMappingLock.endWrite(); - throw; } if (pub) @@ -251,20 +241,18 @@ namespace YValve BaseHandle* BaseHandle::translate(FB_API_HANDLE handle) { Firebird::ReadLockGuard sync(handleMappingLock); - if (handleMapping) + fb_assert(handleMapping); + HandleMapping::Accessor accessor(&handleMapping); + if (accessor.locate(handle)) { - HandleMapping::Accessor accessor(handleMapping); - if (accessor.locate(handle)) + BaseHandle* h = accessor.current(); + if (h->flags & HANDLE_shutdown) { - BaseHandle* h = accessor.current(); - if (h->flags & HANDLE_shutdown) - { - Firebird::status_exception::raise(isc_shutdown, isc_arg_string, - h->parent ? h->parent->db_path.c_str() : "(unknown)", - isc_arg_end); - } - return h; + Firebird::status_exception::raise(isc_shutdown, isc_arg_string, + h->parent ? h->parent->db_path.c_str() : "(unknown)", + isc_arg_end); } + return h; } return 0; @@ -294,7 +282,8 @@ namespace YValve Firebird::WriteLockGuard sync(handleMappingLock); // Silently ignore bad handles for PROD_BUILD - if (handleMapping && handleMapping->locate(public_handle)) + fb_assert(handleMapping); + if (handleMapping->locate(public_handle)) { handleMapping->fastRemove(); } diff --git a/src/lock/lock.cpp b/src/lock/lock.cpp index 1b92e12811..cfc97cd397 100644 --- a/src/lock/lock.cpp +++ b/src/lock/lock.cpp @@ -58,6 +58,7 @@ #include "../jrd/thread_proto.h" #include "../common/config/config.h" #include "../common/classes/semaphore.h" +#include "../common/classes/init.h" #include @@ -220,8 +221,8 @@ static int LOCK_pid = 0; static SH_MEM_T LOCK_data; #ifdef USE_BLOCKING_THREAD -static Firebird::Semaphore cleanupSemaphore; -static Firebird::Semaphore startupSemaphore; +static Firebird::GlobalPtr cleanupSemaphore; +static Firebird::GlobalPtr startupSemaphore; #endif #ifdef WIN_NT @@ -1430,7 +1431,7 @@ static THREAD_ENTRY_DECLARE blocking_action_thread(THREAD_ENTRY_PARAM arg) { if (atStartup) { - startupSemaphore.release(); + startupSemaphore->release(); } break; } @@ -1448,7 +1449,7 @@ static THREAD_ENTRY_DECLARE blocking_action_thread(THREAD_ENTRY_PARAM arg) if (atStartup) { atStartup = false; - startupSemaphore.release(); + startupSemaphore->release(); } event_t* event_ptr = &LOCK_owner->own_blocking; @@ -1466,7 +1467,7 @@ static THREAD_ENTRY_DECLARE blocking_action_thread(THREAD_ENTRY_PARAM arg) /* Main thread won't wait forever, so check LOCK_owner is still mapped */ if (LOCK_owner) - cleanupSemaphore.release(); + cleanupSemaphore->release(); return 0; } @@ -3233,7 +3234,7 @@ static void shutdown_blocking_thread(ISC_STATUS* status_vector) LOCK_owner->own_flags |= OWN_waiting; // wait for AST thread to start (or 5 secs) - startupSemaphore.tryEnter(5); + startupSemaphore->tryEnter(5); // Wakeup the AST thread - it might be blocking or stalled ISC_event_post(&LOCK_owner->own_blocking); @@ -3245,7 +3246,7 @@ static void shutdown_blocking_thread(ISC_STATUS* status_vector) AST_ENABLE(); // Wait for the AST thread to finish cleanup or for 5 seconds - cleanupSemaphore.tryEnter(5); + cleanupSemaphore->tryEnter(5); /* Either AST thread terminated, or our timeout expired */ diff --git a/src/remote/allr.cpp b/src/remote/allr.cpp index 6e81e12df0..4ad6078d6d 100644 --- a/src/remote/allr.cpp +++ b/src/remote/allr.cpp @@ -29,7 +29,7 @@ #include "../remote/allr_proto.h" #include "../remote/remot_proto.h" #include "../jrd/gds_proto.h" -#include "../jrd/thd.h" +#include "../common/thd.h" #include "../common/classes/alloc.h" diff --git a/src/remote/inet.cpp b/src/remote/inet.cpp index 4569b63c37..6215fc498e 100644 --- a/src/remote/inet.cpp +++ b/src/remote/inet.cpp @@ -53,6 +53,7 @@ #include #include "../common/classes/timestamp.h" +#include "../common/classes/init.h" #ifdef HAVE_PWD_H #include @@ -102,7 +103,7 @@ const int INET_RETRY_CALL = 5; #include "../remote/remote.h" #include "../jrd/ibase.h" #include "../jrd/iberr.h" -#include "../jrd/thd.h" +#include "../common/thd.h" #include "../remote/inet_proto.h" #include "../remote/proto_proto.h" #include "../remote/remot_proto.h" @@ -383,18 +384,18 @@ static WSADATA INET_wsadata; #define INTERRUPT_ERROR(x) (SYSCALL_INTERRUPTED(x)) #endif -static Firebird::Mutex port_mutex; +static Firebird::GlobalPtr port_mutex; inline void START_PORT_CRITICAL() { THREAD_EXIT(); - port_mutex.enter(); + port_mutex->enter(); THREAD_ENTER(); } inline void STOP_PORT_CRITICAL() { - port_mutex.leave(); + port_mutex->leave(); } diff --git a/src/remote/inet_server.cpp b/src/remote/inet_server.cpp index c31d4a3339..abe0dfce3a 100644 --- a/src/remote/inet_server.cpp +++ b/src/remote/inet_server.cpp @@ -38,6 +38,7 @@ #include "../jrd/isc_proto.h" #include "../jrd/divorce.h" #include "../jrd/jrd_proto.h" +#include "../common/classes/init.h" #include "../common/config/config.h" #include @@ -80,7 +81,7 @@ #include "../remote/remote.h" #include "../jrd/license.h" -#include "../jrd/thd.h" +#include "../common/thd.h" #include "../jrd/file_params.h" #include "../remote/inet_proto.h" #include "../remote/serve_proto.h" @@ -518,7 +519,7 @@ static void signal_sigpipe_handler(int) #endif #ifdef SHUTDOWN_THREAD -static Firebird::SignalSafeSemaphore shutSem; +static Firebird::GlobalPtr shutSem; static bool alreadyClosing = false; static THREAD_ENTRY_DECLARE shutdown_thread(THREAD_ENTRY_PARAM arg) @@ -535,7 +536,7 @@ static THREAD_ENTRY_DECLARE shutdown_thread(THREAD_ENTRY_PARAM arg) * **************************************/ try { - shutSem.enter(); + shutSem->enter(); } catch (Firebird::status_exception& e) { @@ -572,7 +573,7 @@ static void signal_term(int) **************************************/ try { - shutSem.release(); + shutSem->release(); } catch (Firebird::status_exception& e) { @@ -600,6 +601,6 @@ static void shutdown_fini() set_signal(SIGINT, SIG_IGN); set_signal(SIGTERM, SIG_IGN); alreadyClosing = true; - shutSem.release(); + shutSem->release(); } #endif //SHUTDOWN_THREAD diff --git a/src/remote/os/win32/cntl.cpp b/src/remote/os/win32/cntl.cpp index 6356206fdb..1bc9ecd595 100644 --- a/src/remote/os/win32/cntl.cpp +++ b/src/remote/os/win32/cntl.cpp @@ -53,10 +53,10 @@ static USHORT report_status(DWORD, DWORD, DWORD, DWORD); static ThreadEntryPoint* main_handler; static SERVICE_STATUS_HANDLE service_handle; -static Firebird::string* service_name = NULL; -static Firebird::string* mutex_name = NULL; +static Firebird::GlobalPtr service_name; +static Firebird::GlobalPtr mutex_name; static HANDLE stop_event_handle; -static Firebird::Mutex thread_mutex; +static Firebird::GlobalPtr thread_mutex; static cntl_thread* threads = NULL; static HANDLE hMutex = NULL; @@ -74,10 +74,7 @@ void CNTL_init(ThreadEntryPoint* handler, const TEXT* name) **************************************/ main_handler = handler; - MemoryPool& pool = *getDefaultMemoryPool(); - service_name = FB_NEW(pool) Firebird::string(pool); service_name->printf(REMOTE_SERVICE, name); - mutex_name = FB_NEW(pool) Firebird::string(pool); mutex_name->printf(GUARDIAN_MUTEX, name); } @@ -103,10 +100,9 @@ void* CNTL_insert_thread() GetCurrentProcess(), &new_thread->thread_handle, 0, FALSE, DUPLICATE_SAME_ACCESS); - thread_mutex.enter(); + Firebird::MutexLockGuard guard(thread_mutex); new_thread->thread_next = threads; threads = new_thread; - thread_mutex.leave(); return new_thread; } @@ -188,18 +184,19 @@ void CNTL_remove_thread(void* athread) * Functional description * **************************************/ - thread_mutex.enter(); - cntl_thread* const rem_thread = (cntl_thread*) athread; + { // scope for lock on thread_mutex + Firebird::MutexLockGuard guard(thread_mutex); + cntl_thread* const rem_thread = (cntl_thread*) athread; - for (cntl_thread** thread_ptr = &threads; - *thread_ptr; thread_ptr = &(*thread_ptr)->thread_next) - { - if (*thread_ptr == rem_thread) { - *thread_ptr = rem_thread->thread_next; - break; + for (cntl_thread** thread_ptr = &threads; + *thread_ptr; thread_ptr = &(*thread_ptr)->thread_next) + { + if (*thread_ptr == rem_thread) { + *thread_ptr = rem_thread->thread_next; + break; + } } } - thread_mutex.leave(); CloseHandle(rem_thread->thread_handle); diff --git a/src/remote/os/win32/property.cpp b/src/remote/os/win32/property.cpp index 8571477e57..9ac153115f 100644 --- a/src/remote/os/win32/property.cpp +++ b/src/remote/os/win32/property.cpp @@ -58,7 +58,7 @@ #include "../remote/os/win32/ibsvrhlp.h" #include "../remote/os/win32/chop_proto.h" -#include "../jrd/thd.h" /* get jrd_proto.h to declare the function */ +#include "../common/thd.h" /* get jrd_proto.h to declare the function */ #include "../jrd/jrd_proto.h" /* JRD_num_attachments() */ #include /* sprintf() */ diff --git a/src/remote/os/win32/window.cpp b/src/remote/os/win32/window.cpp index 3f50ba3e4a..18f450604e 100644 --- a/src/remote/os/win32/window.cpp +++ b/src/remote/os/win32/window.cpp @@ -33,7 +33,7 @@ #include "../jrd/svc_proto.h" #include "../jrd/sch_proto.h" -#include "../jrd/thd.h" +#include "../common/thd.h" #include "../jrd/thread_proto.h" #include "../jrd/jrd_proto.h" #include "../remote/os/win32/window_proto.h" diff --git a/src/remote/os/win32/wnet.cpp b/src/remote/os/win32/wnet.cpp index de70718288..3547639225 100644 --- a/src/remote/os/win32/wnet.cpp +++ b/src/remote/os/win32/wnet.cpp @@ -31,7 +31,7 @@ #include #include "../remote/remote.h" #include "../jrd/ibase.h" -#include "../jrd/thd.h" +#include "../common/thd.h" #include "../jrd/iberr.h" #include "../utilities/install/install_nt.h" diff --git a/src/remote/remote.cpp b/src/remote/remote.cpp index 675081364c..3e5967261e 100644 --- a/src/remote/remote.cpp +++ b/src/remote/remote.cpp @@ -34,7 +34,6 @@ #include "../remote/remot_proto.h" #include "../remote/xdr_proto.h" #include "../jrd/gds_proto.h" -#include "../jrd/thd.h" #include "../jrd/thread_proto.h" #include "../common/config/config.h" diff --git a/src/remote/remote.h b/src/remote/remote.h index e867a84e75..e894e78bad 100644 --- a/src/remote/remote.h +++ b/src/remote/remote.h @@ -33,7 +33,7 @@ #include "../remote/allr_proto.h" #include "../remote/remote_def.h" #include "../jrd/ThreadData.h" -#include "../jrd/thd.h" +#include "../common/thd.h" #include "../common/classes/objects_array.h" #include "../auth/trusted/AuthSspi.h" #include "../common/classes/fb_string.h" diff --git a/src/remote/serve_proto.h b/src/remote/serve_proto.h index 0fc351845d..8dd0da8ec0 100644 --- a/src/remote/serve_proto.h +++ b/src/remote/serve_proto.h @@ -32,8 +32,6 @@ extern "C" { #define rem_port void #endif -#include "../jrd/thd.h" - void SRVR_main(rem_port*, USHORT); void SRVR_multi_thread(rem_port*, USHORT); void SRVR_shutdown(); diff --git a/src/remote/server.cpp b/src/remote/server.cpp index a4b6f469c2..e29bfd76b8 100644 --- a/src/remote/server.cpp +++ b/src/remote/server.cpp @@ -54,6 +54,7 @@ #include "../jrd/thread_proto.h" #include "../jrd/why_proto.h" #include "../jrd/constants.h" +#include "../common/classes/init.h" #include "../common/classes/semaphore.h" #include "../common/classes/ClumpletWriter.h" #include "../common/config/config.h" @@ -226,7 +227,7 @@ static SERVER_REQ active_requests = NULL; static bool shutting_down = false; static SRVR servers; -static Firebird::Semaphore requests_semaphore; +static Firebird::GlobalPtr requests_semaphore; static const UCHAR request_info[] = @@ -548,7 +549,7 @@ void SRVR_multi_thread( rem_port* main_port, USHORT flags) } REMOTE_TRACE(("Post event")); - requests_semaphore.release(); + requests_semaphore->release(); } request = 0; } @@ -5502,7 +5503,7 @@ static THREAD_ENTRY_DECLARE loopThread(THREAD_ENTRY_PARAM flags) THREAD_EXIT(); /* Wait for 1 minute (60 seconds) on a new request */ REMOTE_TRACE(("Wait for event")); - if (!requests_semaphore.tryEnter(60)) { + if (!requests_semaphore->tryEnter(60)) { REMOTE_TRACE(("timeout!")); timedout_count++; } @@ -5548,7 +5549,7 @@ void SRVR_shutdown() const int limit = threads_waiting; for (int i = 0; i < limit; i++) { - requests_semaphore.release(); + requests_semaphore->release(); } // let them terminate diff --git a/src/remote/xnet.cpp b/src/remote/xnet.cpp index 5ec6fe9912..5ae89ccfcf 100644 --- a/src/remote/xnet.cpp +++ b/src/remote/xnet.cpp @@ -27,7 +27,7 @@ #include #include "../remote/remote.h" #include "../jrd/ibase.h" -#include "../jrd/thd.h" +#include "../common/thd.h" #include "../jrd/iberr.h" #include "../remote/xnet.h" #include "../utilities/install/install_nt.h" @@ -158,14 +158,14 @@ inline void make_event_name(char* buffer, size_t size, const char* format, ULONG fb_utils::snprintf(buffer, size, format, xnet_endpoint, arg1, arg2, arg3); } -static Firebird::Mutex xnet_mutex; +static Firebird::GlobalPtr xnet_mutex; inline void XNET_LOCK() { if (!xnet_shutdown) { THREAD_EXIT(); } - xnet_mutex.enter(); + xnet_mutex->enter(); if (!xnet_shutdown) { THREAD_ENTER(); @@ -173,7 +173,7 @@ inline void XNET_LOCK() { } inline void XNET_UNLOCK() { - xnet_mutex.leave(); + xnet_mutex->leave(); } static int xnet_error(rem_port*, ISC_STATUS, int); diff --git a/src/utilities/gstat/dba.epp b/src/utilities/gstat/dba.epp index 88ed756fe3..c2b35332b0 100644 --- a/src/utilities/gstat/dba.epp +++ b/src/utilities/gstat/dba.epp @@ -50,7 +50,7 @@ #include "../utilities/gstat/dbaswi.h" #include "../jrd/gds_proto.h" #include "../jrd/isc_f_proto.h" -#include "../jrd/thd.h" +#include "../common/thd.h" #include "../jrd/enc_proto.h" #include "../common/utils_proto.h" #include "../common/classes/ClumpletWriter.h"