~drizzle-trunk/drizzle/development

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
18
#ifndef _my_pthread_h
19
#define _my_pthread_h
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
629.1.1 by Monty Taylor
More solaris fixes.
25
#if !defined(__cplusplus)
26
# include <stdbool.h>
27
#endif
28
1 by brian
clean slate
29
#ifndef ETIME
30
#define ETIME ETIMEDOUT				/* For FreeBSD */
31
#endif
32
33
#ifdef  __cplusplus
34
#define EXTERNC extern "C"
35
extern "C" {
36
#else
37
#define EXTERNC
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
38
#endif /* __cplusplus */
1 by brian
clean slate
39
40
#include <pthread.h>
41
#ifndef _REENTRANT
42
#define _REENTRANT
43
#endif
44
#ifdef HAVE_SCHED_H
45
#include <sched.h>
46
#endif
47
#ifdef HAVE_SYNCH_H
48
#include <synch.h>
49
#endif
50
51
#define pthread_key(T,V) pthread_key_t V
598.1.1 by Super-User
Fixed solaris build crap.
52
#define pthread_handler_t void *
1 by brian
clean slate
53
typedef void *(* pthread_handler)(void *);
54
55
#if defined(HAVE_SIGTHREADMASK) && !defined(HAVE_PTHREAD_SIGMASK)
56
#define pthread_sigmask(A,B,C) sigthreadmask((A),(B),(C))
57
#endif
58
59
60
/*
61
  We define my_sigset() and use that instead of the system sigset() so that
62
  we can favor an implementation based on sigaction(). On some systems, such
63
  as Mac OS X, sigset() results in flags such as SA_RESTART being set, and
64
  we want to make sure that no such flags are set.
65
*/
66
#if defined(HAVE_SIGACTION) && !defined(my_sigset)
67
#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
68
                            assert((A) != 0);                          \
1 by brian
clean slate
69
                            sigemptyset(&l_set);                            \
70
                            l_s.sa_handler = (B);                           \
71
                            l_s.sa_mask   = l_set;                          \
72
                            l_s.sa_flags   = 0;                             \
73
                            l_rc= sigaction((A), &l_s, (struct sigaction *) NULL);\
51.3.28 by Jay Pipes
DBUG entirely removed from server and client
74
                            assert(l_rc == 0);                         \
1 by brian
clean slate
75
                          } while (0)
76
#elif defined(HAVE_SIGSET) && !defined(my_sigset)
77
#define my_sigset(A,B) sigset((A),(B))
78
#elif !defined(my_sigset)
79
#define my_sigset(A,B) signal((A),(B))
80
#endif
81
82
#ifndef my_pthread_attr_setprio
83
#ifdef HAVE_PTHREAD_ATTR_SETPRIO
84
#define my_pthread_attr_setprio(A,B) pthread_attr_setprio((A),(B))
85
#else
86
extern void my_pthread_attr_setprio(pthread_attr_t *attr, int priority);
87
#endif
88
#endif
89
90
#define HAVE_PTHREAD_KILL
91
92
#if !defined(HAVE_PTHREAD_YIELD_ONE_ARG) && !defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
93
/* no pthread_yield() available */
94
#ifdef HAVE_SCHED_YIELD
95
#define pthread_yield() sched_yield()
96
#elif defined(HAVE_PTHREAD_YIELD_NP) /* can be Mac OS X */
97
#define pthread_yield() pthread_yield_np()
98
#endif
99
#endif
100
101
/*
102
  The defines set_timespec and set_timespec_nsec should be used
103
  for calculating an absolute time at which
104
  pthread_cond_timedwait should timeout
105
*/
106
#ifdef HAVE_TIMESPEC_TS_SEC
107
#ifndef set_timespec
108
#define set_timespec(ABSTIME,SEC) \
109
{ \
110
  (ABSTIME).ts_sec=time(0) + (time_t) (SEC); \
111
  (ABSTIME).ts_nsec=0; \
112
}
113
#endif /* !set_timespec */
114
#ifndef set_timespec_nsec
115
#define set_timespec_nsec(ABSTIME,NSEC) \
116
{ \
151 by Brian Aker
Ulonglong to uint64_t
117
  uint64_t now= my_getsystime() + (NSEC/100); \
398.1.8 by Monty Taylor
Enabled -Wlong-long.
118
  (ABSTIME).ts_sec=  (now / 10000000UL); \
119
  (ABSTIME).ts_nsec= (now % 10000000UL * 100 + ((NSEC) % 100)); \
1 by brian
clean slate
120
}
121
#endif /* !set_timespec_nsec */
122
#else
123
#ifndef set_timespec
124
#define set_timespec(ABSTIME,SEC) \
125
{\
126
  struct timeval tv;\
127
  gettimeofday(&tv,0);\
128
  (ABSTIME).tv_sec=tv.tv_sec+(time_t) (SEC);\
129
  (ABSTIME).tv_nsec=tv.tv_usec*1000;\
130
}
131
#endif /* !set_timespec */
132
#ifndef set_timespec_nsec
133
#define set_timespec_nsec(ABSTIME,NSEC) \
134
{\
151 by Brian Aker
Ulonglong to uint64_t
135
  uint64_t now= my_getsystime() + (NSEC/100); \
398.1.8 by Monty Taylor
Enabled -Wlong-long.
136
  (ABSTIME).tv_sec=  (time_t) (now / 10000000UL);                  \
137
  (ABSTIME).tv_nsec= (long) (now % 10000000UL * 100 + ((NSEC) % 100)); \
1 by brian
clean slate
138
}
139
#endif /* !set_timespec_nsec */
140
#endif /* HAVE_TIMESPEC_TS_SEC */
141
142
	/* safe_mutex adds checking to mutex for easier debugging */
143
144
typedef struct st_safe_mutex_t
145
{
146
  pthread_mutex_t global,mutex;
147
  const char *file;
411 by Brian Aker
Removed legacy bits around enum.
148
  uint32_t line,count;
1 by brian
clean slate
149
  pthread_t thread;
150
} safe_mutex_t;
151
152
int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr,
411 by Brian Aker
Removed legacy bits around enum.
153
                    const char *file, uint32_t line);
154
int safe_mutex_lock(safe_mutex_t *mp, bool try_lock, const char *file, uint32_t line);
155
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint32_t line);
156
int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint32_t line);
1 by brian
clean slate
157
int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
411 by Brian Aker
Removed legacy bits around enum.
158
		   uint32_t line);
1 by brian
clean slate
159
int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
411 by Brian Aker
Removed legacy bits around enum.
160
			struct timespec *abstime, const char *file, uint32_t line);
1 by brian
clean slate
161
void safe_mutex_global_init(void);
549 by Monty Taylor
Took gettext.h out of header files.
162
void safe_mutex_end(void);
1 by brian
clean slate
163
164
	/* Wrappers if safe mutex is actually used */
165
#define safe_mutex_assert_owner(mp)
166
#define safe_mutex_assert_not_owner(mp)
167
168
	/* READ-WRITE thread locking */
169
170
#ifndef HAVE_THR_SETCONCURRENCY
171
#define thr_setconcurrency(A) pthread_dummy(0)
172
#endif
173
#if !defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && ! defined(pthread_attr_setstacksize)
174
#define pthread_attr_setstacksize(A,B) pthread_dummy(0)
175
#endif
176
177
/* Define mutex types, see my_thr_init.c */
178
#define MY_MUTEX_INIT_SLOW   NULL
179
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
180
extern pthread_mutexattr_t my_fast_mutexattr;
181
#define MY_MUTEX_INIT_FAST &my_fast_mutexattr
182
#else
183
#define MY_MUTEX_INIT_FAST   NULL
184
#endif
185
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
186
extern pthread_mutexattr_t my_errorcheck_mutexattr;
187
#define MY_MUTEX_INIT_ERRCHK &my_errorcheck_mutexattr
188
#else
189
#define MY_MUTEX_INIT_ERRCHK   NULL
190
#endif
191
192
#ifndef ESRCH
193
/* Define it to something */
194
#define ESRCH 1
195
#endif
196
553 by Monty Taylor
Changed my_thread_id type.
197
typedef uint64_t my_thread_id;
1 by brian
clean slate
198
146 by Brian Aker
my_bool cleanup.
199
extern bool my_thread_global_init(void);
1 by brian
clean slate
200
extern void my_thread_global_end(void);
146 by Brian Aker
my_bool cleanup.
201
extern bool my_thread_init(void);
1 by brian
clean slate
202
extern void my_thread_end(void);
203
extern const char *my_thread_name(void);
204
extern my_thread_id my_thread_dbug_id(void);
205
206
/* All thread specific variables are in the following struct */
207
208
#define THREAD_NAME_SIZE 10
209
/*
481.1.21 by Monty Taylor
Got rid of some size testing.
210
  Drizzle can survive with 32K, but some glibc libraries require > 128K stack
211
  to resolve hostnames. Also recursive stored procedures needs stack.
1 by brian
clean slate
212
*/
481.1.21 by Monty Taylor
Got rid of some size testing.
213
#define DEFAULT_THREAD_STACK	(256*INT32_C(1024))
1 by brian
clean slate
214
215
struct st_my_thread_var
216
{
217
  pthread_cond_t suspend;
218
  pthread_mutex_t mutex;
219
  pthread_mutex_t * volatile current_mutex;
220
  pthread_cond_t * volatile current_cond;
221
  pthread_t pthread_self;
222
  my_thread_id id;
223
  int cmp_length;
224
  int volatile abort;
146 by Brian Aker
my_bool cleanup.
225
  bool init;
1 by brian
clean slate
226
  struct st_my_thread_var *next,**prev;
227
  void *opt_info;
228
};
229
520.4.43 by mordred
A set of Solaris fixes.
230
extern struct st_my_thread_var *_my_thread_var(void);
411 by Brian Aker
Removed legacy bits around enum.
231
extern uint32_t my_thread_end_wait_time;
1 by brian
clean slate
232
#define my_thread_var (_my_thread_var())
233
/*
234
  Keep track of shutdown,signal, and main threads so that my_end() will not
235
  report errors with them
236
*/
237
238
/* Which kind of thread library is in use */
239
240
#define THD_LIB_OTHER 1
241
#define THD_LIB_NPTL  2
242
#define THD_LIB_LT    4
243
411 by Brian Aker
Removed legacy bits around enum.
244
extern uint32_t thd_lib_detected;
1 by brian
clean slate
245
246
/*
247
  thread_safe_xxx functions are for critical statistic or counters.
248
  The implementation is guaranteed to be thread safe, on all platforms.
249
  Note that the calling code should *not* assume the counter is protected
250
  by the mutex given, as the implementation of these helpers may change
251
  to use my_atomic operations instead.
252
*/
253
254
#ifndef thread_safe_increment
255
#define thread_safe_increment(V,L) \
256
        (pthread_mutex_lock((L)), (V)++, pthread_mutex_unlock((L)))
257
#define thread_safe_decrement(V,L) \
258
        (pthread_mutex_lock((L)), (V)--, pthread_mutex_unlock((L)))
259
#endif
260
261
#ifndef thread_safe_add
262
#define thread_safe_add(V,C,L) \
263
        (pthread_mutex_lock((L)), (V)+=(C), pthread_mutex_unlock((L)))
264
#define thread_safe_sub(V,C,L) \
265
        (pthread_mutex_lock((L)), (V)-=(C), pthread_mutex_unlock((L)))
266
#endif
267
268
/*
269
  statistics_xxx functions are for non critical statistic,
270
  maintained in global variables.
271
  - race conditions can occur, making the result slightly inaccurate.
272
  - the lock given is not honored.
273
*/
274
#define statistic_decrement(V,L) (V)--
275
#define statistic_increment(V,L) (V)++
276
#define statistic_add(V,C,L)     (V)+=(C)
277
#define statistic_sub(V,C,L)     (V)-=(C)
278
279
/*
280
  No locking needed, the counter is owned by the thread
281
*/
282
#define status_var_increment(V) (V)++
283
#define status_var_decrement(V) (V)--
284
#define status_var_add(V,C)     (V)+=(C)
285
#define status_var_sub(V,C)     (V)-=(C)
286
287
#ifdef  __cplusplus
288
}
289
#endif
290
#endif /* _my_ptread_h */