MySQL  8.0.27
Source Code Documentation
mysql_cond.h
Go to the documentation of this file.
1 /* Copyright (c) 2008, 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 #ifndef MYSQL_COND_H
24 #define MYSQL_COND_H
25 
26 /**
27  @file include/mysql/psi/mysql_cond.h
28  Instrumentation helpers for conditions.
29 */
30 
31 /* HAVE_PSI_*_INTERFACE */
32 #include "my_psi_config.h" // IWYU pragma: keep
33 
35 #include "mysql/psi/mysql_mutex.h"
36 #include "mysql/psi/psi_cond.h"
37 #include "thr_cond.h"
38 
39 #if defined(MYSQL_SERVER) || defined(PFS_DIRECT_CALL)
40 /* PSI_COND_CALL() as direct call. */
41 #include "pfs_cond_provider.h" // IWYU pragma: keep
42 #endif
43 
44 #ifndef PSI_COND_CALL
45 #define PSI_COND_CALL(M) psi_cond_service->M
46 #endif
47 
48 /**
49  @defgroup psi_api_cond Cond Instrumentation (API)
50  @ingroup psi_api
51  @{
52 */
53 
54 #ifndef DISABLE_MYSQL_THREAD_H
55 
56 /**
57  @def mysql_cond_register(P1, P2, P3)
58  Cond registration.
59 */
60 #define mysql_cond_register(P1, P2, P3) inline_mysql_cond_register(P1, P2, P3)
61 
62 /**
63  @def mysql_cond_init(K, C)
64  Instrumented cond_init.
65  @c mysql_cond_init is a replacement for @c pthread_cond_init.
66  Note that pthread_condattr_t is not supported in MySQL.
67  @param C The cond to initialize
68  @param K The PSI_cond_key for this instrumented cond
69 
70 */
71 
72 #define mysql_cond_init(K, C) mysql_cond_init_with_src(K, C, __FILE__, __LINE__)
73 
74 #define mysql_cond_init_with_src(K, C, F, L) inline_mysql_cond_init(K, C, F, L)
75 
76 /**
77  @def mysql_cond_destroy(C)
78  Instrumented cond_destroy.
79  @c mysql_cond_destroy is a drop-in replacement for @c pthread_cond_destroy.
80 */
81 #define mysql_cond_destroy(C) mysql_cond_destroy_with_src(C, __FILE__, __LINE__)
82 
83 #define mysql_cond_destroy_with_src(C, F, L) inline_mysql_cond_destroy(C, F, L)
84 
85 /**
86  @def mysql_cond_wait(C)
87  Instrumented cond_wait.
88  @c mysql_cond_wait is a drop-in replacement for @c native_cond_wait.
89 */
90 #define mysql_cond_wait(C, M) mysql_cond_wait_with_src(C, M, __FILE__, __LINE__)
91 
92 #define mysql_cond_wait_with_src(C, M, F, L) inline_mysql_cond_wait(C, M, F, L)
93 
94 /**
95  @def mysql_cond_timedwait(C, M, W)
96  Instrumented cond_timedwait.
97  @c mysql_cond_timedwait is a drop-in replacement
98  for @c native_cond_timedwait.
99 */
100 
101 #define mysql_cond_timedwait(C, M, W) \
102  mysql_cond_timedwait_with_src(C, M, W, __FILE__, __LINE__)
103 
104 #define mysql_cond_timedwait_with_src(C, M, W, F, L) \
105  inline_mysql_cond_timedwait(C, M, W, F, L)
106 
107 /**
108  @def mysql_cond_signal(C)
109  Instrumented cond_signal.
110  @c mysql_cond_signal is a drop-in replacement for @c pthread_cond_signal.
111 */
112 
113 #define mysql_cond_signal(C) mysql_cond_signal_with_src(C, __FILE__, __LINE__)
114 
115 #define mysql_cond_signal_with_src(C, F, L) inline_mysql_cond_signal(C, F, L)
116 
117 /**
118  @def mysql_cond_broadcast(C)
119  Instrumented cond_broadcast.
120  @c mysql_cond_broadcast is a drop-in replacement
121  for @c pthread_cond_broadcast.
122 */
123 #define mysql_cond_broadcast(C) \
124  mysql_cond_broadcast_with_src(C, __FILE__, __LINE__)
125 
126 #define mysql_cond_broadcast_with_src(C, F, L) \
127  inline_mysql_cond_broadcast(C, F, L)
128 
129 static inline void inline_mysql_cond_register(const char *category
130  [[maybe_unused]],
132  [[maybe_unused]],
133  int count [[maybe_unused]]) {
134 #ifdef HAVE_PSI_COND_INTERFACE
135  PSI_COND_CALL(register_cond)(category, info, count);
136 #endif
137 }
138 
139 static inline int inline_mysql_cond_init(PSI_cond_key key [[maybe_unused]],
140  mysql_cond_t *that,
141  const char *src_file [[maybe_unused]],
142  int src_line [[maybe_unused]]) {
143 #ifdef HAVE_PSI_COND_INTERFACE
144  that->m_psi = PSI_COND_CALL(init_cond)(key, &that->m_cond);
145 #else
146  that->m_psi = nullptr;
147 #endif
148  return native_cond_init(&that->m_cond);
149 }
150 
151 static inline int inline_mysql_cond_destroy(mysql_cond_t *that,
152  const char *src_file
153  [[maybe_unused]],
154  int src_line [[maybe_unused]]) {
155 #ifdef HAVE_PSI_COND_INTERFACE
156  if (that->m_psi != nullptr) {
158  that->m_psi = nullptr;
159  }
160 #endif
161  return native_cond_destroy(&that->m_cond);
162 }
163 
164 static inline int inline_mysql_cond_wait(mysql_cond_t *that,
165  mysql_mutex_t *mutex,
166  const char *src_file [[maybe_unused]],
167  int src_line [[maybe_unused]]) {
168  int result;
169 
170 #ifdef HAVE_PSI_COND_INTERFACE
171  if (that->m_psi != nullptr) {
172  if (that->m_psi->m_enabled) {
173  /* Instrumentation start */
174  PSI_cond_locker *locker;
175  PSI_cond_locker_state state;
176  locker = PSI_COND_CALL(start_cond_wait)(
177  &state, that->m_psi, mutex->m_psi, PSI_COND_WAIT, src_file, src_line);
178 
179  /* Instrumented code */
180  result = my_cond_wait(&that->m_cond, &mutex->m_mutex
181 #ifdef SAFE_MUTEX
182  ,
183  src_file, src_line
184 #endif
185  );
186 
187  /* Instrumentation end */
188  if (locker != nullptr) {
189  PSI_COND_CALL(end_cond_wait)(locker, result);
190  }
191 
192  return result;
193  }
194  }
195 #endif
196 
197  /* Non instrumented code */
198  result = my_cond_wait(&that->m_cond, &mutex->m_mutex
199 #ifdef SAFE_MUTEX
200  ,
201  src_file, src_line
202 #endif
203  );
204 
205  return result;
206 }
207 
208 static inline int inline_mysql_cond_timedwait(
209  mysql_cond_t *that, mysql_mutex_t *mutex, const struct timespec *abstime,
210  const char *src_file [[maybe_unused]], int src_line [[maybe_unused]]) {
211  int result;
212 
213 #ifdef HAVE_PSI_COND_INTERFACE
214  if (that->m_psi != nullptr) {
215  if (that->m_psi->m_enabled) {
216  /* Instrumentation start */
217  PSI_cond_locker *locker;
218  PSI_cond_locker_state state;
219  locker = PSI_COND_CALL(start_cond_wait)(&state, that->m_psi, mutex->m_psi,
220  PSI_COND_TIMEDWAIT, src_file,
221  src_line);
222 
223  /* Instrumented code */
224  result = my_cond_timedwait(&that->m_cond, &mutex->m_mutex, abstime
225 #ifdef SAFE_MUTEX
226  ,
227  src_file, src_line
228 #endif
229  );
230 
231  /* Instrumentation end */
232  if (locker != nullptr) {
233  PSI_COND_CALL(end_cond_wait)(locker, result);
234  }
235 
236  return result;
237  }
238  }
239 #endif
240 
241  /* Non instrumented code */
242  result = my_cond_timedwait(&that->m_cond, &mutex->m_mutex, abstime
243 #ifdef SAFE_MUTEX
244  ,
245  src_file, src_line
246 #endif
247  );
248 
249  return result;
250 }
251 
252 static inline int inline_mysql_cond_signal(mysql_cond_t *that,
253  const char *src_file
254  [[maybe_unused]],
255  int src_line [[maybe_unused]]) {
256  int result;
257 #ifdef HAVE_PSI_COND_INTERFACE
258  if (that->m_psi != nullptr) {
259  if (that->m_psi->m_enabled) {
260  PSI_COND_CALL(signal_cond)(that->m_psi);
261  }
262  }
263 #endif
264  result = native_cond_signal(&that->m_cond);
265  return result;
266 }
267 
269  const char *src_file
270  [[maybe_unused]],
271  int src_line [[maybe_unused]]) {
272  int result;
273 #ifdef HAVE_PSI_COND_INTERFACE
274  if (that->m_psi != nullptr) {
275  if (that->m_psi->m_enabled) {
276  PSI_COND_CALL(broadcast_cond)(that->m_psi);
277  }
278  }
279 #endif
281  return result;
282 }
283 
284 #endif /* DISABLE_MYSQL_THREAD_H */
285 
286 /** @} (end of group psi_api_cond) */
287 
288 #endif
#define PSI_COND_CALL(M)
Definition: psi_cond.h:35
void destroy_cond(PFS_cond *pfs)
Destroy instrumentation for a condition instance.
Definition: pfs_instr.cc:433
struct PSI_cond_locker PSI_cond_locker
Definition: psi_cond_bits.h:71
unsigned int PSI_cond_key
Instrumented cond key.
Definition: psi_cond_bits.h:43
@ PSI_COND_WAIT
Wait.
Definition: psi_cond_bits.h:76
@ PSI_COND_TIMEDWAIT
Wait, with timeout.
Definition: psi_cond_bits.h:78
static int inline_mysql_cond_signal(mysql_cond_t *that, const char *src_file[[maybe_unused]], int src_line[[maybe_unused]])
Definition: mysql_cond.h:252
static int inline_mysql_cond_wait(mysql_cond_t *that, mysql_mutex_t *mutex, const char *src_file[[maybe_unused]], int src_line[[maybe_unused]])
Definition: mysql_cond.h:164
static int inline_mysql_cond_init(PSI_cond_key key[[maybe_unused]], mysql_cond_t *that, const char *src_file[[maybe_unused]], int src_line[[maybe_unused]])
Definition: mysql_cond.h:139
static int inline_mysql_cond_destroy(mysql_cond_t *that, const char *src_file[[maybe_unused]], int src_line[[maybe_unused]])
Definition: mysql_cond.h:151
static int inline_mysql_cond_broadcast(mysql_cond_t *that, const char *src_file[[maybe_unused]], int src_line[[maybe_unused]])
Definition: mysql_cond.h:268
static void inline_mysql_cond_register(const char *category[[maybe_unused]], PSI_cond_info *info[[maybe_unused]], int count[[maybe_unused]])
Definition: mysql_cond.h:129
static int inline_mysql_cond_timedwait(mysql_cond_t *that, mysql_mutex_t *mutex, const struct timespec *abstime, const char *src_file[[maybe_unused]], int src_line[[maybe_unused]])
Definition: mysql_cond.h:208
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
static int count
Definition: myisam_ftdump.cc:42
Instrumentation helpers for conditions.
Log info(cout, "NOTE")
static const char * category
Definition: sha2_password.cc:168
Performance schema instrumentation (declarations).
struct result result
Definition: result.h:33
Instrumentation helpers for mutexes.
Performance schema instrumentation interface.
required string key
Definition: replication_asynchronous_connection_failover.proto:59
Condition information.
Definition: psi_cond_bits.h:87
State data storage for start_cond_wait_v1_t.
Definition: psi_cond_bits.h:117
bool m_enabled
Instrumentation is enabled.
Definition: psi_bits.h:175
An instrumented cond structure.
Definition: mysql_cond_bits.h:49
struct PSI_cond * m_psi
The instrumentation hook.
Definition: mysql_cond_bits.h:57
native_cond_t m_cond
The real condition.
Definition: mysql_cond_bits.h:51
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
struct PSI_mutex * m_psi
The instrumentation hook.
Definition: mysql_mutex_bits.h:57
my_mutex_t m_mutex
The real mutex.
Definition: mysql_mutex_bits.h:51
Definition: result.h:29
MySQL condition variable implementation.
static int native_cond_init(native_cond_t *cond)
Definition: thr_cond.h:73
static int native_cond_broadcast(native_cond_t *cond)
Definition: thr_cond.h:121
static int my_cond_timedwait(native_cond_t *cond, my_mutex_t *mp, const struct timespec *abstime)
Definition: thr_cond.h:138
static int my_cond_wait(native_cond_t *cond, my_mutex_t *mp)
Definition: thr_cond.h:152
static int native_cond_signal(native_cond_t *cond)
Definition: thr_cond.h:112
static int native_cond_destroy(native_cond_t *cond[[maybe_unused]])
Definition: thr_cond.h:83