1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
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 as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
/* Defines to make different thread packages compatible */
|
|
17 |
||
1122.2.10
by Monty Taylor
Fixed all of the include guards. |
18 |
#ifndef MYSYS_MY_PTHREAD_H
|
19 |
#define MYSYS_MY_PTHREAD_H
|
|
1
by brian
clean slate |
20 |
|
549
by Monty Taylor
Took gettext.h out of header files. |
21 |
#include <stdint.h> |
22 |
#include <unistd.h> |
|
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
23 |
#include <signal.h> |
549
by Monty Taylor
Took gettext.h out of header files. |
24 |
|
1
by brian
clean slate |
25 |
#ifndef ETIME
|
26 |
#define ETIME ETIMEDOUT /* For FreeBSD */ |
|
27 |
#endif
|
|
28 |
||
29 |
#ifdef __cplusplus
|
|
30 |
#define EXTERNC extern "C"
|
|
31 |
extern "C" { |
|
32 |
#else
|
|
33 |
#define EXTERNC
|
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
34 |
#endif /* __cplusplus */ |
1
by brian
clean slate |
35 |
|
36 |
#include <pthread.h> |
|
37 |
#ifndef _REENTRANT
|
|
38 |
#define _REENTRANT
|
|
39 |
#endif
|
|
40 |
#ifdef HAVE_SCHED_H
|
|
41 |
#include <sched.h> |
|
42 |
#endif
|
|
43 |
#ifdef HAVE_SYNCH_H
|
|
44 |
#include <synch.h> |
|
45 |
#endif
|
|
46 |
||
47 |
#define pthread_key(T,V) pthread_key_t V
|
|
598.1.1
by Super-User
Fixed solaris build crap. |
48 |
#define pthread_handler_t void *
|
1
by brian
clean slate |
49 |
typedef void *(* pthread_handler)(void *); |
50 |
||
51 |
||
52 |
/*
|
|
53 |
We define my_sigset() and use that instead of the system sigset() so that
|
|
54 |
we can favor an implementation based on sigaction(). On some systems, such
|
|
55 |
as Mac OS X, sigset() results in flags such as SA_RESTART being set, and
|
|
56 |
we want to make sure that no such flags are set.
|
|
57 |
*/
|
|
1192.3.43
by Monty Taylor
Removed many, many checks for functions that do not need to be checked. |
58 |
#if !defined(my_sigset)
|
1
by brian
clean slate |
59 |
#define my_sigset(A,B) do { struct sigaction l_s; sigset_t l_set; int l_rc; \
|
51.3.28
by Jay Pipes
DBUG entirely removed from server and client |
60 |
assert((A) != 0); \
|
1
by brian
clean slate |
61 |
sigemptyset(&l_set); \
|
62 |
l_s.sa_handler = (B); \
|
|
63 |
l_s.sa_mask = l_set; \
|
|
64 |
l_s.sa_flags = 0; \
|
|
65 |
l_rc= sigaction((A), &l_s, (struct sigaction *) NULL);\
|
|
51.3.28
by Jay Pipes
DBUG entirely removed from server and client |
66 |
assert(l_rc == 0); \
|
1
by brian
clean slate |
67 |
} while (0)
|
68 |
#elif defined(HAVE_SIGSET) && !defined(my_sigset)
|
|
69 |
#define my_sigset(A,B) sigset((A),(B))
|
|
70 |
#elif !defined(my_sigset)
|
|
71 |
#define my_sigset(A,B) signal((A),(B))
|
|
72 |
#endif
|
|
73 |
||
74 |
#ifndef my_pthread_attr_setprio
|
|
75 |
#ifdef HAVE_PTHREAD_ATTR_SETPRIO
|
|
76 |
#define my_pthread_attr_setprio(A,B) pthread_attr_setprio((A),(B))
|
|
77 |
#else
|
|
78 |
extern void my_pthread_attr_setprio(pthread_attr_t *attr, int priority); |
|
79 |
#endif
|
|
80 |
#endif
|
|
81 |
||
82 |
#if !defined(HAVE_PTHREAD_YIELD_ONE_ARG) && !defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
|
|
83 |
/* no pthread_yield() available */
|
|
84 |
#ifdef HAVE_SCHED_YIELD
|
|
85 |
#define pthread_yield() sched_yield()
|
|
86 |
#elif defined(HAVE_PTHREAD_YIELD_NP) /* can be Mac OS X */ |
|
87 |
#define pthread_yield() pthread_yield_np()
|
|
88 |
#endif
|
|
89 |
#endif
|
|
90 |
||
91 |
/*
|
|
92 |
The defines set_timespec and set_timespec_nsec should be used
|
|
93 |
for calculating an absolute time at which
|
|
94 |
pthread_cond_timedwait should timeout
|
|
95 |
*/
|
|
96 |
#ifndef set_timespec
|
|
97 |
#define set_timespec(ABSTIME,SEC) \
|
|
98 |
{\
|
|
99 |
struct timeval tv;\
|
|
100 |
gettimeofday(&tv,0);\
|
|
101 |
(ABSTIME).tv_sec=tv.tv_sec+(time_t) (SEC);\
|
|
102 |
(ABSTIME).tv_nsec=tv.tv_usec*1000;\
|
|
103 |
}
|
|
104 |
#endif /* !set_timespec */ |
|
105 |
#ifndef set_timespec_nsec
|
|
106 |
#define set_timespec_nsec(ABSTIME,NSEC) \
|
|
107 |
{\
|
|
151
by Brian Aker
Ulonglong to uint64_t |
108 |
uint64_t now= my_getsystime() + (NSEC/100); \
|
398.1.8
by Monty Taylor
Enabled -Wlong-long. |
109 |
(ABSTIME).tv_sec= (time_t) (now / 10000000UL); \
|
110 |
(ABSTIME).tv_nsec= (long) (now % 10000000UL * 100 + ((NSEC) % 100)); \
|
|
1
by brian
clean slate |
111 |
}
|
112 |
#endif /* !set_timespec_nsec */ |
|
113 |
||
114 |
/* safe_mutex adds checking to mutex for easier debugging */
|
|
115 |
||
116 |
typedef struct st_safe_mutex_t |
|
117 |
{
|
|
118 |
pthread_mutex_t global,mutex; |
|
119 |
const char *file; |
|
411
by Brian Aker
Removed legacy bits around enum. |
120 |
uint32_t line,count; |
1
by brian
clean slate |
121 |
pthread_t thread; |
122 |
} safe_mutex_t; |
|
123 |
||
124 |
int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr, |
|
411
by Brian Aker
Removed legacy bits around enum. |
125 |
const char *file, uint32_t line); |
126 |
int safe_mutex_lock(safe_mutex_t *mp, bool try_lock, const char *file, uint32_t line); |
|
127 |
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint32_t line); |
|
128 |
int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint32_t line); |
|
1
by brian
clean slate |
129 |
int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file, |
411
by Brian Aker
Removed legacy bits around enum. |
130 |
uint32_t line); |
1
by brian
clean slate |
131 |
int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp, |
411
by Brian Aker
Removed legacy bits around enum. |
132 |
struct timespec *abstime, const char *file, uint32_t line); |
1
by brian
clean slate |
133 |
void safe_mutex_global_init(void); |
549
by Monty Taylor
Took gettext.h out of header files. |
134 |
void safe_mutex_end(void); |
1
by brian
clean slate |
135 |
|
136 |
/* Wrappers if safe mutex is actually used */
|
|
137 |
#define safe_mutex_assert_owner(mp)
|
|
138 |
#define safe_mutex_assert_not_owner(mp)
|
|
139 |
||
140 |
/* READ-WRITE thread locking */
|
|
141 |
||
142 |
#if !defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && ! defined(pthread_attr_setstacksize)
|
|
143 |
#define pthread_attr_setstacksize(A,B) pthread_dummy(0)
|
|
144 |
#endif
|
|
145 |
||
146 |
/* Define mutex types, see my_thr_init.c */
|
|
970.1.4
by Brian Aker
Removed dependency on internal_lock for session to lookup global table value |
147 |
#ifdef THREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
|
1
by brian
clean slate |
148 |
extern pthread_mutexattr_t my_fast_mutexattr; |
149 |
#define MY_MUTEX_INIT_FAST &my_fast_mutexattr
|
|
150 |
#else
|
|
151 |
#define MY_MUTEX_INIT_FAST NULL
|
|
152 |
#endif
|
|
153 |
||
154 |
#ifndef ESRCH
|
|
155 |
/* Define it to something */
|
|
156 |
#define ESRCH 1
|
|
157 |
#endif
|
|
158 |
||
553
by Monty Taylor
Changed my_thread_id type. |
159 |
typedef uint64_t my_thread_id; |
1
by brian
clean slate |
160 |
|
146
by Brian Aker
my_bool cleanup. |
161 |
extern bool my_thread_global_init(void); |
1
by brian
clean slate |
162 |
extern void my_thread_global_end(void); |
146
by Brian Aker
my_bool cleanup. |
163 |
extern bool my_thread_init(void); |
1
by brian
clean slate |
164 |
extern void my_thread_end(void); |
165 |
extern const char *my_thread_name(void); |
|
166 |
||
167 |
/* All thread specific variables are in the following struct */
|
|
168 |
||
169 |
/*
|
|
481.1.21
by Monty Taylor
Got rid of some size testing. |
170 |
Drizzle can survive with 32K, but some glibc libraries require > 128K stack
|
171 |
to resolve hostnames. Also recursive stored procedures needs stack.
|
|
1
by brian
clean slate |
172 |
*/
|
481.1.21
by Monty Taylor
Got rid of some size testing. |
173 |
#define DEFAULT_THREAD_STACK (256*INT32_C(1024))
|
1
by brian
clean slate |
174 |
|
175 |
struct st_my_thread_var |
|
176 |
{
|
|
177 |
pthread_cond_t suspend; |
|
178 |
pthread_mutex_t mutex; |
|
179 |
pthread_mutex_t * volatile current_mutex; |
|
180 |
pthread_cond_t * volatile current_cond; |
|
181 |
pthread_t pthread_self; |
|
182 |
my_thread_id id; |
|
183 |
int volatile abort; |
|
146
by Brian Aker
my_bool cleanup. |
184 |
bool init; |
1
by brian
clean slate |
185 |
struct st_my_thread_var *next,**prev; |
186 |
void *opt_info; |
|
187 |
};
|
|
188 |
||
520.4.43
by mordred
A set of Solaris fixes. |
189 |
extern struct st_my_thread_var *_my_thread_var(void); |
1
by brian
clean slate |
190 |
#define my_thread_var (_my_thread_var())
|
191 |
/*
|
|
192 |
Keep track of shutdown,signal, and main threads so that my_end() will not
|
|
193 |
report errors with them
|
|
194 |
*/
|
|
195 |
||
196 |
/* Which kind of thread library is in use */
|
|
197 |
||
198 |
#define THD_LIB_OTHER 1
|
|
199 |
#define THD_LIB_NPTL 2
|
|
200 |
#define THD_LIB_LT 4
|
|
201 |
||
411
by Brian Aker
Removed legacy bits around enum. |
202 |
extern uint32_t thd_lib_detected; |
1
by brian
clean slate |
203 |
|
204 |
/*
|
|
205 |
thread_safe_xxx functions are for critical statistic or counters.
|
|
206 |
The implementation is guaranteed to be thread safe, on all platforms.
|
|
207 |
Note that the calling code should *not* assume the counter is protected
|
|
208 |
by the mutex given, as the implementation of these helpers may change
|
|
209 |
to use my_atomic operations instead.
|
|
210 |
*/
|
|
211 |
||
212 |
#ifndef thread_safe_increment
|
|
213 |
#define thread_safe_increment(V,L) \
|
|
214 |
(pthread_mutex_lock((L)), (V)++, pthread_mutex_unlock((L)))
|
|
215 |
#define thread_safe_decrement(V,L) \
|
|
216 |
(pthread_mutex_lock((L)), (V)--, pthread_mutex_unlock((L)))
|
|
217 |
#endif
|
|
218 |
||
219 |
#ifndef thread_safe_add
|
|
220 |
#define thread_safe_add(V,C,L) \
|
|
221 |
(pthread_mutex_lock((L)), (V)+=(C), pthread_mutex_unlock((L)))
|
|
222 |
#define thread_safe_sub(V,C,L) \
|
|
223 |
(pthread_mutex_lock((L)), (V)-=(C), pthread_mutex_unlock((L)))
|
|
224 |
#endif
|
|
225 |
||
226 |
/*
|
|
227 |
statistics_xxx functions are for non critical statistic,
|
|
228 |
maintained in global variables.
|
|
229 |
- race conditions can occur, making the result slightly inaccurate.
|
|
230 |
- the lock given is not honored.
|
|
231 |
*/
|
|
232 |
#define statistic_decrement(V,L) (V)--
|
|
233 |
#define statistic_increment(V,L) (V)++
|
|
234 |
#define statistic_add(V,C,L) (V)+=(C)
|
|
235 |
#define statistic_sub(V,C,L) (V)-=(C)
|
|
236 |
||
237 |
/*
|
|
238 |
No locking needed, the counter is owned by the thread
|
|
239 |
*/
|
|
240 |
#define status_var_increment(V) (V)++
|
|
241 |
#define status_var_decrement(V) (V)--
|
|
242 |
#define status_var_add(V,C) (V)+=(C)
|
|
243 |
#define status_var_sub(V,C) (V)-=(C)
|
|
244 |
||
245 |
#ifdef __cplusplus
|
|
246 |
}
|
|
247 |
#endif
|
|
1122.2.10
by Monty Taylor
Fixed all of the include guards. |
248 |
#endif /* MYSYS_MY_PTHREAD_H */ |