MySQL  8.0.27
Source Code Documentation
my_thread.h
Go to the documentation of this file.
1 /* Copyright (c) 2000, 2021, 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 
56 static 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
67 typedef 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
71 typedef void *(*my_start_routine)(void *);
72 #endif
73 
74 static inline my_thread_t my_thread_self() {
75 #ifdef _WIN32
76  return GetCurrentThreadId();
77 #else
78  return pthread_self();
79 #endif
80 }
81 
82 static 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 
90 static 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 
101 static 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 
142 static inline void my_thread_yield() {
143 #ifdef _WIN32
144  SwitchToThread();
145 #else
146  sched_yield();
147 #endif
148 }
149 
150 int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr,
151  my_start_routine func, void *arg);
152 int my_thread_join(my_thread_handle *thread, void **value_ptr);
154 void my_thread_exit(void *value_ptr) MY_ATTRIBUTE((noreturn));
155 
156 /** Sets the name of the thread for system and debugger, if possible.
157 @param name Name to set, must be shorter than SETNAME_MAX_LENGTH, including NULL
158 character. */
159 void my_thread_self_setname(const char *name);
160 
161 extern bool my_thread_global_init();
162 extern void my_thread_global_reinit();
163 extern void my_thread_global_end();
164 
165 // Need to be extern "C" for the time being, due to memcached.
166 extern "C" bool my_thread_init();
167 extern "C" void my_thread_end();
168 
169 #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: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:77
int my_thread_join(my_thread_handle *thread, void **value_ptr)
Definition: my_thread.cc:120
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:138
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:268
void my_thread_global_end()
Definition: my_thr_init.cc:205
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:303
static int my_thread_equal(my_thread_t t1, my_thread_t t2)
Definition: my_thread.h:82
void *(* my_start_routine)(void *)
Definition: my_thread.h:71
bool my_thread_global_init()
initialize thread environment
Definition: my_thr_init.cc:158
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:155
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.
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:51