Debian Codegraph @

storage/innobase/include/os0once.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2014, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/os0once.h
29 A class that aids executing a given function exactly once in a multi-threaded
30 environment.
31
32 Created Feb 20, 2014 Vasil Dimov
33 *******************************************************/
34
35 #ifndef os0once_h
36 #define os0once_h
37
38 #include "univ.i"
39
40 #include "os0atomic.h"
41 #include "ut0ut.h"
42
43 /** Execute a given function exactly once in a multi-threaded environment
44 or wait for the function to be executed by another thread.
45
46 Example usage:
47 First the user must create a control variable of type os_once::state_t and
48 assign it os_once::NEVER_DONE.
49 Then the user must pass this variable, together with a function to be
50 executed to os_once::do_or_wait_for_done().
51
52 Multiple threads can call os_once::do_or_wait_for_done() simultaneously with
53 the same (os_once::state_t) control variable. The provided function will be
54 called exactly once and when os_once::do_or_wait_for_done() returns then this
55 function has completed execution, by this or another thread. In other words
56 os_once::do_or_wait_for_done() will either execute the provided function or
57 will wait for its execution to complete if it is already called by another
58 thread or will do nothing if the function has already completed its execution
59 earlier.
60
61 This mimics pthread_once(3), but unfortunately pthread_once(3) does not
62 support passing arguments to the init_routine() function. We should use
63 std::call_once() when we start compiling with C++11 enabled. */
64 class os_once {
65 public:
66 /** Control variables' state type */
67 typedef uint32_t state_t;
68
69 /** Not yet executed. */
70 static const state_t NEVER_DONE = 0;
71
72 /** Currently being executed by this or another thread. */
73 static const state_t IN_PROGRESS = 1;
74
75 /** Finished execution. */
76 static const state_t DONE = 2;
77
78 /** Call a given function or wait its execution to complete if it is
79 already called by another thread.
80 @param[in,out] state control variable
81 @param[in] do_func function to call
82 @param[in,out] do_func_arg an argument to pass to do_func(). */
83 static void do_or_wait_for_done(std::atomic<state_t> *state,
84 void (*do_func)(void *), void *do_func_arg) {
85 /* Avoid calling compare_exchange_strong() in the most common case. */
86 if (*state == DONE) {
87 return;
88 }
89
90 state_t never_done = NEVER_DONE;
91 if (state->compare_exchange_strong(never_done, IN_PROGRESS)) {
92 /* We are the first. Call the function. */
93
94 do_func(do_func_arg);
95
96 state_t in_progress = IN_PROGRESS;
97 const bool swapped = state->compare_exchange_strong(in_progress, DONE);
98
99 ut_a(swapped);
100 } else {
101 /* The state is not NEVER_DONE, so either it is
102 IN_PROGRESS (somebody is calling the function right
103 now or DONE (it has already been called and completed).
104 Wait for it to become DONE. */
105 for (;;) {
106 const state_t s = *state;
107
108 switch (s) {
109 case DONE:
110 return;
111 case IN_PROGRESS:
112 break;
113 case NEVER_DONE:
114 [[fallthrough]];
115 default:
116 ut_error;
117 }
118
119 #ifndef UNIV_HOTBACKUP
120 UT_RELAX_CPU();
121 #endif /* !UNIV_HOTBACKUP */
122 }
123 }
124 }
125 };
126
127 #endif /* os0once_h */