vpx_once.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VPX_PORTS_VPX_ONCE_H_
  11. #define VPX_PORTS_VPX_ONCE_H_
  12. #include "vpx_config.h"
  13. /* Implement a function wrapper to guarantee initialization
  14. * thread-safety for library singletons.
  15. *
  16. * NOTE: These functions use static locks, and can only be
  17. * used with one common argument per compilation unit. So
  18. *
  19. * file1.c:
  20. * vpx_once(foo);
  21. * ...
  22. * vpx_once(foo);
  23. *
  24. * file2.c:
  25. * vpx_once(bar);
  26. *
  27. * will ensure foo() and bar() are each called only once, but in
  28. *
  29. * file1.c:
  30. * vpx_once(foo);
  31. * vpx_once(bar):
  32. *
  33. * bar() will never be called because the lock is used up
  34. * by the call to foo().
  35. */
  36. #if CONFIG_MULTITHREAD && defined(_WIN32)
  37. #include <windows.h>
  38. #include <stdlib.h>
  39. /* Declare a per-compilation-unit state variable to track the progress
  40. * of calling func() only once. This must be at global scope because
  41. * local initializers are not thread-safe in MSVC prior to Visual
  42. * Studio 2015.
  43. *
  44. * As a static, once_state will be zero-initialized as program start.
  45. */
  46. static LONG once_state;
  47. static void once(void (*func)(void))
  48. {
  49. /* Try to advance once_state from its initial value of 0 to 1.
  50. * Only one thread can succeed in doing so.
  51. */
  52. if (InterlockedCompareExchange(&once_state, 1, 0) == 0) {
  53. /* We're the winning thread, having set once_state to 1.
  54. * Call our function. */
  55. func();
  56. /* Now advance once_state to 2, unblocking any other threads. */
  57. InterlockedIncrement(&once_state);
  58. return;
  59. }
  60. /* We weren't the winning thread, but we want to block on
  61. * the state variable so we don't return before func()
  62. * has finished executing elsewhere.
  63. *
  64. * Try to advance once_state from 2 to 2, which is only possible
  65. * after the winning thead advances it from 1 to 2.
  66. */
  67. while (InterlockedCompareExchange(&once_state, 2, 2) != 2) {
  68. /* State isn't yet 2. Try again.
  69. *
  70. * We are used for singleton initialization functions,
  71. * which should complete quickly. Contention will likewise
  72. * be rare, so it's worthwhile to use a simple but cpu-
  73. * intensive busy-wait instead of successive backoff,
  74. * waiting on a kernel object, or another heavier-weight scheme.
  75. *
  76. * We can at least yield our timeslice.
  77. */
  78. Sleep(0);
  79. }
  80. /* We've seen once_state advance to 2, so we know func()
  81. * has been called. And we've left once_state as we found it,
  82. * so other threads will have the same experience.
  83. *
  84. * It's safe to return now.
  85. */
  86. return;
  87. }
  88. #elif CONFIG_MULTITHREAD && defined(__OS2__)
  89. #define INCL_DOS
  90. #include <os2.h>
  91. static void once(void (*func)(void))
  92. {
  93. static int done;
  94. /* If the initialization is complete, return early. */
  95. if(done)
  96. return;
  97. /* Causes all other threads in the process to block themselves
  98. * and give up their time slice.
  99. */
  100. DosEnterCritSec();
  101. if (!done)
  102. {
  103. func();
  104. done = 1;
  105. }
  106. /* Restores normal thread dispatching for the current process. */
  107. DosExitCritSec();
  108. }
  109. #elif CONFIG_MULTITHREAD && HAVE_PTHREAD_H
  110. #include <pthread.h>
  111. static void once(void (*func)(void))
  112. {
  113. static pthread_once_t lock = PTHREAD_ONCE_INIT;
  114. pthread_once(&lock, func);
  115. }
  116. #else
  117. /* No-op version that performs no synchronization. *_rtcd() is idempotent,
  118. * so as long as your platform provides atomic loads/stores of pointers
  119. * no synchronization is strictly necessary.
  120. */
  121. static void once(void (*func)(void))
  122. {
  123. static int done;
  124. if(!done)
  125. {
  126. func();
  127. done = 1;
  128. }
  129. }
  130. #endif
  131. #endif // VPX_PORTS_VPX_ONCE_H_