init.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * WinPR: Windows Portable Runtime
  3. * Synchronization Functions
  4. *
  5. * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
  6. * Copyright 2014 Thincast Technologies GmbH
  7. * Copyright 2014 Norbert Federa <norbert.federa@thincast.com>
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include <winpr/synch.h>
  25. #include <winpr/interlocked.h>
  26. #include "../log.h"
  27. #define TAG WINPR_TAG("sync")
  28. #if (!defined(_WIN32)) || (defined(_WIN32) && (_WIN32_WINNT < 0x0600))
  29. BOOL winpr_InitOnceBeginInitialize(LPINIT_ONCE lpInitOnce, DWORD dwFlags, PBOOL fPending,
  30. LPVOID* lpContext)
  31. {
  32. WLog_ERR(TAG, "not implemented");
  33. return FALSE;
  34. }
  35. BOOL winpr_InitOnceComplete(LPINIT_ONCE lpInitOnce, DWORD dwFlags, LPVOID lpContext)
  36. {
  37. WLog_ERR(TAG, "not implemented");
  38. return FALSE;
  39. }
  40. VOID winpr_InitOnceInitialize(PINIT_ONCE InitOnce)
  41. {
  42. WLog_ERR(TAG, "not implemented");
  43. }
  44. BOOL winpr_InitOnceExecuteOnce(PINIT_ONCE InitOnce, PINIT_ONCE_FN InitFn, PVOID Parameter,
  45. LPVOID* Context)
  46. {
  47. for (;;)
  48. {
  49. switch ((ULONG_PTR)InitOnce->Ptr & 3)
  50. {
  51. case 2:
  52. /* already completed successfully */
  53. return TRUE;
  54. case 0:
  55. /* first time */
  56. if (InterlockedCompareExchangePointer(&InitOnce->Ptr, (PVOID)1, (PVOID)0) !=
  57. (PVOID)0)
  58. {
  59. /* some other thread was faster */
  60. break;
  61. }
  62. /* it's our job to call the init function */
  63. if (InitFn(InitOnce, Parameter, Context))
  64. {
  65. /* success */
  66. InitOnce->Ptr = (PVOID)2;
  67. return TRUE;
  68. }
  69. /* the init function returned an error, reset the status */
  70. InitOnce->Ptr = (PVOID)0;
  71. return FALSE;
  72. case 1:
  73. /* in progress */
  74. break;
  75. default:
  76. WLog_ERR(TAG, "internal error");
  77. return FALSE;
  78. }
  79. Sleep(5);
  80. }
  81. }
  82. #endif