~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_pthread.h

  • Committer: Brian Aker
  • Date: 2009-01-07 09:27:07 UTC
  • Revision ID: brian@tangent.org-20090107092707-bn67qpdllfcyh3j9
Removing dead field translator code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/* Defines to make different thread packages compatible */
17
17
 
18
 
 
19
 
 
20
 
#ifndef DRIZZLED_INTERNAL_MY_PTHREAD_H
21
 
#define DRIZZLED_INTERNAL_MY_PTHREAD_H
22
 
 
 
18
#ifndef _my_pthread_h
 
19
#define _my_pthread_h
 
20
 
 
21
#include <stdint.h>
23
22
#include <unistd.h>
 
23
#include <signal.h>
24
24
 
25
 
#include <boost/date_time.hpp>
 
25
#if !defined(__cplusplus)
 
26
# include <stdbool.h>
 
27
#endif
26
28
 
27
29
#ifndef ETIME
28
30
#define ETIME ETIMEDOUT                         /* For FreeBSD */
29
31
#endif
30
32
 
 
33
#ifdef  __cplusplus
 
34
#define EXTERNC extern "C"
 
35
extern "C" {
 
36
#else
 
37
#define EXTERNC
 
38
#endif /* __cplusplus */
 
39
 
31
40
#include <pthread.h>
32
41
#ifndef _REENTRANT
33
42
#define _REENTRANT
39
48
#include <synch.h>
40
49
#endif
41
50
 
42
 
#include "drizzled/visibility.h"
43
 
 
44
 
namespace drizzled
45
 
{
46
 
namespace internal
47
 
{
48
 
 
49
51
#define pthread_key(T,V) pthread_key_t V
 
52
#define pthread_detach_this_thread()
50
53
#define pthread_handler_t void *
51
54
typedef void *(* pthread_handler)(void *);
52
55
 
 
56
#if defined(HAVE_SIGTHREADMASK) && !defined(HAVE_PTHREAD_SIGMASK)
 
57
#define pthread_sigmask(A,B,C) sigthreadmask((A),(B),(C))
 
58
#endif
 
59
 
 
60
 
 
61
/*
 
62
  We define my_sigset() and use that instead of the system sigset() so that
 
63
  we can favor an implementation based on sigaction(). On some systems, such
 
64
  as Mac OS X, sigset() results in flags such as SA_RESTART being set, and
 
65
  we want to make sure that no such flags are set.
 
66
*/
 
67
#if defined(HAVE_SIGACTION) && !defined(my_sigset)
 
68
#define my_sigset(A,B) do { struct sigaction l_s; sigset_t l_set; int l_rc; \
 
69
                            assert((A) != 0);                          \
 
70
                            sigemptyset(&l_set);                            \
 
71
                            l_s.sa_handler = (B);                           \
 
72
                            l_s.sa_mask   = l_set;                          \
 
73
                            l_s.sa_flags   = 0;                             \
 
74
                            l_rc= sigaction((A), &l_s, (struct sigaction *) NULL);\
 
75
                            assert(l_rc == 0);                         \
 
76
                          } while (0)
 
77
#elif defined(HAVE_SIGSET) && !defined(my_sigset)
 
78
#define my_sigset(A,B) sigset((A),(B))
 
79
#elif !defined(my_sigset)
 
80
#define my_sigset(A,B) signal((A),(B))
 
81
#endif
 
82
 
 
83
#ifndef my_pthread_attr_setprio
 
84
#ifdef HAVE_PTHREAD_ATTR_SETPRIO
 
85
#define my_pthread_attr_setprio(A,B) pthread_attr_setprio((A),(B))
 
86
#else
 
87
extern void my_pthread_attr_setprio(pthread_attr_t *attr, int priority);
 
88
#endif
 
89
#endif
 
90
 
 
91
#define HAVE_PTHREAD_KILL
 
92
 
53
93
#if !defined(HAVE_PTHREAD_YIELD_ONE_ARG) && !defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
54
94
/* no pthread_yield() available */
55
95
#ifdef HAVE_SCHED_YIELD
64
104
  for calculating an absolute time at which
65
105
  pthread_cond_timedwait should timeout
66
106
*/
 
107
#ifdef HAVE_TIMESPEC_TS_SEC
 
108
#ifndef set_timespec
 
109
#define set_timespec(ABSTIME,SEC) \
 
110
{ \
 
111
  (ABSTIME).ts_sec=time(0) + (time_t) (SEC); \
 
112
  (ABSTIME).ts_nsec=0; \
 
113
}
 
114
#endif /* !set_timespec */
 
115
#ifndef set_timespec_nsec
 
116
#define set_timespec_nsec(ABSTIME,NSEC) \
 
117
{ \
 
118
  uint64_t now= my_getsystime() + (NSEC/100); \
 
119
  (ABSTIME).ts_sec=  (now / 10000000UL); \
 
120
  (ABSTIME).ts_nsec= (now % 10000000UL * 100 + ((NSEC) % 100)); \
 
121
}
 
122
#endif /* !set_timespec_nsec */
 
123
#else
67
124
#ifndef set_timespec
68
125
#define set_timespec(ABSTIME,SEC) \
69
126
{\
76
133
#ifndef set_timespec_nsec
77
134
#define set_timespec_nsec(ABSTIME,NSEC) \
78
135
{\
79
 
  boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());\
80
 
  boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));\
81
 
  uint64_t t_mark= (mytime-epoch).total_microseconds();\
82
 
  uint64_t now= t_mark + (NSEC/100); \
 
136
  uint64_t now= my_getsystime() + (NSEC/100); \
83
137
  (ABSTIME).tv_sec=  (time_t) (now / 10000000UL);                  \
84
138
  (ABSTIME).tv_nsec= (long) (now % 10000000UL * 100 + ((NSEC) % 100)); \
85
139
}
86
140
#endif /* !set_timespec_nsec */
 
141
#endif /* HAVE_TIMESPEC_TS_SEC */
 
142
 
 
143
        /* safe_mutex adds checking to mutex for easier debugging */
 
144
 
 
145
typedef struct st_safe_mutex_t
 
146
{
 
147
  pthread_mutex_t global,mutex;
 
148
  const char *file;
 
149
  uint32_t line,count;
 
150
  pthread_t thread;
 
151
} safe_mutex_t;
 
152
 
 
153
int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr,
 
154
                    const char *file, uint32_t line);
 
155
int safe_mutex_lock(safe_mutex_t *mp, bool try_lock, const char *file, uint32_t line);
 
156
int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint32_t line);
 
157
int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint32_t line);
 
158
int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
 
159
                   uint32_t line);
 
160
int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
 
161
                        struct timespec *abstime, const char *file, uint32_t line);
 
162
void safe_mutex_global_init(void);
 
163
void safe_mutex_end(void);
87
164
 
88
165
        /* Wrappers if safe mutex is actually used */
89
166
#define safe_mutex_assert_owner(mp)
91
168
 
92
169
        /* READ-WRITE thread locking */
93
170
 
 
171
#ifndef HAVE_THR_SETCONCURRENCY
 
172
#define thr_setconcurrency(A) pthread_dummy(0)
 
173
#endif
94
174
#if !defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && ! defined(pthread_attr_setstacksize)
95
175
#define pthread_attr_setstacksize(A,B) pthread_dummy(0)
96
176
#endif
97
177
 
98
178
/* Define mutex types, see my_thr_init.c */
99
 
#ifdef THREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
 
179
#define MY_MUTEX_INIT_SLOW   NULL
 
180
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
100
181
extern pthread_mutexattr_t my_fast_mutexattr;
101
182
#define MY_MUTEX_INIT_FAST &my_fast_mutexattr
102
183
#else
103
184
#define MY_MUTEX_INIT_FAST   NULL
104
185
#endif
 
186
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
 
187
extern pthread_mutexattr_t my_errorcheck_mutexattr;
 
188
#define MY_MUTEX_INIT_ERRCHK &my_errorcheck_mutexattr
 
189
#else
 
190
#define MY_MUTEX_INIT_ERRCHK   NULL
 
191
#endif
105
192
 
106
193
#ifndef ESRCH
107
194
/* Define it to something */
108
195
#define ESRCH 1
109
196
#endif
110
197
 
 
198
typedef uint64_t my_thread_id;
 
199
 
111
200
extern bool my_thread_global_init(void);
112
201
extern void my_thread_global_end(void);
113
 
DRIZZLED_API bool my_thread_init(void);
114
 
DRIZZLED_API void my_thread_end(void);
 
202
extern bool my_thread_init(void);
 
203
extern void my_thread_end(void);
115
204
extern const char *my_thread_name(void);
 
205
extern my_thread_id my_thread_dbug_id(void);
116
206
 
117
207
/* All thread specific variables are in the following struct */
118
208
 
119
 
/**
120
 
  A default thread stack size of zero means that we are going to use
121
 
  the OS defined thread stack size (this varies from OS to OS).
122
 
 */
123
 
#define DEFAULT_THREAD_STACK    0
124
 
 
125
 
} /* namespace internal */
126
 
} /* namespace drizzled */
127
 
 
128
 
#endif /* DRIZZLED_INTERNAL_MY_PTHREAD_H */
 
209
#define THREAD_NAME_SIZE 10
 
210
/*
 
211
  Drizzle can survive with 32K, but some glibc libraries require > 128K stack
 
212
  to resolve hostnames. Also recursive stored procedures needs stack.
 
213
*/
 
214
#define DEFAULT_THREAD_STACK    (256*INT32_C(1024))
 
215
 
 
216
struct st_my_thread_var
 
217
{
 
218
  pthread_cond_t suspend;
 
219
  pthread_mutex_t mutex;
 
220
  pthread_mutex_t * volatile current_mutex;
 
221
  pthread_cond_t * volatile current_cond;
 
222
  pthread_t pthread_self;
 
223
  my_thread_id id;
 
224
  int cmp_length;
 
225
  int volatile abort;
 
226
  bool init;
 
227
  struct st_my_thread_var *next,**prev;
 
228
  void *opt_info;
 
229
};
 
230
 
 
231
extern struct st_my_thread_var *_my_thread_var(void);
 
232
extern uint32_t my_thread_end_wait_time;
 
233
#define my_thread_var (_my_thread_var())
 
234
/*
 
235
  Keep track of shutdown,signal, and main threads so that my_end() will not
 
236
  report errors with them
 
237
*/
 
238
 
 
239
/* Which kind of thread library is in use */
 
240
 
 
241
#define THD_LIB_OTHER 1
 
242
#define THD_LIB_NPTL  2
 
243
#define THD_LIB_LT    4
 
244
 
 
245
extern uint32_t thd_lib_detected;
 
246
 
 
247
/*
 
248
  thread_safe_xxx functions are for critical statistic or counters.
 
249
  The implementation is guaranteed to be thread safe, on all platforms.
 
250
  Note that the calling code should *not* assume the counter is protected
 
251
  by the mutex given, as the implementation of these helpers may change
 
252
  to use my_atomic operations instead.
 
253
*/
 
254
 
 
255
#ifndef thread_safe_increment
 
256
#define thread_safe_increment(V,L) \
 
257
        (pthread_mutex_lock((L)), (V)++, pthread_mutex_unlock((L)))
 
258
#define thread_safe_decrement(V,L) \
 
259
        (pthread_mutex_lock((L)), (V)--, pthread_mutex_unlock((L)))
 
260
#endif
 
261
 
 
262
#ifndef thread_safe_add
 
263
#define thread_safe_add(V,C,L) \
 
264
        (pthread_mutex_lock((L)), (V)+=(C), pthread_mutex_unlock((L)))
 
265
#define thread_safe_sub(V,C,L) \
 
266
        (pthread_mutex_lock((L)), (V)-=(C), pthread_mutex_unlock((L)))
 
267
#endif
 
268
 
 
269
/*
 
270
  statistics_xxx functions are for non critical statistic,
 
271
  maintained in global variables.
 
272
  - race conditions can occur, making the result slightly inaccurate.
 
273
  - the lock given is not honored.
 
274
*/
 
275
#define statistic_decrement(V,L) (V)--
 
276
#define statistic_increment(V,L) (V)++
 
277
#define statistic_add(V,C,L)     (V)+=(C)
 
278
#define statistic_sub(V,C,L)     (V)-=(C)
 
279
 
 
280
/*
 
281
  No locking needed, the counter is owned by the thread
 
282
*/
 
283
#define status_var_increment(V) (V)++
 
284
#define status_var_decrement(V) (V)--
 
285
#define status_var_add(V,C)     (V)+=(C)
 
286
#define status_var_sub(V,C)     (V)-=(C)
 
287
 
 
288
#ifdef  __cplusplus
 
289
}
 
290
#endif
 
291
#endif /* _my_ptread_h */