MySQL  8.0.18
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, 2017, Oracle and/or its affiliates. All rights reserved.
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 
60 static 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 
73 static 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, NULL);
80 #endif
81 }
82 
83 static inline int native_cond_destroy(
84  native_cond_t *cond MY_ATTRIBUTE((unused))) {
85 #ifdef _WIN32
86  return 0; /* no destroy function */
87 #else
88  return pthread_cond_destroy(cond);
89 #endif
90 }
91 
92 static inline int native_cond_timedwait(native_cond_t *cond,
94  const struct timespec *abstime) {
95 #ifdef _WIN32
96  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 
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 
113 static 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 
122 static 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
132 int safe_cond_wait(native_cond_t *cond, safe_mutex_t *mp, const char *file,
133  uint line);
134 int 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 
139 static 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 
153 static 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 */
Some common macros.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
Defines for getting and processing the current system type programmatically.
static int native_cond_init(native_cond_t *cond)
Definition: thr_cond.h:73
union my_mutex_t::u m_u
pthread_cond_t native_cond_t
Definition: thr_cond_bits.h:45
static int my_cond_wait(native_cond_t *cond, my_mutex_t *mp)
Definition: thr_cond.h:153
Include file for Sun RPC to compile out of the box.
pthread_mutex_t mutex
Definition: memcached.c:384
MySQL mutex implementation.
static int native_cond_destroy(native_cond_t *cond)
Definition: thr_cond.h:83
safe_mutex_t * m_safe_ptr
Definition: thr_mutex_bits.h:63
static int native_cond_broadcast(native_cond_t *cond)
Definition: thr_cond.h:122
native_mutex_t m_native
Definition: thr_mutex_bits.h:62
unsigned int uint
Definition: uca-dump.cc:29
static int my_cond_timedwait(native_cond_t *cond, my_mutex_t *mp, const struct timespec *abstime)
Definition: thr_cond.h:139
unsigned long long int my_getsystime()
Get high-resolution time.
Definition: my_systime.h:100
Defines to make different thread packages compatible.
pthread_mutex_t native_mutex_t
Definition: thr_mutex_bits.h:54
static int native_cond_timedwait(native_cond_t *cond, native_mutex_t *mutex, const struct timespec *abstime)
Definition: thr_cond.h:92
Definition: thr_mutex_bits.h:60
#define NULL
Definition: types.h:55
#define ETIMEDOUT
Definition: my_thread.h:47
static int native_cond_wait(native_cond_t *cond, native_mutex_t *mutex)
Definition: thr_cond.h:104
static time_t abstime(const rel_time_t exptime)
Convert the relative time to an absolute time (relative to EPOC ;) )
Definition: memcached.c:271
static int native_cond_signal(native_cond_t *cond)
Definition: thr_cond.h:113
MySQL condition variable implementation.