vpx_once.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_VPX_PORTS_VPX_ONCE_H_
  11. #define VPX_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. /* Try to advance once_state from its initial value of 0 to 1.
  49. * Only one thread can succeed in doing so.
  50. */
  51. if (InterlockedCompareExchange(&once_state, 1, 0) == 0) {
  52. /* We're the winning thread, having set once_state to 1.
  53. * Call our function. */
  54. func();
  55. /* Now advance once_state to 2, unblocking any other threads. */
  56. InterlockedIncrement(&once_state);
  57. return;
  58. }
  59. /* We weren't the winning thread, but we want to block on
  60. * the state variable so we don't return before func()
  61. * has finished executing elsewhere.
  62. *
  63. * Try to advance once_state from 2 to 2, which is only possible
  64. * after the winning thead advances it from 1 to 2.
  65. */
  66. while (InterlockedCompareExchange(&once_state, 2, 2) != 2) {
  67. /* State isn't yet 2. Try again.
  68. *
  69. * We are used for singleton initialization functions,
  70. * which should complete quickly. Contention will likewise
  71. * be rare, so it's worthwhile to use a simple but cpu-
  72. * intensive busy-wait instead of successive backoff,
  73. * waiting on a kernel object, or another heavier-weight scheme.
  74. *
  75. * We can at least yield our timeslice.
  76. */
  77. Sleep(0);
  78. }
  79. /* We've seen once_state advance to 2, so we know func()
  80. * has been called. And we've left once_state as we found it,
  81. * so other threads will have the same experience.
  82. *
  83. * It's safe to return now.
  84. */
  85. return;
  86. }
  87. #elif CONFIG_MULTITHREAD && defined(__OS2__)
  88. #define INCL_DOS
  89. #include <os2.h>
  90. static void once(void (*func)(void)) {
  91. static int done;
  92. /* If the initialization is complete, return early. */
  93. if (done) return;
  94. /* Causes all other threads in the process to block themselves
  95. * and give up their time slice.
  96. */
  97. DosEnterCritSec();
  98. if (!done) {
  99. func();
  100. done = 1;
  101. }
  102. /* Restores normal thread dispatching for the current process. */
  103. DosExitCritSec();
  104. }
  105. #elif CONFIG_MULTITHREAD && HAVE_PTHREAD_H
  106. #include <pthread.h>
  107. static void once(void (*func)(void)) {
  108. static pthread_once_t lock = PTHREAD_ONCE_INIT;
  109. pthread_once(&lock, func);
  110. }
  111. #else
  112. /* No-op version that performs no synchronization. *_rtcd() is idempotent,
  113. * so as long as your platform provides atomic loads/stores of pointers
  114. * no synchronization is strictly necessary.
  115. */
  116. static void once(void (*func)(void)) {
  117. static int done;
  118. if (!done) {
  119. func();
  120. done = 1;
  121. }
  122. }
  123. #endif
  124. #endif // VPX_VPX_PORTS_VPX_ONCE_H_