~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
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
18
#ifndef DRIZZLED_INTERNAL_MY_PTHREAD_H
19
#define DRIZZLED_INTERNAL_MY_PTHREAD_H
1 by brian
clean slate
20
549 by Monty Taylor
Took gettext.h out of header files.
21
#include <unistd.h>
22
1 by brian
clean slate
23
#ifndef ETIME
24
#define ETIME ETIMEDOUT				/* For FreeBSD */
25
#endif
26
27
#include <pthread.h>
28
#ifndef _REENTRANT
29
#define _REENTRANT
30
#endif
31
#ifdef HAVE_SCHED_H
32
#include <sched.h>
33
#endif
34
#ifdef HAVE_SYNCH_H
35
#include <synch.h>
36
#endif
37
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
38
namespace drizzled
39
{
40
namespace internal
41
{
42
1 by brian
clean slate
43
#define pthread_key(T,V) pthread_key_t V
598.1.1 by Super-User
Fixed solaris build crap.
44
#define pthread_handler_t void *
1 by brian
clean slate
45
typedef void *(* pthread_handler)(void *);
46
47
#ifndef my_pthread_attr_setprio
48
#ifdef HAVE_PTHREAD_ATTR_SETPRIO
49
#define my_pthread_attr_setprio(A,B) pthread_attr_setprio((A),(B))
50
#else
51
extern void my_pthread_attr_setprio(pthread_attr_t *attr, int priority);
52
#endif
53
#endif
54
55
#if !defined(HAVE_PTHREAD_YIELD_ONE_ARG) && !defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
56
/* no pthread_yield() available */
57
#ifdef HAVE_SCHED_YIELD
58
#define pthread_yield() sched_yield()
59
#elif defined(HAVE_PTHREAD_YIELD_NP) /* can be Mac OS X */
60
#define pthread_yield() pthread_yield_np()
61
#endif
62
#endif
63
64
/*
65
  The defines set_timespec and set_timespec_nsec should be used
66
  for calculating an absolute time at which
67
  pthread_cond_timedwait should timeout
68
*/
69
#ifndef set_timespec
70
#define set_timespec(ABSTIME,SEC) \
71
{\
72
  struct timeval tv;\
73
  gettimeofday(&tv,0);\
74
  (ABSTIME).tv_sec=tv.tv_sec+(time_t) (SEC);\
75
  (ABSTIME).tv_nsec=tv.tv_usec*1000;\
76
}
77
#endif /* !set_timespec */
78
#ifndef set_timespec_nsec
79
#define set_timespec_nsec(ABSTIME,NSEC) \
80
{\
151 by Brian Aker
Ulonglong to uint64_t
81
  uint64_t now= my_getsystime() + (NSEC/100); \
398.1.8 by Monty Taylor
Enabled -Wlong-long.
82
  (ABSTIME).tv_sec=  (time_t) (now / 10000000UL);                  \
83
  (ABSTIME).tv_nsec= (long) (now % 10000000UL * 100 + ((NSEC) % 100)); \
1 by brian
clean slate
84
}
85
#endif /* !set_timespec_nsec */
86
87
	/* safe_mutex adds checking to mutex for easier debugging */
88
89
typedef struct st_safe_mutex_t
90
{
91
  pthread_mutex_t global,mutex;
92
  const char *file;
411 by Brian Aker
Removed legacy bits around enum.
93
  uint32_t line,count;
1 by brian
clean slate
94
  pthread_t thread;
95
} safe_mutex_t;
96
97
int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr,
411 by Brian Aker
Removed legacy bits around enum.
98
                    const char *file, uint32_t line);
99
int safe_mutex_lock(safe_mutex_t *mp, bool try_lock, const char *file, uint32_t line);
100
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint32_t line);
101
int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint32_t line);
1 by brian
clean slate
102
int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
411 by Brian Aker
Removed legacy bits around enum.
103
		   uint32_t line);
1 by brian
clean slate
104
int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
411 by Brian Aker
Removed legacy bits around enum.
105
			struct timespec *abstime, const char *file, uint32_t line);
1 by brian
clean slate
106
void safe_mutex_global_init(void);
549 by Monty Taylor
Took gettext.h out of header files.
107
void safe_mutex_end(void);
1 by brian
clean slate
108
109
	/* Wrappers if safe mutex is actually used */
110
#define safe_mutex_assert_owner(mp)
111
#define safe_mutex_assert_not_owner(mp)
112
113
	/* READ-WRITE thread locking */
114
115
#if !defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && ! defined(pthread_attr_setstacksize)
116
#define pthread_attr_setstacksize(A,B) pthread_dummy(0)
117
#endif
118
119
/* 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
120
#ifdef THREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
1 by brian
clean slate
121
extern pthread_mutexattr_t my_fast_mutexattr;
122
#define MY_MUTEX_INIT_FAST &my_fast_mutexattr
123
#else
124
#define MY_MUTEX_INIT_FAST   NULL
125
#endif
126
127
#ifndef ESRCH
128
/* Define it to something */
129
#define ESRCH 1
130
#endif
131
146 by Brian Aker
my_bool cleanup.
132
extern bool my_thread_global_init(void);
1 by brian
clean slate
133
extern void my_thread_global_end(void);
146 by Brian Aker
my_bool cleanup.
134
extern bool my_thread_init(void);
1 by brian
clean slate
135
extern void my_thread_end(void);
136
extern const char *my_thread_name(void);
137
138
/* All thread specific variables are in the following struct */
139
140
/*
481.1.21 by Monty Taylor
Got rid of some size testing.
141
  Drizzle can survive with 32K, but some glibc libraries require > 128K stack
142
  to resolve hostnames. Also recursive stored procedures needs stack.
1 by brian
clean slate
143
*/
481.1.21 by Monty Taylor
Got rid of some size testing.
144
#define DEFAULT_THREAD_STACK	(256*INT32_C(1024))
1 by brian
clean slate
145
146
struct st_my_thread_var
147
{
148
  pthread_cond_t suspend;
149
  pthread_mutex_t mutex;
150
  pthread_mutex_t * volatile current_mutex;
151
  pthread_cond_t * volatile current_cond;
152
  pthread_t pthread_self;
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
153
  uint64_t id;
1 by brian
clean slate
154
  int volatile abort;
146 by Brian Aker
my_bool cleanup.
155
  bool init;
1 by brian
clean slate
156
  struct st_my_thread_var *next,**prev;
157
  void *opt_info;
158
};
159
520.4.43 by mordred
A set of Solaris fixes.
160
extern struct st_my_thread_var *_my_thread_var(void);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
161
#define my_thread_var (::drizzled::internal::_my_thread_var())
1 by brian
clean slate
162
/*
163
  Keep track of shutdown,signal, and main threads so that my_end() will not
164
  report errors with them
165
*/
166
167
/* Which kind of thread library is in use */
168
169
#define THD_LIB_OTHER 1
170
#define THD_LIB_NPTL  2
171
#define THD_LIB_LT    4
172
411 by Brian Aker
Removed legacy bits around enum.
173
extern uint32_t thd_lib_detected;
1 by brian
clean slate
174
175
/*
176
  thread_safe_xxx functions are for critical statistic or counters.
177
  The implementation is guaranteed to be thread safe, on all platforms.
178
  Note that the calling code should *not* assume the counter is protected
179
  by the mutex given, as the implementation of these helpers may change
180
  to use my_atomic operations instead.
181
*/
182
183
#ifndef thread_safe_increment
184
#define thread_safe_increment(V,L) \
185
        (pthread_mutex_lock((L)), (V)++, pthread_mutex_unlock((L)))
186
#define thread_safe_decrement(V,L) \
187
        (pthread_mutex_lock((L)), (V)--, pthread_mutex_unlock((L)))
188
#endif
189
190
#ifndef thread_safe_add
191
#define thread_safe_add(V,C,L) \
192
        (pthread_mutex_lock((L)), (V)+=(C), pthread_mutex_unlock((L)))
193
#define thread_safe_sub(V,C,L) \
194
        (pthread_mutex_lock((L)), (V)-=(C), pthread_mutex_unlock((L)))
195
#endif
196
197
/*
198
  statistics_xxx functions are for non critical statistic,
199
  maintained in global variables.
200
  - race conditions can occur, making the result slightly inaccurate.
201
  - the lock given is not honored.
202
*/
203
#define statistic_decrement(V,L) (V)--
204
#define statistic_increment(V,L) (V)++
205
#define statistic_add(V,C,L)     (V)+=(C)
206
#define statistic_sub(V,C,L)     (V)-=(C)
207
208
/*
209
  No locking needed, the counter is owned by the thread
210
*/
211
#define status_var_increment(V) (V)++
212
#define status_var_decrement(V) (V)--
213
#define status_var_add(V,C)     (V)+=(C)
214
#define status_var_sub(V,C)     (V)-=(C)
215
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
216
} /* namespace internal */
217
} /* namespace drizzled */
218
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
219
#endif /* DRIZZLED_INTERNAL_MY_PTHREAD_H */