~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/internal/my_pthread.h

  • Committer: Brian Aker
  • Date: 2010-01-22 00:53:13 UTC
  • Revision ID: brian@gaz-20100122005313-jmizcbcdi1lt4tcx
Revert db patch.

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
18
#ifndef DRIZZLED_INTERNAL_MY_PTHREAD_H
21
19
#define DRIZZLED_INTERNAL_MY_PTHREAD_H
22
20
 
 
21
#include <stdint.h>
23
22
#include <unistd.h>
24
 
 
25
 
#include <boost/date_time.hpp>
 
23
#include <signal.h>
26
24
 
27
25
#ifndef ETIME
28
26
#define ETIME ETIMEDOUT                         /* For FreeBSD */
29
27
#endif
30
28
 
 
29
#ifdef  __cplusplus
 
30
#define EXTERNC extern "C"
 
31
extern "C" {
 
32
#else
 
33
#define EXTERNC
 
34
#endif /* __cplusplus */
 
35
 
31
36
#include <pthread.h>
32
37
#ifndef _REENTRANT
33
38
#define _REENTRANT
39
44
#include <synch.h>
40
45
#endif
41
46
 
42
 
#include <drizzled/visibility.h>
43
 
 
44
 
namespace drizzled
45
 
{
46
 
namespace internal
47
 
{
48
 
 
49
47
#define pthread_key(T,V) pthread_key_t V
50
48
#define pthread_handler_t void *
51
49
typedef void *(* pthread_handler)(void *);
52
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
*/
 
58
#if !defined(my_sigset)
 
59
#define my_sigset(A,B) do { struct sigaction l_s; sigset_t l_set; int l_rc; \
 
60
                            assert((A) != 0);                          \
 
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);\
 
66
                            assert(l_rc == 0);                         \
 
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
 
53
82
#if !defined(HAVE_PTHREAD_YIELD_ONE_ARG) && !defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
54
83
/* no pthread_yield() available */
55
84
#ifdef HAVE_SCHED_YIELD
76
105
#ifndef set_timespec_nsec
77
106
#define set_timespec_nsec(ABSTIME,NSEC) \
78
107
{\
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); \
 
108
  uint64_t now= my_getsystime() + (NSEC/100); \
83
109
  (ABSTIME).tv_sec=  (time_t) (now / 10000000UL);                  \
84
110
  (ABSTIME).tv_nsec= (long) (now % 10000000UL * 100 + ((NSEC) % 100)); \
85
111
}
86
112
#endif /* !set_timespec_nsec */
87
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;
 
120
  uint32_t line,count;
 
121
  pthread_t thread;
 
122
} safe_mutex_t;
 
123
 
 
124
int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr,
 
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);
 
129
int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
 
130
                   uint32_t line);
 
131
int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
 
132
                        struct timespec *abstime, const char *file, uint32_t line);
 
133
void safe_mutex_global_init(void);
 
134
void safe_mutex_end(void);
 
135
 
88
136
        /* Wrappers if safe mutex is actually used */
89
137
#define safe_mutex_assert_owner(mp)
90
138
#define safe_mutex_assert_not_owner(mp)
110
158
 
111
159
extern bool my_thread_global_init(void);
112
160
extern void my_thread_global_end(void);
113
 
DRIZZLED_API bool my_thread_init(void);
114
 
DRIZZLED_API void my_thread_end(void);
 
161
extern bool my_thread_init(void);
 
162
extern void my_thread_end(void);
115
163
extern const char *my_thread_name(void);
116
164
 
117
165
/* All thread specific variables are in the following struct */
118
166
 
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
 
 
 
167
/*
 
168
  Drizzle can survive with 32K, but some glibc libraries require > 128K stack
 
169
  to resolve hostnames. Also recursive stored procedures needs stack.
 
170
*/
 
171
#define DEFAULT_THREAD_STACK    (256*INT32_C(1024))
 
172
 
 
173
struct st_my_thread_var
 
174
{
 
175
  pthread_cond_t suspend;
 
176
  pthread_mutex_t mutex;
 
177
  pthread_mutex_t * volatile current_mutex;
 
178
  pthread_cond_t * volatile current_cond;
 
179
  pthread_t pthread_self;
 
180
  uint64_t id;
 
181
  int volatile abort;
 
182
  bool init;
 
183
  struct st_my_thread_var *next,**prev;
 
184
  void *opt_info;
 
185
};
 
186
 
 
187
extern struct st_my_thread_var *_my_thread_var(void);
 
188
#define my_thread_var (_my_thread_var())
 
189
/*
 
190
  Keep track of shutdown,signal, and main threads so that my_end() will not
 
191
  report errors with them
 
192
*/
 
193
 
 
194
/* Which kind of thread library is in use */
 
195
 
 
196
#define THD_LIB_OTHER 1
 
197
#define THD_LIB_NPTL  2
 
198
#define THD_LIB_LT    4
 
199
 
 
200
extern uint32_t thd_lib_detected;
 
201
 
 
202
/*
 
203
  thread_safe_xxx functions are for critical statistic or counters.
 
204
  The implementation is guaranteed to be thread safe, on all platforms.
 
205
  Note that the calling code should *not* assume the counter is protected
 
206
  by the mutex given, as the implementation of these helpers may change
 
207
  to use my_atomic operations instead.
 
208
*/
 
209
 
 
210
#ifndef thread_safe_increment
 
211
#define thread_safe_increment(V,L) \
 
212
        (pthread_mutex_lock((L)), (V)++, pthread_mutex_unlock((L)))
 
213
#define thread_safe_decrement(V,L) \
 
214
        (pthread_mutex_lock((L)), (V)--, pthread_mutex_unlock((L)))
 
215
#endif
 
216
 
 
217
#ifndef thread_safe_add
 
218
#define thread_safe_add(V,C,L) \
 
219
        (pthread_mutex_lock((L)), (V)+=(C), pthread_mutex_unlock((L)))
 
220
#define thread_safe_sub(V,C,L) \
 
221
        (pthread_mutex_lock((L)), (V)-=(C), pthread_mutex_unlock((L)))
 
222
#endif
 
223
 
 
224
/*
 
225
  statistics_xxx functions are for non critical statistic,
 
226
  maintained in global variables.
 
227
  - race conditions can occur, making the result slightly inaccurate.
 
228
  - the lock given is not honored.
 
229
*/
 
230
#define statistic_decrement(V,L) (V)--
 
231
#define statistic_increment(V,L) (V)++
 
232
#define statistic_add(V,C,L)     (V)+=(C)
 
233
#define statistic_sub(V,C,L)     (V)-=(C)
 
234
 
 
235
/*
 
236
  No locking needed, the counter is owned by the thread
 
237
*/
 
238
#define status_var_increment(V) (V)++
 
239
#define status_var_decrement(V) (V)--
 
240
#define status_var_add(V,C)     (V)+=(C)
 
241
#define status_var_sub(V,C)     (V)-=(C)
 
242
 
 
243
#ifdef  __cplusplus
 
244
}
 
245
#endif
128
246
#endif /* DRIZZLED_INTERNAL_MY_PTHREAD_H */