MySQL  8.0.16
Source Code Documentation
my_thread.h
Go to the documentation of this file.
1 /* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
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 #if defined(__sparc) && (defined(__SUNPRO_CC) || defined(__SUNPRO_C))
51 #define STACK_MULTIPLIER 2UL
52 #elif defined HAVE_UBSAN
53 #define STACK_MULTIPLIER 3UL
54 #elif defined HAVE_LSAN
55 #define STACK_MULTIPLIER 4UL
56 #else
57 #define STACK_MULTIPLIER 1UL
58 #endif
59 
60 #if SIZEOF_CHARP > 4
61 #define DEFAULT_THREAD_STACK (STACK_MULTIPLIER * 280UL * 1024UL)
62 #else
63 #define DEFAULT_THREAD_STACK (STACK_MULTIPLIER * 216UL * 1024UL)
64 #endif
65 
66 static inline int is_timeout(int e) {
67 #if ETIMEDOUT == ETIME
68  return e == ETIMEDOUT;
69 #else
70  return e == ETIMEDOUT || e == ETIME;
71 #endif
72 }
73 
74 #ifdef _WIN32
75 #define MY_THREAD_CREATE_JOINABLE 0
76 #define MY_THREAD_CREATE_DETACHED 1
77 typedef void *(__cdecl *my_start_routine)(void *);
78 #else
79 #define MY_THREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
80 #define MY_THREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
81 typedef void *(*my_start_routine)(void *);
82 #endif
83 
84 static inline my_thread_t my_thread_self() {
85 #ifdef _WIN32
86  return GetCurrentThreadId();
87 #else
88  return pthread_self();
89 #endif
90 }
91 
92 static inline int my_thread_equal(my_thread_t t1, my_thread_t t2) {
93 #ifdef _WIN32
94  return t1 == t2;
95 #else
96  return pthread_equal(t1, t2);
97 #endif
98 }
99 
100 static inline int my_thread_attr_init(my_thread_attr_t *attr) {
101 #ifdef _WIN32
102  attr->dwStackSize = 0;
103  /* Set to joinable by default to match Linux */
104  attr->detachstate = MY_THREAD_CREATE_JOINABLE;
105  return 0;
106 #else
107  return pthread_attr_init(attr);
108 #endif
109 }
110 
111 static inline int my_thread_attr_destroy(my_thread_attr_t *attr) {
112 #ifdef _WIN32
113  attr->dwStackSize = 0;
114  /* Set to joinable by default to match Linux */
115  attr->detachstate = MY_THREAD_CREATE_JOINABLE;
116  return 0;
117 #else
118  return pthread_attr_destroy(attr);
119 #endif
120 }
121 
123  size_t stacksize) {
124 #ifdef _WIN32
125  attr->dwStackSize = (DWORD)stacksize;
126  return 0;
127 #else
128  return pthread_attr_setstacksize(attr, stacksize);
129 #endif
130 }
131 
133  int detachstate) {
134 #ifdef _WIN32
135  attr->detachstate = detachstate;
136  return 0;
137 #else
138  return pthread_attr_setdetachstate(attr, detachstate);
139 #endif
140 }
141 
143  size_t *stacksize) {
144 #ifdef _WIN32
145  *stacksize = (size_t)attr->dwStackSize;
146  return 0;
147 #else
148  return pthread_attr_getstacksize(attr, stacksize);
149 #endif
150 }
151 
152 static inline void my_thread_yield() {
153 #ifdef _WIN32
154  SwitchToThread();
155 #else
156  sched_yield();
157 #endif
158 }
159 
160 int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr,
161  my_start_routine func, void *arg);
162 int my_thread_join(my_thread_handle *thread, void **value_ptr);
164 void my_thread_exit(void *value_ptr) MY_ATTRIBUTE((noreturn));
165 
166 extern bool my_thread_global_init();
167 extern void my_thread_global_reinit();
168 extern void my_thread_global_end();
169 
170 // Need to be extern "C" for the time being, due to memcached.
171 extern "C" bool my_thread_init();
172 extern "C" void my_thread_end();
173 
174 #endif /* MY_THREAD_INCLUDED */
int my_thread_join(my_thread_handle *thread, void **value_ptr)
Definition: my_thread.cc:98
Some common macros.
static void my_thread_yield()
Definition: my_thread.h:152
Some integer typedefs for easier portability.
bool my_thread_init()
Allocate thread specific memory for the thread, used by mysys and dbug.
Definition: my_thr_init.cc:268
static int is_timeout(int e)
Definition: my_thread.h:66
void my_thread_global_end()
Definition: my_thr_init.cc:205
Types to make different thread packages compatible.
#define ETIME
Definition: my_thread.h:43
void my_thread_global_reinit()
Re-initialize components initialized early with my_thread_global_init.
Definition: my_thr_init.cc:117
static int my_thread_attr_setdetachstate(my_thread_attr_t *attr, int detachstate)
Definition: my_thread.h:132
void *(* my_start_routine)(void *)
Definition: my_thread.h:81
static int my_thread_equal(my_thread_t t1, my_thread_t t2)
Definition: my_thread.h:92
void my_thread_end()
Deallocate memory used by the thread for book-keeping.
Definition: my_thr_init.cc:303
Definition: my_thread_bits.h:51
Header for compiler-dependent features.
void my_thread_exit(void *value_ptr)
Definition: my_thread.cc:133
int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr, my_start_routine func, void *arg)
Definition: my_thread.cc:55
pthread_t my_thread_t
Definition: my_thread_bits.h:47
static int my_thread_attr_destroy(my_thread_attr_t *attr)
Definition: my_thread.h:111
#define MY_THREAD_CREATE_JOINABLE
Definition: my_thread.h:79
pthread_attr_t my_thread_attr_t
Definition: my_thread_bits.h:48
int my_thread_cancel(my_thread_handle *thread)
Definition: my_thread.cc:116
static int my_thread_attr_init(my_thread_attr_t *attr)
Definition: my_thread.h:100
static my_thread_t my_thread_self()
Definition: my_thread.h:84
#define ETIMEDOUT
Definition: my_thread.h:47
static int my_thread_attr_setstacksize(my_thread_attr_t *attr, size_t stacksize)
Definition: my_thread.h:122
static int my_thread_attr_getstacksize(my_thread_attr_t *attr, size_t *stacksize)
Definition: my_thread.h:142
bool my_thread_global_init()
initialize thread environment
Definition: my_thr_init.cc:158