xref: /aosp_15_r20/external/libaom/aom_ports/aom_once.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AOM_PORTS_AOM_ONCE_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AOM_PORTS_AOM_ONCE_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker /* Implement a function wrapper to guarantee initialization
18*77c1e3ccSAndroid Build Coastguard Worker  * thread-safety for library singletons.
19*77c1e3ccSAndroid Build Coastguard Worker  *
20*77c1e3ccSAndroid Build Coastguard Worker  * NOTE: This function uses static locks, and can only be
21*77c1e3ccSAndroid Build Coastguard Worker  * used with one common argument per compilation unit. So
22*77c1e3ccSAndroid Build Coastguard Worker  *
23*77c1e3ccSAndroid Build Coastguard Worker  * file1.c:
24*77c1e3ccSAndroid Build Coastguard Worker  *   aom_once(foo);
25*77c1e3ccSAndroid Build Coastguard Worker  *   ...
26*77c1e3ccSAndroid Build Coastguard Worker  *   aom_once(foo);
27*77c1e3ccSAndroid Build Coastguard Worker  *
28*77c1e3ccSAndroid Build Coastguard Worker  * file2.c:
29*77c1e3ccSAndroid Build Coastguard Worker  *   aom_once(bar);
30*77c1e3ccSAndroid Build Coastguard Worker  *
31*77c1e3ccSAndroid Build Coastguard Worker  * will ensure foo() and bar() are each called only once, but in
32*77c1e3ccSAndroid Build Coastguard Worker  *
33*77c1e3ccSAndroid Build Coastguard Worker  * file1.c:
34*77c1e3ccSAndroid Build Coastguard Worker  *   aom_once(foo);
35*77c1e3ccSAndroid Build Coastguard Worker  *   aom_once(bar):
36*77c1e3ccSAndroid Build Coastguard Worker  *
37*77c1e3ccSAndroid Build Coastguard Worker  * bar() will never be called because the lock is used up
38*77c1e3ccSAndroid Build Coastguard Worker  * by the call to foo().
39*77c1e3ccSAndroid Build Coastguard Worker  */
40*77c1e3ccSAndroid Build Coastguard Worker 
41*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD && defined(_WIN32)
42*77c1e3ccSAndroid Build Coastguard Worker #undef WIN32_LEAN_AND_MEAN
43*77c1e3ccSAndroid Build Coastguard Worker #define WIN32_LEAN_AND_MEAN
44*77c1e3ccSAndroid Build Coastguard Worker #include <windows.h>
45*77c1e3ccSAndroid Build Coastguard Worker /* Declare a per-compilation-unit state variable to track the progress
46*77c1e3ccSAndroid Build Coastguard Worker  * of calling func() only once. This must be at global scope because
47*77c1e3ccSAndroid Build Coastguard Worker  * local initializers are not thread-safe in MSVC prior to Visual
48*77c1e3ccSAndroid Build Coastguard Worker  * Studio 2015.
49*77c1e3ccSAndroid Build Coastguard Worker  */
50*77c1e3ccSAndroid Build Coastguard Worker static INIT_ONCE aom_init_once = INIT_ONCE_STATIC_INIT;
51*77c1e3ccSAndroid Build Coastguard Worker 
aom_once(void (* func)(void))52*77c1e3ccSAndroid Build Coastguard Worker static void aom_once(void (*func)(void)) {
53*77c1e3ccSAndroid Build Coastguard Worker   BOOL pending;
54*77c1e3ccSAndroid Build Coastguard Worker   InitOnceBeginInitialize(&aom_init_once, 0, &pending, NULL);
55*77c1e3ccSAndroid Build Coastguard Worker   if (!pending) {
56*77c1e3ccSAndroid Build Coastguard Worker     // Initialization has already completed.
57*77c1e3ccSAndroid Build Coastguard Worker     return;
58*77c1e3ccSAndroid Build Coastguard Worker   }
59*77c1e3ccSAndroid Build Coastguard Worker   func();
60*77c1e3ccSAndroid Build Coastguard Worker   InitOnceComplete(&aom_init_once, 0, NULL);
61*77c1e3ccSAndroid Build Coastguard Worker }
62*77c1e3ccSAndroid Build Coastguard Worker 
63*77c1e3ccSAndroid Build Coastguard Worker #elif CONFIG_MULTITHREAD && HAVE_PTHREAD_H
64*77c1e3ccSAndroid Build Coastguard Worker #include <pthread.h>
aom_once(void (* func)(void))65*77c1e3ccSAndroid Build Coastguard Worker static void aom_once(void (*func)(void)) {
66*77c1e3ccSAndroid Build Coastguard Worker   static pthread_once_t lock = PTHREAD_ONCE_INIT;
67*77c1e3ccSAndroid Build Coastguard Worker   pthread_once(&lock, func);
68*77c1e3ccSAndroid Build Coastguard Worker }
69*77c1e3ccSAndroid Build Coastguard Worker 
70*77c1e3ccSAndroid Build Coastguard Worker #else
71*77c1e3ccSAndroid Build Coastguard Worker /* Default version that performs no synchronization. */
72*77c1e3ccSAndroid Build Coastguard Worker 
aom_once(void (* func)(void))73*77c1e3ccSAndroid Build Coastguard Worker static void aom_once(void (*func)(void)) {
74*77c1e3ccSAndroid Build Coastguard Worker   static volatile int done;
75*77c1e3ccSAndroid Build Coastguard Worker 
76*77c1e3ccSAndroid Build Coastguard Worker   if (!done) {
77*77c1e3ccSAndroid Build Coastguard Worker     func();
78*77c1e3ccSAndroid Build Coastguard Worker     done = 1;
79*77c1e3ccSAndroid Build Coastguard Worker   }
80*77c1e3ccSAndroid Build Coastguard Worker }
81*77c1e3ccSAndroid Build Coastguard Worker #endif
82*77c1e3ccSAndroid Build Coastguard Worker 
83*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AOM_PORTS_AOM_ONCE_H_
84