MySQL  8.0.16
Source Code Documentation
mysql_thread.h
Go to the documentation of this file.
1 /* Copyright (c) 2008, 2019, 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 #ifndef MYSQL_THREAD_H
24 #define MYSQL_THREAD_H
25 
26 /**
27  @file include/mysql/psi/mysql_thread.h
28  Instrumentation helpers for mysys threads.
29  This header file provides the necessary declarations
30  to use the mysys thread API with the performance schema instrumentation.
31  In some compilers (SunStudio), 'static inline' functions, when declared
32  but not used, are not optimized away (because they are unused) by default,
33  so that including a static inline function from a header file does
34  create unwanted dependencies, causing unresolved symbols at link time.
35  Other compilers, like gcc, optimize these dependencies by default.
36 
37  Since the instrumented APIs declared here are wrapper on top
38  of my_thread / safemutex / etc APIs,
39  including mysql/psi/mysql_thread.h assumes that
40  the dependency on my_thread and safemutex already exists.
41 */
42 
43 #include "my_psi_config.h" // IWYU pragma: keep
44 #include "my_thread.h"
45 #include "my_thread_local.h"
46 #include "mysql/psi/psi_thread.h"
47 #ifdef MYSQL_SERVER
48 #ifndef MYSQL_DYNAMIC_PLUGIN
49 #include "pfs_thread_provider.h"
50 #endif
51 #endif
52 
53 #ifndef PSI_THREAD_CALL
54 #define PSI_THREAD_CALL(M) psi_thread_service->M
55 #endif
56 
57 /**
58  @defgroup psi_api_thread Thread Instrumentation (API)
59  @ingroup psi_api
60  @{
61 */
62 
63 /**
64  @def mysql_thread_register(P1, P2, P3)
65  Thread registration.
66 */
67 #define mysql_thread_register(P1, P2, P3) \
68  inline_mysql_thread_register(P1, P2, P3)
69 
70 /**
71  @def mysql_thread_create(K, P1, P2, P3, P4)
72  Instrumented my_thread_create.
73  This function creates both the thread instrumentation and a thread.
74  @c mysql_thread_create is a replacement for @c my_thread_create.
75  The parameter P4 (or, if it is NULL, P1) will be used as the
76  instrumented thread "indentity".
77  Providing a P1 / P4 parameter with a different value for each call
78  will on average improve performances, since this thread identity value
79  is used internally to randomize access to data and prevent contention.
80  This is optional, and the improvement is not guaranteed, only statistical.
81  @param K The PSI_thread_key for this instrumented thread
82  @param P1 my_thread_create parameter 1
83  @param P2 my_thread_create parameter 2
84  @param P3 my_thread_create parameter 3
85  @param P4 my_thread_create parameter 4
86 */
87 #ifdef HAVE_PSI_THREAD_INTERFACE
88 #define mysql_thread_create(K, P1, P2, P3, P4) \
89  inline_mysql_thread_create(K, P1, P2, P3, P4)
90 #else
91 #define mysql_thread_create(K, P1, P2, P3, P4) my_thread_create(P1, P2, P3, P4)
92 #endif
93 
94 /**
95  @def mysql_thread_set_psi_id(I)
96  Set the thread identifier for the instrumentation.
97  @param I The thread identifier
98 */
99 #ifdef HAVE_PSI_THREAD_INTERFACE
100 #define mysql_thread_set_psi_id(I) inline_mysql_thread_set_psi_id(I)
101 #else
102 #define mysql_thread_set_psi_id(I) \
103  do { \
104  } while (0)
105 #endif
106 
107 /**
108  @def mysql_thread_set_psi_THD(T)
109  Set the thread sql session for the instrumentation.
110  @param T The thread sql session
111 */
112 #ifdef HAVE_PSI_THREAD_INTERFACE
113 #define mysql_thread_set_psi_THD(T) inline_mysql_thread_set_psi_THD(T)
114 #else
115 #define mysql_thread_set_psi_THD(T) \
116  do { \
117  } while (0)
118 #endif
119 
120 static inline void inline_mysql_thread_register(
122  const char *category, PSI_thread_info *info, int count
123 #else
124  const char *category MY_ATTRIBUTE((unused)),
125  void *info MY_ATTRIBUTE((unused)), int count MY_ATTRIBUTE((unused))
126 #endif
127 ) {
128 #ifdef HAVE_PSI_THREAD_INTERFACE
129  PSI_THREAD_CALL(register_thread)(category, info, count);
130 #endif
131 }
132 
133 #ifdef HAVE_PSI_THREAD_INTERFACE
135  my_thread_handle *thread,
136  const my_thread_attr_t *attr,
137  my_start_routine start_routine,
138  void *arg) {
139  int result;
140  result = PSI_THREAD_CALL(spawn_thread)(key, thread, attr, start_routine, arg);
141  return result;
142 }
143 
145  struct PSI_thread *psi = PSI_THREAD_CALL(get_thread)();
146  PSI_THREAD_CALL(set_thread_id)(psi, id);
147 }
148 
149 #ifdef __cplusplus
150 class THD;
151 static inline void inline_mysql_thread_set_psi_THD(THD *thd) {
152  struct PSI_thread *psi = PSI_THREAD_CALL(get_thread)();
153  PSI_THREAD_CALL(set_thread_THD)(psi, thd);
154 }
155 #endif /* __cplusplus */
156 
157 #endif
158 
159  /** @} (end of group psi_api_thread) */
160 
161 #endif
Performance schema instrumentation interface.
#define PSI_THREAD_CALL(M)
Definition: psi_thread.h:31
ssize_t count
Definition: memcached.c:386
static void inline_mysql_thread_register(const char *category, PSI_thread_info *info, int count)
Definition: mysql_thread.h:120
void *(* my_start_routine)(void *)
Definition: my_thread.h:81
Definition: my_thread_bits.h:51
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
Defines to make different thread packages compatible.
static const char * key
Definition: suite_stubs.c:14
static const char * category
Definition: sha2_password.cc:168
uint32 my_thread_id
Definition: my_thread_local.h:33
Thread instrument information.
Definition: psi_thread_bits.h:78
unsigned int PSI_thread_key
Instrumented thread key.
Definition: psi_thread_bits.h:48
pthread_attr_t my_thread_attr_t
Definition: my_thread_bits.h:48
Performance schema instrumentation (declarations).
Log info(cout, "NOTE")
#define id
Definition: lexyy.cc:961
#define HAVE_PSI_THREAD_INTERFACE
Definition: my_psi_config.h:111
static void inline_mysql_thread_set_psi_THD(THD *thd)
Definition: mysql_thread.h:151
static void inline_mysql_thread_set_psi_id(my_thread_id id)
Definition: mysql_thread.h:144
struct PSI_thread PSI_thread
Definition: psi_thread_bits.h:71
static int inline_mysql_thread_create(PSI_thread_key key, my_thread_handle *thread, const my_thread_attr_t *attr, my_start_routine start_routine, void *arg)
Definition: mysql_thread.h:134
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:776