MySQL 8.0.31
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, 2022, 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 also distributed 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 included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26/**
27 @file include/thr_cond.h
28 MySQL condition variable implementation.
29
30 There are three "layers":
31 1) native_cond_*()
32 Functions that map directly down to OS primitives.
33 Windows - ConditionVariable
34 Other OSes - pthread
35 2) my_cond_*()
36 Functions that use SAFE_MUTEX (default for debug).
37 Otherwise native_cond_*() is used.
38 3) mysql_cond*()
39 Functions that include Performance Schema instrumentation.
40 See include/mysql/psi/mysql_thread.h
41*/
42
43#include <stddef.h>
44#include <sys/types.h>
45#ifdef _WIN32
46#include <time.h>
47#include "my_systime.h"
48#endif
49
50#include "my_macros.h"
51#include "my_thread.h"
53#include "thr_mutex.h"
54
55#ifdef _WIN32
56/**
57 Convert abstime to milliseconds
58*/
59
60static DWORD get_milliseconds(const struct timespec *abstime) {
61 /*
62 Convert timespec to millis and subtract current time.
63 my_getsystime() returns time in 100 ns units.
64 */
65 ulonglong future = abstime->tv_sec * 1000 + abstime->tv_nsec / 1000000;
66 ulonglong now = my_getsystime() / 10000;
67 /* Don't allow the timeout to be negative. */
68 if (future < now) return 0;
69 return (DWORD)(future - now);
70}
71#endif /* _WIN32 */
72
73static inline int native_cond_init(native_cond_t *cond) {
74#ifdef _WIN32
75 InitializeConditionVariable(cond);
76 return 0;
77#else
78 /* pthread_condattr_t is not used in MySQL */
79 return pthread_cond_init(cond, nullptr);
80#endif
81}
82
83static inline int native_cond_destroy(native_cond_t *cond [[maybe_unused]]) {
84#ifdef _WIN32
85 return 0; /* no destroy function */
86#else
87 return pthread_cond_destroy(cond);
88#endif
89}
90
91static inline int native_cond_timedwait(native_cond_t *cond,
92 native_mutex_t *mutex,
93 const struct timespec *abstime) {
94#ifdef _WIN32
95 DWORD timeout = get_milliseconds(abstime);
96 if (!SleepConditionVariableCS(cond, mutex, timeout)) return ETIMEDOUT;
97 return 0;
98#else
99 return pthread_cond_timedwait(cond, mutex, abstime);
100#endif
101}
102
103static inline int native_cond_wait(native_cond_t *cond, native_mutex_t *mutex) {
104#ifdef _WIN32
105 if (!SleepConditionVariableCS(cond, mutex, INFINITE)) return ETIMEDOUT;
106 return 0;
107#else
108 return pthread_cond_wait(cond, mutex);
109#endif
110}
111
112static inline int native_cond_signal(native_cond_t *cond) {
113#ifdef _WIN32
114 WakeConditionVariable(cond);
115 return 0;
116#else
117 return pthread_cond_signal(cond);
118#endif
119}
120
121static inline int native_cond_broadcast(native_cond_t *cond) {
122#ifdef _WIN32
123 WakeAllConditionVariable(cond);
124 return 0;
125#else
126 return pthread_cond_broadcast(cond);
127#endif
128}
129
130#ifdef SAFE_MUTEX
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 */
unsigned long long int ulonglong
Definition: my_inttypes.h:55
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:101
Defines to make different thread packages compatible.
#define ETIMEDOUT
Definition: my_thread.h:47
Definition: os0file.h:85
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:495
Definition: thr_mutex_bits.h:60
union my_mutex_t::u m_u
static int native_cond_init(native_cond_t *cond)
Definition: thr_cond.h:73
static int native_cond_wait(native_cond_t *cond, native_mutex_t *mutex)
Definition: thr_cond.h:103
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_destroy(native_cond_t *cond)
Definition: thr_cond.h:83
static int native_cond_signal(native_cond_t *cond)
Definition: thr_cond.h:112
static int native_cond_timedwait(native_cond_t *cond, native_mutex_t *mutex, const struct timespec *abstime)
Definition: thr_cond.h:91
MySQL condition variable implementation.
pthread_cond_t native_cond_t
Definition: thr_cond_bits.h:45
MySQL mutex implementation.
pthread_mutex_t native_mutex_t
Definition: thr_mutex_bits.h:54
Include file for Sun RPC to compile out of the box.
unsigned int uint
Definition: uca-dump.cc:29
native_mutex_t m_native
Definition: thr_mutex_bits.h:62
safe_mutex_t * m_safe_ptr
Definition: thr_mutex_bits.h:63