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 */
16
16
/* Defines to make different thread packages compatible */
20
18
#ifndef DRIZZLED_INTERNAL_MY_PTHREAD_H
21
19
#define DRIZZLED_INTERNAL_MY_PTHREAD_H
23
21
#include <unistd.h>
25
#include <boost/date_time.hpp>
28
24
#define ETIME ETIMEDOUT /* For FreeBSD */
76
78
#ifndef set_timespec_nsec
77
79
#define set_timespec_nsec(ABSTIME,NSEC) \
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); \
81
uint64_t now= my_getsystime() + (NSEC/100); \
83
82
(ABSTIME).tv_sec= (time_t) (now / 10000000UL); \
84
83
(ABSTIME).tv_nsec= (long) (now % 10000000UL * 100 + ((NSEC) % 100)); \
86
85
#endif /* !set_timespec_nsec */
87
/* safe_mutex adds checking to mutex for easier debugging */
89
typedef struct st_safe_mutex_t
91
pthread_mutex_t global,mutex;
97
int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr,
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);
102
int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
104
int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
105
struct timespec *abstime, const char *file, uint32_t line);
106
void safe_mutex_global_init(void);
107
void safe_mutex_end(void);
88
109
/* Wrappers if safe mutex is actually used */
89
110
#define safe_mutex_assert_owner(mp)
90
111
#define safe_mutex_assert_not_owner(mp)
111
132
extern bool my_thread_global_init(void);
112
133
extern void my_thread_global_end(void);
113
DRIZZLED_API bool my_thread_init(void);
114
DRIZZLED_API void my_thread_end(void);
134
extern bool my_thread_init(void);
135
extern void my_thread_end(void);
115
136
extern const char *my_thread_name(void);
117
138
/* All thread specific variables are in the following struct */
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).
123
#define DEFAULT_THREAD_STACK 0
141
Drizzle can survive with 32K, but some glibc libraries require > 128K stack
142
to resolve hostnames. Also recursive stored procedures needs stack.
144
#define DEFAULT_THREAD_STACK (256*INT32_C(1024))
146
struct st_my_thread_var
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;
156
struct st_my_thread_var *next,**prev;
160
extern struct st_my_thread_var *_my_thread_var(void);
161
#define my_thread_var (::drizzled::internal::_my_thread_var())
163
Keep track of shutdown,signal, and main threads so that my_end() will not
164
report errors with them
167
/* Which kind of thread library is in use */
169
#define THD_LIB_OTHER 1
170
#define THD_LIB_NPTL 2
173
extern uint32_t thd_lib_detected;
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.
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)))
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)))
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.
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)
209
No locking needed, the counter is owned by the thread
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)
125
216
} /* namespace internal */
126
217
} /* namespace drizzled */