Skip to content

Commit 1346dc8

Browse files
committed
Front ported fix for bug #8850 : Firebird 3.0.13 Win32: KERNEL32.GetThreadId import breaks Windows XP (regression vs 3.0.12)
1 parent 0a00598 commit 1346dc8

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

src/common/ThreadStart.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,14 @@ ThreadId Thread::getId()
219219
#endif
220220
}
221221

222-
bool Thread::isCurrent(Handle threadHandle)
222+
bool Thread::isCurrent(InternalId iid)
223223
{
224-
static_assert(std::is_same<Handle, InternalId>().value, "type mismatch");
225-
return pthread_equal(threadHandle, pthread_self());
224+
return pthread_equal(iid, pthread_self());
226225
}
227226

228227
bool Thread::isCurrent()
229228
{
230-
return pthread_equal(internalId, pthread_self());
229+
return isCurrent(internalId);
231230
}
232231

233232
void Thread::sleep(unsigned milliseconds)
@@ -369,14 +368,14 @@ ThreadId Thread::getId()
369368
return GetCurrentThreadId();
370369
}
371370

372-
bool Thread::isCurrent(Handle threadHandle)
371+
bool Thread::isCurrent(InternalId iid)
373372
{
374-
return GetCurrentThreadId() == GetThreadId(threadHandle);
373+
return GetCurrentThreadId() == iid;
375374
}
376375

377376
bool Thread::isCurrent()
378377
{
379-
return GetCurrentThreadId() == internalId;
378+
return isCurrent(internalId);
380379
}
381380

382381
void Thread::sleep(unsigned milliseconds)

src/common/ThreadStart.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,19 @@ class Thread
8282
static void waitForCompletion(Handle& handle);
8383
static void kill(Handle& handle);
8484

85+
// Returns TID of current OS thread
8586
static ThreadId getId();
8687

88+
// Returns internal ID of given thread instance
89+
InternalId getInternalId() const
90+
{
91+
return internalId;
92+
}
93+
8794
static void sleep(unsigned milliseconds);
8895
static void yield();
8996

90-
static bool isCurrent(Handle threadHandle);
97+
static bool isCurrent(InternalId iid);
9198
bool isCurrent();
9299

93100
Thread()

src/jrd/CryptoManager.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ namespace Jrd {
331331
flDown(false),
332332
run(false)
333333
{
334+
memset(&cryptThreadId, 0, sizeof(cryptThreadId));
335+
334336
stateLock = FB_NEW_RPT(getPool(), 0)
335337
Lock(tdbb, 0, LCK_crypt_status, this, blockingAstChangeCryptState);
336338
threadLock = FB_NEW_RPT(getPool(), 0) Lock(tdbb, 0, LCK_crypt);
@@ -987,7 +989,8 @@ namespace Jrd {
987989

988990
// ready to go
989991
guard.leave(); // release in advance to avoid races with cryptThread()
990-
Thread::start(cryptThreadStatic, (THREAD_ENTRY_PARAM) this, THREAD_medium, &cryptThreadHandle);
992+
const Thread& thread = Thread::start(cryptThreadStatic, (THREAD_ENTRY_PARAM) this, THREAD_medium, &cryptThreadHandle);
993+
cryptThreadId = thread.getInternalId();
991994
}
992995
catch (const Firebird::Exception&)
993996
{

src/jrd/CryptoManager.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,11 @@ class CryptoManager FB_FINAL : public Firebird::PermanentStorage, public BarSync
301301
UCHAR getCurrentState(thread_db* tdbb) const;
302302
const char* getKeyName() const;
303303
const char* getPluginName() const;
304-
Thread::Handle getCryptThreadHandle() const
304+
305+
// Return true if crypt thread is running and it is the current thread.
306+
bool isCryptThreadCurrent() const
305307
{
306-
return cryptThreadHandle;
308+
return cryptThreadHandle && Thread::isCurrent(cryptThreadId);
307309
}
308310

309311
private:
@@ -383,6 +385,7 @@ class CryptoManager FB_FINAL : public Firebird::PermanentStorage, public BarSync
383385
Firebird::string hash;
384386
Firebird::RefPtr<DbInfo> dbInfo;
385387
Thread::Handle cryptThreadHandle;
388+
Thread::InternalId cryptThreadId;
386389
Firebird::IDbCryptPlugin* cryptPlugin;
387390
Factory* checkFactory;
388391
Database& dbb;

src/jrd/jrd.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7661,8 +7661,7 @@ void release_attachment(thread_db* tdbb, Jrd::Attachment* attachment, XThreadEns
76617661
XThreadEnsureUnlock* activeThreadGuard = dropGuard;
76627662
if (!activeThreadGuard)
76637663
{
7664-
if (dbb->dbb_crypto_manager &&
7665-
Thread::isCurrent(dbb->dbb_crypto_manager->getCryptThreadHandle()))
7664+
if (dbb->dbb_crypto_manager && dbb->dbb_crypto_manager->isCryptThreadCurrent())
76667665
{
76677666
activeThreadGuard = &dummyGuard;
76687667
}

0 commit comments

Comments
 (0)