2012-09-18 20:57:19 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Synchronization Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2016-06-03 19:56:36 +03:00
|
|
|
* Copyright 2016 Norbert Federa <norbert.federa@thincast.com>
|
2012-09-18 20:57:19 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2012-09-18 20:57:19 +04:00
|
|
|
|
|
|
|
#include <winpr/synch.h>
|
2021-06-16 13:58:21 +03:00
|
|
|
#include <winpr/assert.h>
|
2012-09-18 20:57:19 +04:00
|
|
|
|
2014-08-09 01:34:30 +04:00
|
|
|
#include "synch.h"
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2012-09-18 20:57:19 +04:00
|
|
|
|
2016-05-11 20:12:44 +03:00
|
|
|
#ifdef WINPR_SYNCHRONIZATION_BARRIER
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2021-06-09 15:03:34 +03:00
|
|
|
#include <winpr/assert.h>
|
2016-06-03 19:56:36 +03:00
|
|
|
#include <winpr/sysinfo.h>
|
2014-08-09 01:34:30 +04:00
|
|
|
#include <winpr/library.h>
|
|
|
|
#include <winpr/interlocked.h>
|
2016-06-03 19:56:36 +03:00
|
|
|
#include <winpr/thread.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WinPR uses the internal RTL_BARRIER struct members exactly like Windows:
|
|
|
|
*
|
|
|
|
* DWORD Reserved1: number of threads that have not yet entered the barrier
|
|
|
|
* DWORD Reserved2: number of threads required to enter the barrier
|
|
|
|
* ULONG_PTR Reserved3[2]; two synchronization events (manual reset events)
|
|
|
|
* DWORD Reserved4; number of processors
|
|
|
|
* DWORD Reserved5; spincount
|
|
|
|
*/
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2014-08-09 05:02:15 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2014-08-09 01:34:30 +04:00
|
|
|
static HMODULE g_Kernel32 = NULL;
|
|
|
|
static BOOL g_NativeBarrier = FALSE;
|
|
|
|
static INIT_ONCE g_InitOnce = INIT_ONCE_STATIC_INIT;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
typedef BOOL(WINAPI* fnInitializeSynchronizationBarrier)(LPSYNCHRONIZATION_BARRIER lpBarrier,
|
|
|
|
LONG lTotalThreads, LONG lSpinCount);
|
|
|
|
typedef BOOL(WINAPI* fnEnterSynchronizationBarrier)(LPSYNCHRONIZATION_BARRIER lpBarrier,
|
|
|
|
DWORD dwFlags);
|
|
|
|
typedef BOOL(WINAPI* fnDeleteSynchronizationBarrier)(LPSYNCHRONIZATION_BARRIER lpBarrier);
|
2014-08-09 01:34:30 +04:00
|
|
|
|
|
|
|
static fnInitializeSynchronizationBarrier pfnInitializeSynchronizationBarrier = NULL;
|
|
|
|
static fnEnterSynchronizationBarrier pfnEnterSynchronizationBarrier = NULL;
|
|
|
|
static fnDeleteSynchronizationBarrier pfnDeleteSynchronizationBarrier = NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL CALLBACK InitOnce_Barrier(PINIT_ONCE once, PVOID param, PVOID* context)
|
2012-09-18 20:57:19 +04:00
|
|
|
{
|
2014-08-09 01:34:30 +04:00
|
|
|
g_Kernel32 = LoadLibraryA("kernel32.dll");
|
|
|
|
|
|
|
|
if (!g_Kernel32)
|
|
|
|
return TRUE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pfnInitializeSynchronizationBarrier = (fnInitializeSynchronizationBarrier)GetProcAddress(
|
|
|
|
g_Kernel32, "InitializeSynchronizationBarrier");
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pfnEnterSynchronizationBarrier =
|
|
|
|
(fnEnterSynchronizationBarrier)GetProcAddress(g_Kernel32, "EnterSynchronizationBarrier");
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pfnDeleteSynchronizationBarrier =
|
|
|
|
(fnDeleteSynchronizationBarrier)GetProcAddress(g_Kernel32, "DeleteSynchronizationBarrier");
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (pfnInitializeSynchronizationBarrier && pfnEnterSynchronizationBarrier &&
|
|
|
|
pfnDeleteSynchronizationBarrier)
|
2014-08-09 01:34:30 +04:00
|
|
|
{
|
|
|
|
g_NativeBarrier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-08-09 05:02:15 +04:00
|
|
|
#endif
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL WINAPI winpr_InitializeSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier,
|
|
|
|
LONG lTotalThreads, LONG lSpinCount)
|
2014-08-09 01:34:30 +04:00
|
|
|
{
|
2016-06-03 19:56:36 +03:00
|
|
|
SYSTEM_INFO sysinfo;
|
|
|
|
HANDLE hEvent0;
|
|
|
|
HANDLE hEvent1;
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2014-08-09 05:02:15 +04:00
|
|
|
#ifdef _WIN32
|
2014-08-09 01:34:30 +04:00
|
|
|
InitOnceExecuteOnce(&g_InitOnce, InitOnce_Barrier, NULL, NULL);
|
|
|
|
|
|
|
|
if (g_NativeBarrier)
|
|
|
|
return pfnInitializeSynchronizationBarrier(lpBarrier, lTotalThreads, lSpinCount);
|
2014-08-09 05:02:15 +04:00
|
|
|
#endif
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2016-06-03 19:56:36 +03:00
|
|
|
if (!lpBarrier || lTotalThreads < 1 || lSpinCount < -1)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
2014-08-09 01:34:30 +04:00
|
|
|
return FALSE;
|
2016-06-03 19:56:36 +03:00
|
|
|
}
|
2014-08-09 01:34:30 +04:00
|
|
|
|
|
|
|
ZeroMemory(lpBarrier, sizeof(SYNCHRONIZATION_BARRIER));
|
|
|
|
|
2016-06-03 19:56:36 +03:00
|
|
|
if (lSpinCount == -1)
|
2014-08-09 01:34:30 +04:00
|
|
|
lSpinCount = 2000;
|
|
|
|
|
2016-06-03 19:56:36 +03:00
|
|
|
if (!(hEvent0 = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
return FALSE;
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2016-06-03 19:56:36 +03:00
|
|
|
if (!(hEvent1 = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
2014-08-09 01:34:30 +04:00
|
|
|
{
|
2016-06-03 19:56:36 +03:00
|
|
|
CloseHandle(hEvent0);
|
2014-08-09 01:34:30 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-06-03 19:56:36 +03:00
|
|
|
GetNativeSystemInfo(&sysinfo);
|
|
|
|
|
2021-06-16 13:58:21 +03:00
|
|
|
WINPR_ASSERT(lTotalThreads >= 0);
|
|
|
|
lpBarrier->Reserved1 = (DWORD)lTotalThreads;
|
|
|
|
lpBarrier->Reserved2 = (DWORD)lTotalThreads;
|
2016-06-03 19:56:36 +03:00
|
|
|
lpBarrier->Reserved3[0] = (ULONG_PTR)hEvent0;
|
|
|
|
lpBarrier->Reserved3[1] = (ULONG_PTR)hEvent1;
|
|
|
|
lpBarrier->Reserved4 = sysinfo.dwNumberOfProcessors;
|
2021-06-16 13:58:21 +03:00
|
|
|
WINPR_ASSERT(lSpinCount >= 0);
|
|
|
|
lpBarrier->Reserved5 = (DWORD)lSpinCount;
|
2014-08-09 01:34:30 +04:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-06-16 12:47:33 +03:00
|
|
|
BOOL WINAPI winpr_EnterSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier, DWORD dwFlags)
|
2014-08-09 01:34:30 +04:00
|
|
|
{
|
2016-06-03 19:56:36 +03:00
|
|
|
LONG remainingThreads;
|
|
|
|
HANDLE hCurrentEvent;
|
|
|
|
HANDLE hDormantEvent;
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2014-08-09 05:02:15 +04:00
|
|
|
#ifdef _WIN32
|
2014-08-09 01:34:30 +04:00
|
|
|
if (g_NativeBarrier)
|
|
|
|
return pfnEnterSynchronizationBarrier(lpBarrier, dwFlags);
|
2014-08-09 05:02:15 +04:00
|
|
|
#endif
|
2014-08-09 01:34:30 +04:00
|
|
|
|
|
|
|
if (!lpBarrier)
|
|
|
|
return FALSE;
|
|
|
|
|
2016-06-03 19:56:36 +03:00
|
|
|
/**
|
2019-11-06 17:24:51 +03:00
|
|
|
* dwFlags according to
|
|
|
|
* https://msdn.microsoft.com/en-us/library/windows/desktop/hh706889(v=vs.85).aspx
|
2016-06-03 19:56:36 +03:00
|
|
|
*
|
|
|
|
* SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY (0x01)
|
|
|
|
* Specifies that the thread entering the barrier should block
|
|
|
|
* immediately until the last thread enters the barrier.
|
|
|
|
*
|
|
|
|
* SYNCHRONIZATION_BARRIER_FLAGS_SPIN_ONLY (0x02)
|
|
|
|
* Specifies that the thread entering the barrier should spin until the
|
|
|
|
* last thread enters the barrier, even if the spinning thread exceeds
|
|
|
|
* the barrier's maximum spin count.
|
|
|
|
*
|
|
|
|
* SYNCHRONIZATION_BARRIER_FLAGS_NO_DELETE (0x04)
|
|
|
|
* Specifies that the function can skip the work required to ensure
|
|
|
|
* that it is safe to delete the barrier, which can improve
|
|
|
|
* performance. All threads that enter this barrier must specify the
|
|
|
|
* flag; otherwise, the flag is ignored. This flag should be used only
|
|
|
|
* if the barrier will never be deleted.
|
|
|
|
*/
|
|
|
|
|
|
|
|
hCurrentEvent = (HANDLE)lpBarrier->Reserved3[0];
|
|
|
|
hDormantEvent = (HANDLE)lpBarrier->Reserved3[1];
|
|
|
|
|
|
|
|
remainingThreads = InterlockedDecrement((LONG*)&lpBarrier->Reserved1);
|
|
|
|
|
2021-06-09 15:03:34 +03:00
|
|
|
WINPR_ASSERT(remainingThreads >= 0);
|
2016-06-03 19:56:36 +03:00
|
|
|
|
|
|
|
if (remainingThreads > 0)
|
|
|
|
{
|
2016-06-04 01:55:27 +03:00
|
|
|
DWORD dwProcessors = lpBarrier->Reserved4;
|
|
|
|
BOOL spinOnly = dwFlags & SYNCHRONIZATION_BARRIER_FLAGS_SPIN_ONLY;
|
|
|
|
BOOL blockOnly = dwFlags & SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY;
|
|
|
|
BOOL block = TRUE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If SYNCHRONIZATION_BARRIER_FLAGS_SPIN_ONLY is set we will
|
|
|
|
* always spin and trust that the user knows what he/she/it
|
|
|
|
* is doing. Otherwise we'll only spin if the flag
|
|
|
|
* SYNCHRONIZATION_BARRIER_FLAGS_BLOCK_ONLY is not set and
|
|
|
|
* the number of remaining threads is less than the number
|
|
|
|
* of processors.
|
|
|
|
*/
|
|
|
|
|
2019-01-30 11:24:52 +03:00
|
|
|
if (spinOnly || (((ULONG)remainingThreads < dwProcessors) && !blockOnly))
|
2016-06-04 01:55:27 +03:00
|
|
|
{
|
|
|
|
DWORD dwSpinCount = lpBarrier->Reserved5;
|
|
|
|
DWORD sp = 0;
|
2016-06-07 14:06:50 +03:00
|
|
|
/**
|
|
|
|
* nb: we must let the compiler know that our comparand
|
|
|
|
* can change between the iterations in the loop below
|
|
|
|
*/
|
|
|
|
volatile ULONG_PTR* cmp = &lpBarrier->Reserved3[0];
|
2016-06-04 01:55:27 +03:00
|
|
|
/* we spin until the last thread _completed_ the event switch */
|
2016-06-07 14:06:50 +03:00
|
|
|
while ((block = (*cmp == (ULONG_PTR)hCurrentEvent)))
|
2016-06-04 01:55:27 +03:00
|
|
|
if (!spinOnly && ++sp > dwSpinCount)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block)
|
|
|
|
WaitForSingleObject(hCurrentEvent, INFINITE);
|
|
|
|
|
2014-08-09 01:34:30 +04:00
|
|
|
return FALSE;
|
2016-06-03 19:56:36 +03:00
|
|
|
}
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2016-06-03 19:56:36 +03:00
|
|
|
/* reset the dormant event first */
|
|
|
|
ResetEvent(hDormantEvent);
|
|
|
|
|
|
|
|
/* reset the remaining counter */
|
|
|
|
lpBarrier->Reserved1 = lpBarrier->Reserved2;
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2016-06-04 01:55:27 +03:00
|
|
|
/* switch events - this will also unblock the spinning threads */
|
|
|
|
lpBarrier->Reserved3[1] = (ULONG_PTR)hCurrentEvent;
|
|
|
|
lpBarrier->Reserved3[0] = (ULONG_PTR)hDormantEvent;
|
|
|
|
|
2016-06-03 19:56:36 +03:00
|
|
|
/* signal the blocked threads */
|
|
|
|
SetEvent(hCurrentEvent);
|
2015-05-21 09:32:51 +03:00
|
|
|
|
2016-06-03 19:56:36 +03:00
|
|
|
return TRUE;
|
2014-08-09 01:34:30 +04:00
|
|
|
}
|
|
|
|
|
2016-06-16 12:47:33 +03:00
|
|
|
BOOL WINAPI winpr_DeleteSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier)
|
2014-08-09 01:34:30 +04:00
|
|
|
{
|
2014-08-09 05:02:15 +04:00
|
|
|
#ifdef _WIN32
|
2014-08-09 01:34:30 +04:00
|
|
|
if (g_NativeBarrier)
|
|
|
|
return pfnDeleteSynchronizationBarrier(lpBarrier);
|
2014-08-09 05:02:15 +04:00
|
|
|
#endif
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2016-06-04 14:40:01 +03:00
|
|
|
/**
|
|
|
|
* According to https://msdn.microsoft.com/en-us/library/windows/desktop/hh706887(v=vs.85).aspx
|
|
|
|
* Return value:
|
|
|
|
* The DeleteSynchronizationBarrier function always returns TRUE.
|
|
|
|
*/
|
|
|
|
|
2014-08-09 01:34:30 +04:00
|
|
|
if (!lpBarrier)
|
2016-06-04 14:40:01 +03:00
|
|
|
return TRUE;
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2016-06-03 19:56:36 +03:00
|
|
|
while (lpBarrier->Reserved1 != lpBarrier->Reserved2)
|
|
|
|
SwitchToThread();
|
2015-05-21 09:32:51 +03:00
|
|
|
|
2016-06-03 19:56:36 +03:00
|
|
|
if (lpBarrier->Reserved3[0])
|
|
|
|
CloseHandle((HANDLE)lpBarrier->Reserved3[0]);
|
2014-08-09 01:34:30 +04:00
|
|
|
|
2016-06-03 19:56:36 +03:00
|
|
|
if (lpBarrier->Reserved3[1])
|
|
|
|
CloseHandle((HANDLE)lpBarrier->Reserved3[1]);
|
2014-08-09 01:34:30 +04:00
|
|
|
|
|
|
|
ZeroMemory(lpBarrier, sizeof(SYNCHRONIZATION_BARRIER));
|
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
2012-09-19 02:36:13 +04:00
|
|
|
|
|
|
|
#endif
|