2012-09-18 20:57:19 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Synchronization Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/synch.h>
|
|
|
|
|
2014-08-09 01:34:30 +04:00
|
|
|
#include "synch.h"
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2012-09-18 20:57:19 +04:00
|
|
|
|
2014-08-09 05:02:15 +04:00
|
|
|
#if (!defined(_WIN32)) || (defined(_WIN32) && (_WIN32_WINNT < 0x0602))
|
2014-08-09 01:34:30 +04:00
|
|
|
|
|
|
|
#include <winpr/library.h>
|
|
|
|
#include <winpr/interlocked.h>
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
typedef BOOL (WINAPI * fnInitializeSynchronizationBarrier)(LPSYNCHRONIZATION_BARRIER lpBarrier, LONG lTotalThreads, LONG lSpinCount);
|
2014-08-12 03:31:38 +04:00
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
pfnInitializeSynchronizationBarrier = (fnInitializeSynchronizationBarrier)
|
|
|
|
GetProcAddress(g_Kernel32, "InitializeSynchronizationBarrier");
|
|
|
|
|
|
|
|
pfnEnterSynchronizationBarrier = (fnEnterSynchronizationBarrier)
|
|
|
|
GetProcAddress(g_Kernel32, "EnterSynchronizationBarrier");
|
|
|
|
|
|
|
|
pfnDeleteSynchronizationBarrier = (fnDeleteSynchronizationBarrier)
|
|
|
|
GetProcAddress(g_Kernel32, "DeleteSynchronizationBarrier");
|
|
|
|
|
|
|
|
if (pfnInitializeSynchronizationBarrier && pfnEnterSynchronizationBarrier
|
|
|
|
&& pfnDeleteSynchronizationBarrier)
|
|
|
|
{
|
|
|
|
g_NativeBarrier = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-08-09 05:02:15 +04:00
|
|
|
#endif
|
|
|
|
|
2014-08-09 01:34:30 +04:00
|
|
|
BOOL WINAPI InitializeSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier, LONG lTotalThreads, LONG lSpinCount)
|
|
|
|
{
|
|
|
|
WINPR_BARRIER* pBarrier;
|
|
|
|
|
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
|
|
|
|
|
|
|
if (!lpBarrier)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ZeroMemory(lpBarrier, sizeof(SYNCHRONIZATION_BARRIER));
|
|
|
|
|
|
|
|
pBarrier = (WINPR_BARRIER*) calloc(1, sizeof(WINPR_BARRIER));
|
|
|
|
|
|
|
|
if (!pBarrier)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (lSpinCount < 0)
|
|
|
|
lSpinCount = 2000;
|
|
|
|
|
|
|
|
pBarrier->lTotalThreads = lTotalThreads;
|
|
|
|
pBarrier->lSpinCount = lSpinCount;
|
|
|
|
pBarrier->count = 0;
|
|
|
|
|
|
|
|
pBarrier->event = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
|
|
|
|
if (!pBarrier->event)
|
|
|
|
{
|
|
|
|
free(pBarrier);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpBarrier->Reserved3[0] = (ULONG_PTR) pBarrier;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI EnterSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier, DWORD dwFlags)
|
|
|
|
{
|
|
|
|
LONG count;
|
|
|
|
BOOL status = FALSE;
|
|
|
|
WINPR_BARRIER* pBarrier;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
pBarrier = (WINPR_BARRIER*) lpBarrier->Reserved3[0];
|
|
|
|
|
|
|
|
if (!pBarrier)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
count = InterlockedIncrement(&(pBarrier->count));
|
|
|
|
|
|
|
|
if (count < pBarrier->lTotalThreads)
|
|
|
|
{
|
|
|
|
WaitForSingleObject(pBarrier->event, INFINITE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetEvent(pBarrier->event);
|
|
|
|
status = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI DeleteSynchronizationBarrier(LPSYNCHRONIZATION_BARRIER lpBarrier)
|
|
|
|
{
|
|
|
|
WINPR_BARRIER* pBarrier;
|
|
|
|
|
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
|
|
|
|
|
|
|
if (!lpBarrier)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
pBarrier = (WINPR_BARRIER*) lpBarrier->Reserved3[0];
|
|
|
|
|
|
|
|
if (!pBarrier)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
CloseHandle(pBarrier->event);
|
|
|
|
|
|
|
|
free(pBarrier);
|
|
|
|
|
|
|
|
ZeroMemory(lpBarrier, sizeof(SYNCHRONIZATION_BARRIER));
|
|
|
|
|
2012-09-18 20:57:19 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
2012-09-19 02:36:13 +04:00
|
|
|
|
|
|
|
#endif
|