Blame winpr/libwinpr/synch/barrier.c

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