MySQL 9.5.0
Source Code Documentation
thr_cond.h
Go to the documentation of this file.
1#ifndef THR_COND_INCLUDED
2#define THR_COND_INCLUDED
3
4/* Copyright (c) 2014, 2025, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27/**
28 @file include/thr_cond.h
29 MySQL condition variable implementation.
30
31 There are three "layers":
32 1) native_cond_*()
33 Functions that map directly down to OS primitives.
34 Windows - ConditionVariable
35 Other OSes - pthread
36 2) my_cond_*()
37 Functions that use SAFE_MUTEX (default for debug).
38 Otherwise native_cond_*() is used.
39 3) mysql_cond*()
40 Functions that include Performance Schema instrumentation.
41 See include/mysql/psi/mysql_thread.h
42*/
43
44#include <sys/types.h>
45#ifdef _WIN32
46#include <time.h>
47#include "my_inttypes.h"
48#include "my_systime.h"
49#endif
50
53
54#ifdef _WIN32
55/**
56 Convert abstime to milliseconds
57*/
58
59static DWORD get_milliseconds(const struct timespec *abstime) {
60 /*
61 Convert timespec to millis and subtract current time.
62 my_getsystime() returns time in 100 ns units.
63 */
64 const ulonglong future = abstime->tv_sec * 1000 + abstime->tv_nsec / 1000000;
65 const ulonglong now = my_getsystime() / 10000;
66 /* Don't allow the timeout to be negative. */
67 if (future < now) return 0;
68 return (DWORD)(future - now);
69}
70#endif /* _WIN32 */
71
72static inline int native_cond_init(native_cond_t *cond) {
73#ifdef _WIN32
74 InitializeConditionVariable(cond);
75 return 0;
76#else
77 /* pthread_condattr_t is not used in MySQL */
78 return pthread_cond_init(cond, nullptr);
79#endif
80}
81
82static inline int native_cond_destroy(native_cond_t *cond [[maybe_unused]]) {
83#ifdef _WIN32
84 return 0; /* no destroy function */
85#else
86 return pthread_cond_destroy(cond);
87#endif
88}
89
90static inline int native_cond_timedwait(native_cond_t *cond,
91 native_mutex_t *mutex,
92 const struct timespec *abstime) {
93#ifdef _WIN32
94 const DWORD timeout = get_milliseconds(abstime);
95 if (!SleepConditionVariableCS(cond, mutex, timeout)) return ETIMEDOUT;
96 return 0;
97#else
98 return pthread_cond_timedwait(cond, mutex, abstime);
99#endif
100}
101
102static inline int native_cond_wait(native_cond_t *cond, native_mutex_t *mutex) {
103#ifdef _WIN32
104 if (!SleepConditionVariableCS(cond, mutex, INFINITE)) return ETIMEDOUT;
105 return 0;
106#else
107 return pthread_cond_wait(cond, mutex);
108#endif
109}
110
111static inline int native_cond_signal(native_cond_t *cond) {
112#ifdef _WIN32
113 WakeConditionVariable(cond);
114 return 0;
115#else
116 return pthread_cond_signal(cond);
117#endif
118}
119
120static inline int native_cond_broadcast(native_cond_t *cond) {
121#ifdef _WIN32
122 WakeAllConditionVariable(cond);
123 return 0;
124#else
125 return pthread_cond_broadcast(cond);
126#endif
127}
128
129#ifdef SAFE_MUTEX
130struct safe_mutex_t;
131int safe_cond_wait(native_cond_t *cond, safe_mutex_t *mp, const char *file,
132 uint line);
133int safe_cond_timedwait(native_cond_t *cond, safe_mutex_t *mp,
134 const struct timespec *abstime, const char *file,
135 uint line);
136#endif
137
138static inline int my_cond_timedwait(native_cond_t *cond, my_mutex_t *mp,
139 const struct timespec *abstime
140#ifdef SAFE_MUTEX
141 ,
142 const char *file, uint line
143#endif
144) {
145#ifdef SAFE_MUTEX
146 return safe_cond_timedwait(cond, mp->m_u.m_safe_ptr, abstime, file, line);
147#else
148 return native_cond_timedwait(cond, &mp->m_u.m_native, abstime);
149#endif
150}
151
152static inline int my_cond_wait(native_cond_t *cond, my_mutex_t *mp
153#ifdef SAFE_MUTEX
154 ,
155 const char *file, uint line
156#endif
157) {
158#ifdef SAFE_MUTEX
159 return safe_cond_wait(cond, mp->m_u.m_safe_ptr, file, line);
160#else
161 return native_cond_wait(cond, &mp->m_u.m_native);
162#endif
163}
164
165#endif /* THR_COND_INCLUDED */
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
Defines for getting and processing the current system type programmatically.
unsigned long long int my_getsystime()
Get high-resolution time.
Definition: my_systime.h:104
#define ETIMEDOUT
Definition: my_thread.h:48
Definition: os0file.h:89
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:498
Definition: thr_mutex_bits.h:61
union my_mutex_t::u m_u
static int native_cond_init(native_cond_t *cond)
Definition: thr_cond.h:72
static int native_cond_wait(native_cond_t *cond, native_mutex_t *mutex)
Definition: thr_cond.h:102
static int native_cond_broadcast(native_cond_t *cond)
Definition: thr_cond.h:120
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_destroy(native_cond_t *cond)
Definition: thr_cond.h:82
static int native_cond_signal(native_cond_t *cond)
Definition: thr_cond.h:111
static int native_cond_timedwait(native_cond_t *cond, native_mutex_t *mutex, const struct timespec *abstime)
Definition: thr_cond.h:90
MySQL condition variable implementation.
pthread_cond_t native_cond_t
Definition: thr_cond_bits.h:46
ABI for thd_mutex.
pthread_mutex_t native_mutex_t
Definition: thr_mutex_bits.h:55
Include file for Sun RPC to compile out of the box.
native_mutex_t m_native
Definition: thr_mutex_bits.h:63
safe_mutex_t * m_safe_ptr
Definition: thr_mutex_bits.h:64