FreeRDP/winpr/libwinpr/synch/init.c

97 lines
2.3 KiB
C
Raw Normal View History

2012-09-18 23:51:33 +04:00
/**
* WinPR: Windows Portable Runtime
* Synchronization Functions
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
OpenSSL thread safety freerdp/winpr had the following issues: * The non reentrant SSL_library_init() was called concurrently (crash) * Missing code/api to set the eventually required OpenSSL static and dynamic locking callbacks * Missing code/api to free the application-global or thread-local OpenSSL data and tables This commit creates two new winpr functions: BOOL winpr_InitializeSSL(DWORD flags): Use the flag WINPR_SSL_INIT_ALREADY_INITIALIZED if you want to tell winpr that your application has already initialized OpenSSL. If required use the flag WINPR_SSL_INIT_ENABLE_LOCKING to tell winpr that it should set the OpenSSL static and dynamic locking callbacks. Otherwise just call it with the flag WINPR_SSL_INIT_DEFAULT. The recommended way is that your application calls this function once before any threads are created. However, in order to support lazy OpenSSL library initialization winpr_InitializeSSL() can also safely be called multiple times and concurrently because it uses the new InitOnceExecuteOnce() function to guarantee that the initialization is only performed successfully once during the life time of the calling process. BOOL winpr_CleanupSSL(DWORD flags): If you create a thread that uses SSL you should call this function before the thread returns using the flag WINPR_SSL_CLEANUP_THREAD in order to clean up the thread-local OpenSSL data and tables. Call the function with the flag WINPR_SSL_CLEANUP_GLOBAL before terminating your application. Note: This commit only replaced the current occurences of the SSL_load_error_strings(); SSL_library_init(); pairs in the freerdp source with winpr_InitializeSSL(). None of the server or client applications has been changed according to the recommended usage described above (TBDL).
2014-07-28 23:55:57 +04:00
* Copyright 2014 Thincast Technologies GmbH
* Copyright 2014 Norbert Federa <norbert.federa@thincast.com>
2012-09-18 23:51:33 +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 23:51:33 +04:00
#include <winpr/synch.h>
#include <winpr/interlocked.h>
2012-09-18 23:51:33 +04:00
2014-08-18 19:22:22 +04:00
#include "../log.h"
#define TAG WINPR_TAG("sync")
2014-08-18 19:22:22 +04:00
#if (!defined(_WIN32)) || (defined(_WIN32) && (_WIN32_WINNT < 0x0600))
2019-11-06 17:24:51 +03:00
BOOL winpr_InitOnceBeginInitialize(LPINIT_ONCE lpInitOnce, DWORD dwFlags, PBOOL fPending,
LPVOID* lpContext)
2012-09-18 23:51:33 +04:00
{
2014-08-18 19:22:22 +04:00
WLog_ERR(TAG, "not implemented");
return FALSE;
2012-09-18 23:51:33 +04:00
}
BOOL winpr_InitOnceComplete(LPINIT_ONCE lpInitOnce, DWORD dwFlags, LPVOID lpContext)
2012-09-18 23:51:33 +04:00
{
2014-08-18 19:22:22 +04:00
WLog_ERR(TAG, "not implemented");
return FALSE;
2012-09-18 23:51:33 +04:00
}
VOID winpr_InitOnceInitialize(PINIT_ONCE InitOnce)
2012-09-18 23:51:33 +04:00
{
2014-08-18 19:22:22 +04:00
WLog_ERR(TAG, "not implemented");
2012-09-18 23:51:33 +04:00
}
2019-11-06 17:24:51 +03:00
BOOL winpr_InitOnceExecuteOnce(PINIT_ONCE InitOnce, PINIT_ONCE_FN InitFn, PVOID Parameter,
LPVOID* Context)
2012-09-18 23:51:33 +04:00
{
for (;;)
{
2014-08-18 19:22:22 +04:00
switch ((ULONG_PTR)InitOnce->Ptr & 3)
{
case 2:
/* already completed successfully */
return TRUE;
case 0:
2014-08-18 19:22:22 +04:00
/* first time */
2019-11-06 17:24:51 +03:00
if (InterlockedCompareExchangePointer(&InitOnce->Ptr, (PVOID)1, (PVOID)0) !=
(PVOID)0)
{
/* some other thread was faster */
break;
}
/* it's our job to call the init function */
if (InitFn(InitOnce, Parameter, Context))
{
/* success */
InitOnce->Ptr = (PVOID)2;
return TRUE;
}
/* the init function returned an error, reset the status */
InitOnce->Ptr = (PVOID)0;
return FALSE;
case 1:
/* in progress */
break;
default:
2014-08-18 19:22:22 +04:00
WLog_ERR(TAG, "internal error");
return FALSE;
}
2012-09-18 23:51:33 +04:00
Sleep(5);
}
2012-09-18 23:51:33 +04:00
}
#endif