MySQL 8.3.0
Source Code Documentation
my_thread.h
Go to the documentation of this file.
1/* Copyright (c) 2000, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/**
24 @file include/my_thread.h
25 Defines to make different thread packages compatible.
26*/
27
28#ifndef MY_THREAD_INCLUDED
29#define MY_THREAD_INCLUDED
30
31#include <errno.h>
32#include <stdbool.h>
33#include <stddef.h>
34
36
37#include "my_compiler.h"
38#include "my_config.h"
39#include "my_inttypes.h"
40#include "my_macros.h"
41
42#ifndef ETIME
43#define ETIME ETIMEDOUT /* For FreeBSD */
44#endif
45
46#ifndef ETIMEDOUT
47#define ETIMEDOUT 145 /* Win32 doesn't have this */
48#endif
49
50// Pick a value which is enough for all mtr tests,
51// on all known/supported platforms.
52// Currently the largest stack requirement is with
53// clang with DEBUG and UBSAN -O0 -fno-inline
54#define DEFAULT_THREAD_STACK (1024UL * 1024UL)
55
56static inline int is_timeout(int e) {
57#if ETIMEDOUT == ETIME
58 return e == ETIMEDOUT;
59#else
60 return e == ETIMEDOUT || e == ETIME;
61#endif
62}
63
64#ifdef _WIN32
65#define MY_THREAD_CREATE_JOINABLE 0
66#define MY_THREAD_CREATE_DETACHED 1
67typedef void *(__cdecl *my_start_routine)(void *);
68#else
69#define MY_THREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
70#define MY_THREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
71typedef void *(*my_start_routine)(void *);
72#endif
73
74static inline my_thread_t my_thread_self() {
75#ifdef _WIN32
76 return GetCurrentThreadId();
77#else
78 return pthread_self();
79#endif
80}
81
82static inline int my_thread_equal(my_thread_t t1, my_thread_t t2) {
83#ifdef _WIN32
84 return t1 == t2;
85#else
86 return pthread_equal(t1, t2);
87#endif
88}
89
90static inline int my_thread_attr_init(my_thread_attr_t *attr) {
91#ifdef _WIN32
92 attr->dwStackSize = 0;
93 /* Set to joinable by default to match Linux */
94 attr->detachstate = MY_THREAD_CREATE_JOINABLE;
95 return 0;
96#else
97 return pthread_attr_init(attr);
98#endif
99}
100
101static inline int my_thread_attr_destroy(my_thread_attr_t *attr) {
102#ifdef _WIN32
103 attr->dwStackSize = 0;
104 /* Set to joinable by default to match Linux */
105 attr->detachstate = MY_THREAD_CREATE_JOINABLE;
106 return 0;
107#else
108 return pthread_attr_destroy(attr);
109#endif
110}
111
113 size_t stacksize) {
114#ifdef _WIN32
115 attr->dwStackSize = (DWORD)stacksize;
116 return 0;
117#else
118 return pthread_attr_setstacksize(attr, stacksize);
119#endif
120}
121
123 int detachstate) {
124#ifdef _WIN32
125 attr->detachstate = detachstate;
126 return 0;
127#else
128 return pthread_attr_setdetachstate(attr, detachstate);
129#endif
130}
131
133 size_t *stacksize) {
134#ifdef _WIN32
135 *stacksize = (size_t)attr->dwStackSize;
136 return 0;
137#else
138 return pthread_attr_getstacksize(attr, stacksize);
139#endif
140}
141
142static inline void my_thread_yield() {
143#ifdef _WIN32
144 SwitchToThread();
145#else
146 sched_yield();
147#endif
148}
149
150inline bool operator==(const my_thread_handle &a, const my_thread_handle &b) {
151 return (a.thread == b.thread
152#ifdef WIN32
153 && a.handle == b.handle
154#endif /* WIN32 */
155 );
156}
157inline bool operator!=(const my_thread_handle &a, const my_thread_handle &b) {
158 return !(a == b);
159}
160
161int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr,
162 my_start_routine func, void *arg);
163int my_thread_join(my_thread_handle *thread, void **value_ptr);
165
166[[noreturn]] void my_thread_exit(void *value_ptr);
167
168/** Sets the name of the thread for system and debugger, if possible.
169@param name Name to set, must be shorter than SETNAME_MAX_LENGTH, including NULL
170character. */
171void my_thread_self_setname(const char *name);
172
173extern bool my_thread_global_init();
174extern void my_thread_global_reinit();
175extern void my_thread_global_end();
176
177extern bool my_thread_init();
178extern void my_thread_end();
179
180#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:122
static int is_timeout(int e)
Definition: my_thread.h:56
void my_thread_global_reinit()
Re-initialize components initialized early with my_thread_global_init.
Definition: my_thr_init.cc:116
int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr, my_start_routine func, void *arg)
Definition: my_thread.cc:77
int my_thread_join(my_thread_handle *thread, void **value_ptr)
Definition: my_thread.cc:120
void *(* my_start_routine)(void *)
Definition: my_thread.h:71
static int my_thread_attr_getstacksize(my_thread_attr_t *attr, size_t *stacksize)
Definition: my_thread.h:132
#define MY_THREAD_CREATE_JOINABLE
Definition: my_thread.h:69
#define ETIMEDOUT
Definition: my_thread.h:47
int my_thread_cancel(my_thread_handle *thread)
Definition: my_thread.cc:140
bool operator!=(const my_thread_handle &a, const my_thread_handle &b)
Definition: my_thread.h:157
static my_thread_t my_thread_self()
Definition: my_thread.h:74
bool my_thread_init()
Allocate thread specific memory for the thread, used by mysys and dbug.
Definition: my_thr_init.cc:262
void my_thread_global_end()
Definition: my_thr_init.cc:200
static void my_thread_yield()
Definition: my_thread.h:142
#define ETIME
Definition: my_thread.h:43
static int my_thread_attr_destroy(my_thread_attr_t *attr)
Definition: my_thread.h:101
void my_thread_end()
Deallocate memory used by the thread for book-keeping.
Definition: my_thr_init.cc:297
static int my_thread_equal(my_thread_t t1, my_thread_t t2)
Definition: my_thread.h:82
bool operator==(const my_thread_handle &a, const my_thread_handle &b)
Definition: my_thread.h:150
bool my_thread_global_init()
initialize thread environment
Definition: my_thr_init.cc:154
static int my_thread_attr_setstacksize(my_thread_attr_t *attr, size_t stacksize)
Definition: my_thread.h:112
void my_thread_exit(void *value_ptr)
Definition: my_thread.cc:162
static int my_thread_attr_init(my_thread_attr_t *attr)
Definition: my_thread.h:90
void my_thread_self_setname(const char *name)
Sets the name of the thread for system and debugger, if possible.
Definition: my_thread.cc:212
Types to make different thread packages compatible.
pthread_attr_t my_thread_attr_t
Definition: my_thread_bits.h:48
pthread_t my_thread_t
Definition: my_thread_bits.h:47
case opt name
Definition: sslopt-case.h:32
Definition: my_thread_bits.h:57
my_thread_t thread
Definition: my_thread_bits.h:58