MySQL 9.0.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, 2024, 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 <stddef.h>
45#include <sys/types.h>
46#ifdef _WIN32
47#include <time.h>
48#include "my_systime.h"
49#endif
50
51#include "my_macros.h"
52#include "my_thread.h"
54#include "thr_mutex.h"
55
56#ifdef _WIN32
57/**
58 Convert abstime to milliseconds
59*/
60
61static DWORD get_milliseconds(const struct timespec *abstime) {
62 /*
63 Convert timespec to millis and subtract current time.
64 my_getsystime() returns time in 100 ns units.
65 */
66 const ulonglong future = abstime->tv_sec * 1000 + abstime->tv_nsec / 1000000;
67 const ulonglong now = my_getsystime() / 10000;
68 /* Don't allow the timeout to be negative. */
69 if (future < now) return 0;
70 return (DWORD)(future - now);
71}
72#endif /* _WIN32 */
73
74static inline int native_cond_init(native_cond_t *cond) {
75#ifdef _WIN32
76 InitializeConditionVariable(cond);
77 return 0;
78#else
79 /* pthread_condattr_t is not used in MySQL */
80 return pthread_cond_init(cond, nullptr);
81#endif
82}
83
84static inline int native_cond_destroy(native_cond_t *cond [[maybe_unused]]) {
85#ifdef _WIN32
86 return 0; /* no destroy function */
87#else
88 return pthread_cond_destroy(cond);
89#endif
90}
91
92static inline int native_cond_timedwait(native_cond_t *cond,
93 native_mutex_t *mutex,
94 const struct timespec *abstime) {
95#ifdef _WIN32
96 const DWORD timeout = get_milliseconds(abstime);
97 if (!SleepConditionVariableCS(cond, mutex, timeout)) return ETIMEDOUT;
98 return 0;
99#else
100 return pthread_cond_timedwait(cond, mutex, abstime);
101#endif
102}
103
104static inline int native_cond_wait(native_cond_t *cond, native_mutex_t *mutex) {
105#ifdef _WIN32
106 if (!SleepConditionVariableCS(cond, mutex, INFINITE)) return ETIMEDOUT;
107 return 0;
108#else
109 return pthread_cond_wait(cond, mutex);
110#endif
111}
112
113static inline int native_cond_signal(native_cond_t *cond) {
114#ifdef _WIN32
115 WakeConditionVariable(cond);
116 return 0;
117#else
118 return pthread_cond_signal(cond);
119#endif
120}
121
122static inline int native_cond_broadcast(native_cond_t *cond) {
123#ifdef _WIN32
124 WakeAllConditionVariable(cond);
125 return 0;
126#else
127 return pthread_cond_broadcast(cond);
128#endif
129}
130
131#ifdef SAFE_MUTEX
132int safe_cond_wait(native_cond_t *cond, safe_mutex_t *mp, const char *file,
133 uint line);
134int safe_cond_timedwait(native_cond_t *cond, safe_mutex_t *mp,
135 const struct timespec *abstime, const char *file,
136 uint line);
137#endif
138
139static inline int my_cond_timedwait(native_cond_t *cond, my_mutex_t *mp,
140 const struct timespec *abstime
141#ifdef SAFE_MUTEX
142 ,
143 const char *file, uint line
144#endif
145) {
146#ifdef SAFE_MUTEX
147 return safe_cond_timedwait(cond, mp->m_u.m_safe_ptr, abstime, file, line);
148#else
149 return native_cond_timedwait(cond, &mp->m_u.m_native, abstime);
150#endif
151}
152
153static inline int my_cond_wait(native_cond_t *cond, my_mutex_t *mp
154#ifdef SAFE_MUTEX
155 ,
156 const char *file, uint line
157#endif
158) {
159#ifdef SAFE_MUTEX
160 return safe_cond_wait(cond, mp->m_u.m_safe_ptr, file, line);
161#else
162 return native_cond_wait(cond, &mp->m_u.m_native);
163#endif
164}
165
166#endif /* THR_COND_INCLUDED */
unsigned long long int ulonglong
Definition: my_inttypes.h:56
Some common macros.
Defines for getting and processing the current system type programmatically.
unsigned long long int my_getsystime()
Get high-resolution time.
Definition: my_systime.h:102
Defines to make different thread packages compatible.
#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:74
static int native_cond_wait(native_cond_t *cond, native_mutex_t *mutex)
Definition: thr_cond.h:104
static int native_cond_broadcast(native_cond_t *cond)
Definition: thr_cond.h:122
static int my_cond_timedwait(native_cond_t *cond, my_mutex_t *mp, const struct timespec *abstime)
Definition: thr_cond.h:139
static int my_cond_wait(native_cond_t *cond, my_mutex_t *mp)
Definition: thr_cond.h:153
static int native_cond_destroy(native_cond_t *cond)
Definition: thr_cond.h:84
static int native_cond_signal(native_cond_t *cond)
Definition: thr_cond.h:113
static int native_cond_timedwait(native_cond_t *cond, native_mutex_t *mutex, const struct timespec *abstime)
Definition: thr_cond.h:92
MySQL condition variable implementation.
pthread_cond_t native_cond_t
Definition: thr_cond_bits.h:46
MySQL mutex implementation.
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