mdbx-windows: fix and refine mdbx_suspend_threads_before_remap().

Change-Id: I4f289f5d603ca18e6714455604dd4ce98931af41
This commit is contained in:
Leo Yuriev 2018-01-08 18:07:11 +03:00
parent 30bd7d3078
commit 59ad929d36

View File

@ -209,18 +209,17 @@ int mdbx_suspend_threads_before_remap(MDBX_env *env,
const MDBX_reader *const end = begin + env->me_lck->mti_numreaders; const MDBX_reader *const end = begin + env->me_lck->mti_numreaders;
const mdbx_tid_t WriteTxnOwner = env->me_txn0 ? env->me_txn0->mt_owner : 0; const mdbx_tid_t WriteTxnOwner = env->me_txn0 ? env->me_txn0->mt_owner : 0;
for (const MDBX_reader *reader = begin; reader < end; ++reader) { for (const MDBX_reader *reader = begin; reader < end; ++reader) {
if (reader->mr_pid != env->me_pid || reader->mr_tid == CurrentTid || if (reader->mr_pid != env->me_pid || !reader->mr_tid) {
reader->mr_tid == WriteTxnOwner) skip_lck:
continue; continue;
}
if (reader->mr_tid == CurrentTid || reader->mr_tid == WriteTxnOwner)
goto skip_lck;
if (env->me_flags & MDBX_NOTLS) { if (env->me_flags & MDBX_NOTLS) {
/* Skip duplicates in no-tls mode */ /* Skip duplicates in no-tls mode */
const MDBX_reader *scan = reader; for (const MDBX_reader *scan = reader; --scan >= begin;)
while (--scan >= begin)
if (scan->mr_tid == reader->mr_tid) if (scan->mr_tid == reader->mr_tid)
break; goto skip_lck;
if (scan >= reader)
continue;
} }
rc = suspend_and_append(array, reader->mr_tid); rc = suspend_and_append(array, reader->mr_tid);
@ -238,17 +237,18 @@ int mdbx_suspend_threads_before_remap(MDBX_env *env,
} else { } else {
/* Without LCK (i.e. read-only mode). /* Without LCK (i.e. read-only mode).
* Walk thougth a snapshot of all running threads */ * Walk thougth a snapshot of all running threads */
const HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); mdbx_assert(env, env->me_txn0 == NULL);
if (hThreadSnap == INVALID_HANDLE_VALUE) const HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
return GetLastError(); return GetLastError();
THREADENTRY32 entry; THREADENTRY32 entry;
entry.dwSize = sizeof(THREADENTRY32); entry.dwSize = sizeof(THREADENTRY32);
if (!Thread32First(hThreadSnap, &entry)) { if (!Thread32First(hSnapshot, &entry)) {
rc = GetLastError(); rc = GetLastError();
bailout_toolhelp: bailout_toolhelp:
CloseHandle(hThreadSnap); CloseHandle(hSnapshot);
(void)mdbx_resume_threads_after_remap(*array); (void)mdbx_resume_threads_after_remap(*array);
return rc; return rc;
} }
@ -262,11 +262,12 @@ int mdbx_suspend_threads_before_remap(MDBX_env *env,
if (rc != MDBX_SUCCESS) if (rc != MDBX_SUCCESS)
goto bailout_toolhelp; goto bailout_toolhelp;
} while (Thread32Next(hThreadSnap, &entry)); } while (Thread32Next(hSnapshot, &entry));
rc = GetLastError(); rc = GetLastError();
if (rc != ERROR_NO_MORE_FILES) if (rc != ERROR_NO_MORE_FILES)
goto bailout_toolhelp; goto bailout_toolhelp;
CloseHandle(hSnapshot);
} }
return MDBX_SUCCESS; return MDBX_SUCCESS;