FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
init.c
1
22#include <winpr/config.h>
23
24#include <winpr/synch.h>
25#include <winpr/interlocked.h>
26
27#include "../log.h"
28#define TAG WINPR_TAG("sync")
29
30#if (!defined(_WIN32)) || (defined(_WIN32) && (_WIN32_WINNT < 0x0600))
31
32BOOL winpr_InitOnceBeginInitialize(WINPR_ATTR_UNUSED LPINIT_ONCE lpInitOnce,
33 WINPR_ATTR_UNUSED DWORD dwFlags,
34 WINPR_ATTR_UNUSED PBOOL fPending,
35 WINPR_ATTR_UNUSED LPVOID* lpContext)
36{
37 WLog_ERR(TAG, "not implemented");
38 return FALSE;
39}
40
41BOOL winpr_InitOnceComplete(WINPR_ATTR_UNUSED LPINIT_ONCE lpInitOnce,
42 WINPR_ATTR_UNUSED DWORD dwFlags, WINPR_ATTR_UNUSED LPVOID lpContext)
43{
44 WLog_ERR(TAG, "not implemented");
45 return FALSE;
46}
47
48VOID winpr_InitOnceInitialize(WINPR_ATTR_UNUSED PINIT_ONCE InitOnce)
49{
50 WLog_ERR(TAG, "not implemented");
51}
52
53BOOL winpr_InitOnceExecuteOnce(PINIT_ONCE InitOnce, PINIT_ONCE_FN InitFn, PVOID Parameter,
54 LPVOID* Context)
55{
56 for (;;)
57 {
58 switch ((ULONG_PTR)InitOnce->Ptr & 3)
59 {
60 case 2:
61 /* already completed successfully */
62 return TRUE;
63
64 case 0:
65
66 /* first time */
67 if (InterlockedCompareExchangePointer(&InitOnce->Ptr, (PVOID)1, (PVOID)0) !=
68 (PVOID)0)
69 {
70 /* some other thread was faster */
71 break;
72 }
73
74 /* it's our job to call the init function */
75 if (InitFn(InitOnce, Parameter, Context))
76 {
77 /* success */
78 InitOnce->Ptr = (PVOID)2;
79 return TRUE;
80 }
81
82 /* the init function returned an error, reset the status */
83 InitOnce->Ptr = (PVOID)0;
84 return FALSE;
85
86 case 1:
87 /* in progress */
88 break;
89
90 default:
91 WLog_ERR(TAG, "internal error");
92 return FALSE;
93 }
94
95 Sleep(5);
96 }
97}
98
99#endif