/src/mysql-server/include/my_thread.h
Line | Count | Source |
1 | | /* Copyright (c) 2000, 2025, Oracle and/or its affiliates. |
2 | | |
3 | | This program is free software; you can redistribute it and/or modify |
4 | | it under the terms of the GNU General Public License, version 2.0, |
5 | | as published by the Free Software Foundation. |
6 | | |
7 | | This program is designed to work with certain software (including |
8 | | but not limited to OpenSSL) that is licensed under separate terms, |
9 | | as designated in a particular file or component or in included license |
10 | | documentation. The authors of MySQL hereby grant you an additional |
11 | | permission to link the program and your derivative works with the |
12 | | separately licensed software that they have either included with |
13 | | the program or referenced in the documentation. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, |
16 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | | GNU General Public License, version 2.0, for more details. |
19 | | |
20 | | You should have received a copy of the GNU General Public License |
21 | | along with this program; if not, write to the Free Software |
22 | | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
23 | | |
24 | | /** |
25 | | @file include/my_thread.h |
26 | | Defines to make different thread packages compatible. |
27 | | */ |
28 | | |
29 | | #ifndef MY_THREAD_INCLUDED |
30 | | #define MY_THREAD_INCLUDED |
31 | | |
32 | | #include <errno.h> |
33 | | #include <stdbool.h> |
34 | | #include <stddef.h> |
35 | | |
36 | | #include <mysql/components/services/bits/my_thread_bits.h> // IWYU pragma: export |
37 | | |
38 | | #include "my_compiler.h" |
39 | | #include "my_config.h" |
40 | | #include "my_inttypes.h" |
41 | | #include "my_macros.h" |
42 | | |
43 | | #ifndef ETIME |
44 | | #define ETIME ETIMEDOUT /* For FreeBSD */ |
45 | | #endif |
46 | | |
47 | | #ifndef ETIMEDOUT |
48 | | #define ETIMEDOUT 145 /* Win32 doesn't have this */ |
49 | | #endif |
50 | | |
51 | | // Pick a value which is enough for all mtr tests, |
52 | | // on all known/supported platforms. |
53 | | // Currently the largest stack requirement is with |
54 | | // clang with DEBUG and UBSAN -O0 -fno-inline |
55 | | #define DEFAULT_THREAD_STACK (1024UL * 1024UL) |
56 | | |
57 | 0 | static inline bool is_timeout(int e) { |
58 | 0 | #if ETIMEDOUT == ETIME |
59 | 0 | return e == ETIMEDOUT; |
60 | 0 | #else |
61 | 0 | return e == ETIMEDOUT || e == ETIME; |
62 | 0 | #endif |
63 | 0 | } Unexecuted instantiation: sql_lexer.cc:is_timeout(int) Unexecuted instantiation: charset.cc:is_timeout(int) Unexecuted instantiation: my_error.cc:is_timeout(int) Unexecuted instantiation: my_init.cc:is_timeout(int) Unexecuted instantiation: my_lib.cc:is_timeout(int) Unexecuted instantiation: my_open.cc:is_timeout(int) Unexecuted instantiation: my_static.cc:is_timeout(int) Unexecuted instantiation: my_thr_init.cc:is_timeout(int) Unexecuted instantiation: psi_noop.cc:is_timeout(int) Unexecuted instantiation: my_file.cc:is_timeout(int) Unexecuted instantiation: my_thread.cc:is_timeout(int) |
64 | | |
65 | | #ifdef _WIN32 |
66 | | #define MY_THREAD_CREATE_JOINABLE 0 |
67 | | #define MY_THREAD_CREATE_DETACHED 1 |
68 | | typedef void *(__cdecl *my_start_routine)(void *); |
69 | | #else |
70 | | #define MY_THREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE |
71 | | #define MY_THREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED |
72 | | typedef void *(*my_start_routine)(void *); |
73 | | #endif |
74 | | |
75 | 0 | static inline my_thread_t my_thread_self() { |
76 | | #ifdef _WIN32 |
77 | | return GetCurrentThreadId(); |
78 | | #else |
79 | 0 | return pthread_self(); |
80 | 0 | #endif |
81 | 0 | } Unexecuted instantiation: sql_lexer.cc:my_thread_self() Unexecuted instantiation: charset.cc:my_thread_self() Unexecuted instantiation: my_error.cc:my_thread_self() Unexecuted instantiation: my_init.cc:my_thread_self() Unexecuted instantiation: my_lib.cc:my_thread_self() Unexecuted instantiation: my_open.cc:my_thread_self() Unexecuted instantiation: my_static.cc:my_thread_self() Unexecuted instantiation: my_thr_init.cc:my_thread_self() Unexecuted instantiation: psi_noop.cc:my_thread_self() Unexecuted instantiation: my_file.cc:my_thread_self() Unexecuted instantiation: my_thread.cc:my_thread_self() |
82 | | |
83 | 0 | static inline int my_thread_equal(my_thread_t t1, my_thread_t t2) { |
84 | 0 | #ifdef _WIN32 |
85 | 0 | return t1 == t2; |
86 | 0 | #else |
87 | 0 | return pthread_equal(t1, t2); |
88 | 0 | #endif |
89 | 0 | } Unexecuted instantiation: sql_lexer.cc:my_thread_equal(unsigned long, unsigned long) Unexecuted instantiation: charset.cc:my_thread_equal(unsigned long, unsigned long) Unexecuted instantiation: my_error.cc:my_thread_equal(unsigned long, unsigned long) Unexecuted instantiation: my_init.cc:my_thread_equal(unsigned long, unsigned long) Unexecuted instantiation: my_lib.cc:my_thread_equal(unsigned long, unsigned long) Unexecuted instantiation: my_open.cc:my_thread_equal(unsigned long, unsigned long) Unexecuted instantiation: my_static.cc:my_thread_equal(unsigned long, unsigned long) Unexecuted instantiation: my_thr_init.cc:my_thread_equal(unsigned long, unsigned long) Unexecuted instantiation: psi_noop.cc:my_thread_equal(unsigned long, unsigned long) Unexecuted instantiation: my_file.cc:my_thread_equal(unsigned long, unsigned long) Unexecuted instantiation: my_thread.cc:my_thread_equal(unsigned long, unsigned long) |
90 | | |
91 | 0 | static inline int my_thread_attr_init(my_thread_attr_t *attr) { |
92 | 0 | #ifdef _WIN32 |
93 | 0 | attr->dwStackSize = 0; |
94 | 0 | /* Set to joinable by default to match Linux */ |
95 | 0 | attr->detachstate = MY_THREAD_CREATE_JOINABLE; |
96 | 0 | return 0; |
97 | 0 | #else |
98 | 0 | return pthread_attr_init(attr); |
99 | 0 | #endif |
100 | 0 | } Unexecuted instantiation: sql_lexer.cc:my_thread_attr_init(pthread_attr_t*) Unexecuted instantiation: charset.cc:my_thread_attr_init(pthread_attr_t*) Unexecuted instantiation: my_error.cc:my_thread_attr_init(pthread_attr_t*) Unexecuted instantiation: my_init.cc:my_thread_attr_init(pthread_attr_t*) Unexecuted instantiation: my_lib.cc:my_thread_attr_init(pthread_attr_t*) Unexecuted instantiation: my_open.cc:my_thread_attr_init(pthread_attr_t*) Unexecuted instantiation: my_static.cc:my_thread_attr_init(pthread_attr_t*) Unexecuted instantiation: my_thr_init.cc:my_thread_attr_init(pthread_attr_t*) Unexecuted instantiation: psi_noop.cc:my_thread_attr_init(pthread_attr_t*) Unexecuted instantiation: my_file.cc:my_thread_attr_init(pthread_attr_t*) Unexecuted instantiation: my_thread.cc:my_thread_attr_init(pthread_attr_t*) |
101 | | |
102 | 0 | static inline int my_thread_attr_destroy(my_thread_attr_t *attr) { |
103 | 0 | #ifdef _WIN32 |
104 | 0 | attr->dwStackSize = 0; |
105 | 0 | /* Set to joinable by default to match Linux */ |
106 | 0 | attr->detachstate = MY_THREAD_CREATE_JOINABLE; |
107 | 0 | return 0; |
108 | 0 | #else |
109 | 0 | return pthread_attr_destroy(attr); |
110 | 0 | #endif |
111 | 0 | } Unexecuted instantiation: sql_lexer.cc:my_thread_attr_destroy(pthread_attr_t*) Unexecuted instantiation: charset.cc:my_thread_attr_destroy(pthread_attr_t*) Unexecuted instantiation: my_error.cc:my_thread_attr_destroy(pthread_attr_t*) Unexecuted instantiation: my_init.cc:my_thread_attr_destroy(pthread_attr_t*) Unexecuted instantiation: my_lib.cc:my_thread_attr_destroy(pthread_attr_t*) Unexecuted instantiation: my_open.cc:my_thread_attr_destroy(pthread_attr_t*) Unexecuted instantiation: my_static.cc:my_thread_attr_destroy(pthread_attr_t*) Unexecuted instantiation: my_thr_init.cc:my_thread_attr_destroy(pthread_attr_t*) Unexecuted instantiation: psi_noop.cc:my_thread_attr_destroy(pthread_attr_t*) Unexecuted instantiation: my_file.cc:my_thread_attr_destroy(pthread_attr_t*) Unexecuted instantiation: my_thread.cc:my_thread_attr_destroy(pthread_attr_t*) |
112 | | |
113 | | static inline int my_thread_attr_setstacksize(my_thread_attr_t *attr, |
114 | 0 | size_t stacksize) { |
115 | 0 | #ifdef _WIN32 |
116 | 0 | attr->dwStackSize = (DWORD)stacksize; |
117 | 0 | return 0; |
118 | 0 | #else |
119 | 0 | return pthread_attr_setstacksize(attr, stacksize); |
120 | 0 | #endif |
121 | 0 | } Unexecuted instantiation: sql_lexer.cc:my_thread_attr_setstacksize(pthread_attr_t*, unsigned long) Unexecuted instantiation: charset.cc:my_thread_attr_setstacksize(pthread_attr_t*, unsigned long) Unexecuted instantiation: my_error.cc:my_thread_attr_setstacksize(pthread_attr_t*, unsigned long) Unexecuted instantiation: my_init.cc:my_thread_attr_setstacksize(pthread_attr_t*, unsigned long) Unexecuted instantiation: my_lib.cc:my_thread_attr_setstacksize(pthread_attr_t*, unsigned long) Unexecuted instantiation: my_open.cc:my_thread_attr_setstacksize(pthread_attr_t*, unsigned long) Unexecuted instantiation: my_static.cc:my_thread_attr_setstacksize(pthread_attr_t*, unsigned long) Unexecuted instantiation: my_thr_init.cc:my_thread_attr_setstacksize(pthread_attr_t*, unsigned long) Unexecuted instantiation: psi_noop.cc:my_thread_attr_setstacksize(pthread_attr_t*, unsigned long) Unexecuted instantiation: my_file.cc:my_thread_attr_setstacksize(pthread_attr_t*, unsigned long) Unexecuted instantiation: my_thread.cc:my_thread_attr_setstacksize(pthread_attr_t*, unsigned long) |
122 | | |
123 | | static inline int my_thread_attr_setdetachstate(my_thread_attr_t *attr, |
124 | 0 | int detachstate) { |
125 | 0 | #ifdef _WIN32 |
126 | 0 | attr->detachstate = detachstate; |
127 | 0 | return 0; |
128 | 0 | #else |
129 | 0 | return pthread_attr_setdetachstate(attr, detachstate); |
130 | 0 | #endif |
131 | 0 | } Unexecuted instantiation: sql_lexer.cc:my_thread_attr_setdetachstate(pthread_attr_t*, int) Unexecuted instantiation: charset.cc:my_thread_attr_setdetachstate(pthread_attr_t*, int) Unexecuted instantiation: my_error.cc:my_thread_attr_setdetachstate(pthread_attr_t*, int) Unexecuted instantiation: my_init.cc:my_thread_attr_setdetachstate(pthread_attr_t*, int) Unexecuted instantiation: my_lib.cc:my_thread_attr_setdetachstate(pthread_attr_t*, int) Unexecuted instantiation: my_open.cc:my_thread_attr_setdetachstate(pthread_attr_t*, int) Unexecuted instantiation: my_static.cc:my_thread_attr_setdetachstate(pthread_attr_t*, int) Unexecuted instantiation: my_thr_init.cc:my_thread_attr_setdetachstate(pthread_attr_t*, int) Unexecuted instantiation: psi_noop.cc:my_thread_attr_setdetachstate(pthread_attr_t*, int) Unexecuted instantiation: my_file.cc:my_thread_attr_setdetachstate(pthread_attr_t*, int) Unexecuted instantiation: my_thread.cc:my_thread_attr_setdetachstate(pthread_attr_t*, int) |
132 | | |
133 | | static inline int my_thread_attr_getstacksize(my_thread_attr_t *attr, |
134 | 0 | size_t *stacksize) { |
135 | 0 | #ifdef _WIN32 |
136 | 0 | *stacksize = (size_t)attr->dwStackSize; |
137 | 0 | return 0; |
138 | 0 | #else |
139 | 0 | return pthread_attr_getstacksize(attr, stacksize); |
140 | 0 | #endif |
141 | 0 | } Unexecuted instantiation: sql_lexer.cc:my_thread_attr_getstacksize(pthread_attr_t*, unsigned long*) Unexecuted instantiation: charset.cc:my_thread_attr_getstacksize(pthread_attr_t*, unsigned long*) Unexecuted instantiation: my_error.cc:my_thread_attr_getstacksize(pthread_attr_t*, unsigned long*) Unexecuted instantiation: my_init.cc:my_thread_attr_getstacksize(pthread_attr_t*, unsigned long*) Unexecuted instantiation: my_lib.cc:my_thread_attr_getstacksize(pthread_attr_t*, unsigned long*) Unexecuted instantiation: my_open.cc:my_thread_attr_getstacksize(pthread_attr_t*, unsigned long*) Unexecuted instantiation: my_static.cc:my_thread_attr_getstacksize(pthread_attr_t*, unsigned long*) Unexecuted instantiation: my_thr_init.cc:my_thread_attr_getstacksize(pthread_attr_t*, unsigned long*) Unexecuted instantiation: psi_noop.cc:my_thread_attr_getstacksize(pthread_attr_t*, unsigned long*) Unexecuted instantiation: my_file.cc:my_thread_attr_getstacksize(pthread_attr_t*, unsigned long*) Unexecuted instantiation: my_thread.cc:my_thread_attr_getstacksize(pthread_attr_t*, unsigned long*) |
142 | | |
143 | 0 | static inline void my_thread_yield() { |
144 | 0 | #ifdef _WIN32 |
145 | 0 | SwitchToThread(); |
146 | 0 | #else |
147 | 0 | sched_yield(); |
148 | 0 | #endif |
149 | 0 | } Unexecuted instantiation: sql_lexer.cc:my_thread_yield() Unexecuted instantiation: charset.cc:my_thread_yield() Unexecuted instantiation: my_error.cc:my_thread_yield() Unexecuted instantiation: my_init.cc:my_thread_yield() Unexecuted instantiation: my_lib.cc:my_thread_yield() Unexecuted instantiation: my_open.cc:my_thread_yield() Unexecuted instantiation: my_static.cc:my_thread_yield() Unexecuted instantiation: my_thr_init.cc:my_thread_yield() Unexecuted instantiation: psi_noop.cc:my_thread_yield() Unexecuted instantiation: my_file.cc:my_thread_yield() Unexecuted instantiation: my_thread.cc:my_thread_yield() |
150 | | |
151 | 0 | inline bool operator==(const my_thread_handle &a, const my_thread_handle &b) { |
152 | 0 | return (a.thread == b.thread |
153 | 0 | #ifdef WIN32 |
154 | 0 | && a.handle == b.handle |
155 | 0 | #endif /* WIN32 */ |
156 | 0 | ); |
157 | 0 | } |
158 | 0 | inline bool operator!=(const my_thread_handle &a, const my_thread_handle &b) { |
159 | 0 | return !(a == b); |
160 | 0 | } |
161 | | |
162 | | int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr, |
163 | | my_start_routine func, void *arg); |
164 | | void my_thread_handle_self(my_thread_handle *thread); |
165 | | int my_thread_join(my_thread_handle *thread, void **value_ptr); |
166 | | int my_thread_cancel(my_thread_handle *thread); |
167 | | |
168 | | [[noreturn]] void my_thread_exit(void *value_ptr); |
169 | | |
170 | | /** Sets the name of the thread for system and debugger, if possible. |
171 | | @param name Name to set, must be shorter than SETNAME_MAX_LENGTH, including NULL |
172 | | character. */ |
173 | | void my_thread_self_setname(const char *name); |
174 | | |
175 | | extern bool my_thread_global_init(); |
176 | | extern void my_thread_global_reinit(); |
177 | | extern void my_thread_global_end(); |
178 | | |
179 | | extern bool my_thread_init(); |
180 | | extern void my_thread_end(); |
181 | | extern bool my_thread_is_inited(); |
182 | | |
183 | | #endif /* MY_THREAD_INCLUDED */ |