Merge "vp9: Fix to quality regression issue for 1 pass."
[aom.git] / vpx_ports / vpx_once.h
blobda04db459075c1490b4fab923164925ca6576dd4
1 /*
2 * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
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 */
11 #ifndef VPX_PORTS_VPX_ONCE_H_
12 #define VPX_PORTS_VPX_ONCE_H_
14 #include "vpx_config.h"
16 /* Implement a function wrapper to guarantee initialization
17 * thread-safety for library singletons.
19 * NOTE: These functions use static locks, and can only be
20 * used with one common argument per compilation unit. So
22 * file1.c:
23 * vpx_once(foo);
24 * ...
25 * vpx_once(foo);
27 * file2.c:
28 * vpx_once(bar);
30 * will ensure foo() and bar() are each called only once, but in
32 * file1.c:
33 * vpx_once(foo);
34 * vpx_once(bar):
36 * bar() will never be called because the lock is used up
37 * by the call to foo().
40 #if CONFIG_MULTITHREAD && defined(_WIN32)
41 #include <windows.h>
42 #include <stdlib.h>
43 /* Declare a per-compilation-unit state variable to track the progress
44 * of calling func() only once. This must be at global scope because
45 * local initializers are not thread-safe in MSVC prior to Visual
46 * Studio 2015.
48 * As a static, once_state will be zero-initialized as program start.
50 static LONG once_state;
51 static void once(void (*func)(void))
53 /* Try to advance once_state from its initial value of 0 to 1.
54 * Only one thread can succeed in doing so.
56 if (InterlockedCompareExchange(&once_state, 1, 0) == 0) {
57 /* We're the winning thread, having set once_state to 1.
58 * Call our function. */
59 func();
60 /* Now advance once_state to 2, unblocking any other threads. */
61 InterlockedIncrement(&once_state);
62 return;
65 /* We weren't the winning thread, but we want to block on
66 * the state variable so we don't return before func()
67 * has finished executing elsewhere.
69 * Try to advance once_state from 2 to 2, which is only possible
70 * after the winning thead advances it from 1 to 2.
72 while (InterlockedCompareExchange(&once_state, 2, 2) != 2) {
73 /* State isn't yet 2. Try again.
75 * We are used for singleton initialization functions,
76 * which should complete quickly. Contention will likewise
77 * be rare, so it's worthwhile to use a simple but cpu-
78 * intensive busy-wait instead of successive backoff,
79 * waiting on a kernel object, or another heavier-weight scheme.
81 * We can at least yield our timeslice.
83 Sleep(0);
86 /* We've seen once_state advance to 2, so we know func()
87 * has been called. And we've left once_state as we found it,
88 * so other threads will have the same experience.
90 * It's safe to return now.
92 return;
96 #elif CONFIG_MULTITHREAD && defined(__OS2__)
97 #define INCL_DOS
98 #include <os2.h>
99 static void once(void (*func)(void))
101 static int done;
103 /* If the initialization is complete, return early. */
104 if(done)
105 return;
107 /* Causes all other threads in the process to block themselves
108 * and give up their time slice.
110 DosEnterCritSec();
112 if (!done)
114 func();
115 done = 1;
118 /* Restores normal thread dispatching for the current process. */
119 DosExitCritSec();
123 #elif CONFIG_MULTITHREAD && HAVE_PTHREAD_H
124 #include <pthread.h>
125 static void once(void (*func)(void))
127 static pthread_once_t lock = PTHREAD_ONCE_INIT;
128 pthread_once(&lock, func);
132 #else
133 /* No-op version that performs no synchronization. *_rtcd() is idempotent,
134 * so as long as your platform provides atomic loads/stores of pointers
135 * no synchronization is strictly necessary.
138 static void once(void (*func)(void))
140 static int done;
142 if(!done)
144 func();
145 done = 1;
148 #endif
150 #endif // VPX_PORTS_VPX_ONCE_H_