Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/common/ThreadStart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,19 @@ ThreadId Thread::getId()
#endif
}

ThreadId Thread::getIdFromHandle(Handle threadHandle)
{
return threadHandle;
}

bool Thread::isCurrent(InternalId iid)
{
return pthread_equal(iid, pthread_self());
}

bool Thread::isCurrent()
{
return pthread_equal(internalId, pthread_self());
return isCurrent(internalId);
}

void Thread::sleep(unsigned milliseconds)
Expand Down Expand Up @@ -363,9 +373,19 @@ ThreadId Thread::getId()
return GetCurrentThreadId();
}

ThreadId Thread::getIdFromHandle(Handle threadHandle)
{
return GetThreadId(threadHandle);
}

bool Thread::isCurrent(InternalId iid)
{
return GetCurrentThreadId() == iid;
}

bool Thread::isCurrent()
{
return GetCurrentThreadId() == internalId;
return isCurrent(internalId);
}

void Thread::sleep(unsigned milliseconds)
Expand Down Expand Up @@ -410,6 +430,10 @@ Thread::Handle Thread::getId()
{
}

Thread::Handle Thread::getIdFromHandle(Handle)
{
}

void Thread::sleep(unsigned milliseconds)
{
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/ThreadStart.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ class Thread
static void kill(Handle& handle);

static ThreadId getId();
static ThreadId getIdFromHandle(Handle threadHandle);

static void sleep(unsigned milliseconds);
static void yield();

static bool isCurrent(InternalId iid);
bool isCurrent();

Thread()
Expand Down
14 changes: 7 additions & 7 deletions src/jrd/CryptoManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ namespace Jrd {
keyConsumers(getPool()),
hash(getPool()),
dbInfo(FB_NEW DbInfo(this)),
cryptThreadId(0),
cryptThreadHandle(0),
cryptPlugin(NULL),
checkFactory(NULL),
dbb(*tdbb->getDatabase()),
Expand All @@ -337,8 +337,8 @@ namespace Jrd {

CryptoManager::~CryptoManager()
{
if (cryptThreadId)
Thread::waitForCompletion(cryptThreadId);
if (cryptThreadHandle)
Thread::waitForCompletion(cryptThreadHandle);

delete stateLock;
delete threadLock;
Expand Down Expand Up @@ -925,10 +925,10 @@ namespace Jrd {
void CryptoManager::terminateCryptThread(thread_db*, bool wait)
{
flDown = true;
if (wait && cryptThreadId)
if (wait && cryptThreadHandle)
{
Thread::waitForCompletion(cryptThreadId);
cryptThreadId = 0;
Thread::waitForCompletion(cryptThreadHandle);
cryptThreadHandle = 0;
}
}

Expand Down Expand Up @@ -987,7 +987,7 @@ namespace Jrd {

// ready to go
guard.leave(); // release in advance to avoid races with cryptThread()
Thread::start(cryptThreadStatic, (THREAD_ENTRY_PARAM) this, THREAD_medium, &cryptThreadId);
Thread::start(cryptThreadStatic, (THREAD_ENTRY_PARAM) this, THREAD_medium, &cryptThreadHandle);
}
catch (const Firebird::Exception&)
{
Expand Down
6 changes: 5 additions & 1 deletion src/jrd/CryptoManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ class CryptoManager final : public Firebird::PermanentStorage, public BarSync::I
UCHAR getCurrentState(thread_db* tdbb) const;
const char* getKeyName() const;
const char* getPluginName() const;
Thread::Handle getCryptThreadHandle() const
{
return cryptThreadHandle;
}

private:
enum IoResult {SUCCESS_ALL, FAILED_CRYPT, FAILED_IO};
Expand Down Expand Up @@ -378,7 +382,7 @@ class CryptoManager final : public Firebird::PermanentStorage, public BarSync::I
AttachmentsRefHolder keyProviders, keyConsumers;
Firebird::string hash;
Firebird::RefPtr<DbInfo> dbInfo;
Thread::Handle cryptThreadId;
Thread::Handle cryptThreadHandle;
Firebird::IDbCryptPlugin* cryptPlugin;
Factory* checkFactory;
Database& dbb;
Expand Down
16 changes: 14 additions & 2 deletions src/jrd/jrd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7755,14 +7755,26 @@ void release_attachment(thread_db* tdbb, Jrd::Attachment* attachment, XThreadEns

Sync sync(&dbb->dbb_sync, "jrd.cpp: release_attachment");

// dummy mutex is used to avoid races with crypto thread
XThreadMutex dummy_mutex;
XThreadEnsureUnlock dummyGuard(dummy_mutex, FB_FUNCTION);

// avoid races with special threads
// take into an account lock earlier taken in DROP DATABASE
XThreadEnsureUnlock threadGuard(dbb->dbb_thread_mutex, FB_FUNCTION);
XThreadEnsureUnlock* activeThreadGuard = dropGuard;
if (!activeThreadGuard)
{
threadGuard.enter();
activeThreadGuard = &threadGuard;
if (dbb->dbb_crypto_manager
&& Thread::isCurrent(Thread::getIdFromHandle(dbb->dbb_crypto_manager->getCryptThreadHandle())))
{
activeThreadGuard = &dummyGuard;
}
else
{
activeThreadGuard = &threadGuard;
}
activeThreadGuard->enter();
}

sync.lock(SYNC_EXCLUSIVE);
Expand Down
Loading