MySQL 9.0.0
Source Code Documentation
my_thread.h
Go to the documentation of this file.
1/* Copyright (c) 2000, 2024, 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
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
57static inline int is_timeout(int e) {
58#if ETIMEDOUT == ETIME
59 return e == ETIMEDOUT;
60#else
61 return e == ETIMEDOUT || e == ETIME;
62#endif
63}
64
65#ifdef _WIN32
66#define MY_THREAD_CREATE_JOINABLE 0
67#define MY_THREAD_CREATE_DETACHED 1
68typedef 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
72typedef void *(*my_start_routine)(void *);
73#endif
74
75static inline my_thread_t my_thread_self() {
76#ifdef _WIN32
77 return GetCurrentThreadId();
78#else
79 return pthread_self();
80#endif
81}
82
83static inline int my_thread_equal(my_thread_t t1, my_thread_t t2) {
84#ifdef _WIN32
85 return t1 == t2;
86#else
87 return pthread_equal(t1, t2);
88#endif
89}
90
91static inline int my_thread_attr_init(my_thread_attr_t *attr) {
92#ifdef _WIN32
93 attr->dwStackSize = 0;
94 /* Set to joinable by default to match Linux */
95 attr->detachstate = MY_THREAD_CREATE_JOINABLE;
96 return 0;
97#else
98 return pthread_attr_init(attr);
99#endif
100}
101
102static inline int my_thread_attr_destroy(my_thread_attr_t *attr) {
103#ifdef _WIN32
104 attr->dwStackSize = 0;
105 /* Set to joinable by default to match Linux */
106 attr->detachstate = MY_THREAD_CREATE_JOINABLE;
107 return 0;
108#else
109 return pthread_attr_destroy(attr);
110#endif
111}
112
114 size_t stacksize) {
115#ifdef _WIN32
116 attr->dwStackSize = (DWORD)stacksize;
117 return 0;
118#else
119 return pthread_attr_setstacksize(attr, stacksize);
120#endif
121}
122
124 int detachstate) {
125#ifdef _WIN32
126 attr->detachstate = detachstate;
127 return 0;
128#else
129 return pthread_attr_setdetachstate(attr, detachstate);
130#endif
131}
132
134 size_t *stacksize) {
135#ifdef _WIN32
136 *stacksize = (size_t)attr->dwStackSize;
137 return 0;
138#else
139 return pthread_attr_getstacksize(attr, stacksize);
140#endif
141}
142
143static inline void my_thread_yield() {
144#ifdef _WIN32
145 SwitchToThread();
146#else
147 sched_yield();
148#endif
149}
150
151inline bool operator==(const my_thread_handle &a, const my_thread_handle &b) {
152 return (a.thread == b.thread
153#ifdef WIN32
154 && a.handle == b.handle
155#endif /* WIN32 */
156 );
157}
158inline bool operator!=(const my_thread_handle &a, const my_thread_handle &b) {
159 return !(a == b);
160}
161
162int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr,
163 my_start_routine func, void *arg);
164int my_thread_join(my_thread_handle *thread, void **value_ptr);
166
167[[noreturn]] void my_thread_exit(void *value_ptr);
168
169/** Sets the name of the thread for system and debugger, if possible.
170@param name Name to set, must be shorter than SETNAME_MAX_LENGTH, including NULL
171character. */
172void my_thread_self_setname(const char *name);
173
174extern bool my_thread_global_init();
175extern void my_thread_global_reinit();
176extern void my_thread_global_end();
177
178extern bool my_thread_init();
179extern void my_thread_end();
180
181#endif /* MY_THREAD_INCLUDED */
Header for compiler-dependent features.
Some integer typedefs for easier portability.
Some common macros.
static int my_thread_attr_setdetachstate(my_thread_attr_t *attr, int detachstate)
Definition: my_thread.h:123
static int is_timeout(int e)
Definition: my_thread.h:57
void my_thread_global_reinit()
Re-initialize components initialized early with my_thread_global_init.
Definition: my_thr_init.cc:117
int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr, my_start_routine func, void *arg)
Definition: my_thread.cc:78
int my_thread_join(my_thread_handle *thread, void **value_ptr)
Definition: my_thread.cc:121
void *(* my_start_routine)(void *)
Definition: my_thread.h:72
static int my_thread_attr_getstacksize(my_thread_attr_t *attr, size_t *stacksize)
Definition: my_thread.h:133
#define MY_THREAD_CREATE_JOINABLE
Definition: my_thread.h:70
#define ETIMEDOUT
Definition: my_thread.h:48
int my_thread_cancel(my_thread_handle *thread)
Definition: my_thread.cc:141
bool operator!=(const my_thread_handle &a, const my_thread_handle &b)
Definition: my_thread.h:158
static my_thread_t my_thread_self()
Definition: my_thread.h:75
bool my_thread_init()
Allocate thread specific memory for the thread, used by mysys and dbug.
Definition: my_thr_init.cc:263
void my_thread_global_end()
Definition: my_thr_init.cc:201
static void my_thread_yield()
Definition: my_thread.h:143
#define ETIME
Definition: my_thread.h:44
static int my_thread_attr_destroy(my_thread_attr_t *attr)
Definition: my_thread.h:102
void my_thread_end()
Deallocate memory used by the thread for book-keeping.
Definition: my_thr_init.cc:298
static int my_thread_equal(my_thread_t t1, my_thread_t t2)
Definition: my_thread.h:83
bool operator==(const my_thread_handle &a, const my_thread_handle &b)
Definition: my_thread.h:151
bool my_thread_global_init()
initialize thread environment
Definition: my_thr_init.cc:155
static int my_thread_attr_setstacksize(my_thread_attr_t *attr, size_t stacksize)
Definition: my_thread.h:113
void my_thread_exit(void *value_ptr)
Definition: my_thread.cc:163
static int my_thread_attr_init(my_thread_attr_t *attr)
Definition: my_thread.h:91
void my_thread_self_setname(const char *name)
Sets the name of the thread for system and debugger, if possible.
Definition: my_thread.cc:213
Types to make different thread packages compatible.
pthread_attr_t my_thread_attr_t
Definition: my_thread_bits.h:49
pthread_t my_thread_t
Definition: my_thread_bits.h:48
case opt name
Definition: sslopt-case.h:29
Definition: my_thread_bits.h:58
my_thread_t thread
Definition: my_thread_bits.h:59