FreeRDP
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 
32 BOOL winpr_InitOnceBeginInitialize(LPINIT_ONCE lpInitOnce, DWORD dwFlags, PBOOL fPending,
33  LPVOID* lpContext)
34 {
35  WLog_ERR(TAG, "not implemented");
36  return FALSE;
37 }
38 
39 BOOL winpr_InitOnceComplete(LPINIT_ONCE lpInitOnce, DWORD dwFlags, LPVOID lpContext)
40 {
41  WLog_ERR(TAG, "not implemented");
42  return FALSE;
43 }
44 
45 VOID winpr_InitOnceInitialize(PINIT_ONCE InitOnce)
46 {
47  WLog_ERR(TAG, "not implemented");
48 }
49 
50 BOOL winpr_InitOnceExecuteOnce(PINIT_ONCE InitOnce, PINIT_ONCE_FN InitFn, PVOID Parameter,
51  LPVOID* Context)
52 {
53  for (;;)
54  {
55  switch ((ULONG_PTR)InitOnce->Ptr & 3)
56  {
57  case 2:
58  /* already completed successfully */
59  return TRUE;
60 
61  case 0:
62 
63  /* first time */
64  if (InterlockedCompareExchangePointer(&InitOnce->Ptr, (PVOID)1, (PVOID)0) !=
65  (PVOID)0)
66  {
67  /* some other thread was faster */
68  break;
69  }
70 
71  /* it's our job to call the init function */
72  if (InitFn(InitOnce, Parameter, Context))
73  {
74  /* success */
75  InitOnce->Ptr = (PVOID)2;
76  return TRUE;
77  }
78 
79  /* the init function returned an error, reset the status */
80  InitOnce->Ptr = (PVOID)0;
81  return FALSE;
82 
83  case 1:
84  /* in progress */
85  break;
86 
87  default:
88  WLog_ERR(TAG, "internal error");
89  return FALSE;
90  }
91 
92  Sleep(5);
93  }
94 }
95 
96 #endif