18 #define DONT_REMAP_PTHREAD_FUNCTIONS
20 #include "mysys_priv.h"
23 #include <thr_alarm.h>
25 #if (defined(__BSD__) || defined(_BSDI_VERSION))
26 #define SCHED_POLICY SCHED_RR
28 #define SCHED_POLICY SCHED_OTHER
31 uint thd_lib_detected= 0;
35 #ifdef HAVE_NONPOSIX_PTHREAD_GETSPECIFIC
36 #undef pthread_getspecific
38 void *my_pthread_getspecific_imp(pthread_key_t key)
41 if (pthread_getspecific(key,(
void *) &value))
52 int my_pthread_create_detached=1;
54 #if defined(HAVE_NONPOSIX_SIGWAIT) || defined(HAVE_DEC_3_2_THREADS)
56 int my_sigwait(
const sigset_t *
set,
int *sig)
58 int signal=sigwait((sigset_t*)
set);
68 #if !defined(HAVE_LOCALTIME_R) || !defined(HAVE_GMTIME_R)
74 #if !defined(HAVE_LOCALTIME_R)
75 struct tm *localtime_r(
const time_t *clock,
struct tm *res)
86 #if !defined(HAVE_GMTIME_R)
92 struct tm *gmtime_r(
const time_t *clock,
struct tm *res)
120 #if !defined(HAVE_SIGWAIT) && !defined(sigwait) && !defined(__WIN__) && !defined(HAVE_rts_threads) && !defined(HAVE_NONPOSIX_SIGWAIT) && !defined(HAVE_DEC_3_2_THREADS)
122 #if !defined(DONT_USE_SIGSUSPEND)
124 static sigset_t sigwait_set,rev_sigwait_set,px_recd;
126 void px_handle_sig(
int sig)
128 sigaddset(&px_recd, sig);
132 void sigwait_setup(sigset_t *
set)
136 sigset_t unblock_mask;
139 sact.sa_handler = px_handle_sig;
140 memcpy(&sact.sa_mask,
set,
sizeof(*
set));
141 sigemptyset(&unblock_mask);
142 pthread_sigmask(SIG_UNBLOCK,(sigset_t*) 0,&rev_sigwait_set);
144 for (i = 1; i <=
sizeof(sigwait_set)*8; i++)
146 if (sigismember(
set,i))
148 sigdelset(&rev_sigwait_set,i);
149 if (!sigismember(&sigwait_set,i))
154 sigdelset(&px_recd,i);
155 if (sigismember(&sigwait_set,i))
157 sigaddset(&unblock_mask,i);
158 sigdelset(&rev_sigwait_set,i);
160 sact1.sa_handler = SIG_DFL;
161 sigemptyset(&sact1.sa_mask);
166 memcpy(&sigwait_set,
set,
sizeof(*
set));
167 pthread_sigmask(SIG_BLOCK,(sigset_t*)
set,(sigset_t*) 0);
168 pthread_sigmask(SIG_UNBLOCK,&unblock_mask,(sigset_t*) 0);
172 int sigwait(sigset_t *setp,
int *sigp)
174 if (memcmp(setp,&sigwait_set,
sizeof(sigwait_set)))
186 ulong *ptr= (ulong*) &px_recd;
187 ulong *end=ptr+
sizeof(px_recd)/
sizeof(ulong);
189 for ( ; ptr != end ; ptr++)
194 int found= (int) ((
char*) ptr - (
char*) &px_recd)*8+1;
201 sigdelset(&px_recd,found);
205 sigsuspend(&rev_sigwait_set);
232 static sigset_t pending_set;
233 static bool inited=0;
234 static pthread_cond_t COND_sigwait;
235 static pthread_mutex_t LOCK_sigwait;
238 void sigwait_handle_sig(
int sig)
240 pthread_mutex_lock(&LOCK_sigwait);
241 sigaddset(&pending_set, sig);
242 pthread_cond_signal(&COND_sigwait);
243 pthread_mutex_unlock(&LOCK_sigwait);
246 void *sigwait_thread(
void *set_arg)
248 sigset_t *
set=(sigset_t*) set_arg;
253 sact.sa_handler = sigwait_handle_sig;
254 memcpy(&sact.sa_mask,
set,
sizeof(*
set));
255 sigemptyset(&pending_set);
257 for (i = 1; i <=
sizeof(pending_set)*8; i++)
259 if (sigismember(
set,i))
265 DBUG_ASSERT(thr_client_alarm);
266 sigaddset(
set, thr_client_alarm);
267 pthread_sigmask(SIG_UNBLOCK,(sigset_t*)
set,(sigset_t*) 0);
268 alarm_thread=pthread_self();
272 #ifdef HAVE_NOT_BROKEN_SELECT
283 int sigwait(sigset_t *setp,
int *sigp)
287 pthread_attr_t thr_attr;
288 pthread_t sigwait_thread_id;
290 sigemptyset(&pending_set);
291 pthread_mutex_init(&LOCK_sigwait, MY_MUTEX_INIT_FAST);
292 pthread_cond_init(&COND_sigwait, NULL);
294 pthread_attr_init(&thr_attr);
295 pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_PROCESS);
296 pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED);
297 pthread_attr_setstacksize(&thr_attr,8196);
298 pthread_create(&sigwait_thread_id, &thr_attr, sigwait_thread, setp);
299 pthread_attr_destroy(&thr_attr);
302 pthread_mutex_lock(&LOCK_sigwait);
305 ulong *ptr= (ulong*) &pending_set;
306 ulong *end=ptr+
sizeof(pending_set)/
sizeof(ulong);
308 for ( ; ptr != end ; ptr++)
313 int found= (int) ((
char*) ptr - (
char*) &pending_set)*8+1;
320 sigdelset(&pending_set,found);
321 pthread_mutex_unlock(&LOCK_sigwait);
325 pthread_cond_wait(&COND_sigwait, &LOCK_sigwait);
340 #undef pthread_mutex_init
341 #undef pthread_mutex_lock
342 #undef pthread_mutex_unlock
343 #undef pthread_mutex_destroy
344 #undef pthread_mutex_wait
345 #undef pthread_mutex_timedwait
346 #undef pthread_mutex_trylock
347 #undef pthread_mutex_t
348 #undef pthread_cond_init
349 #undef pthread_cond_wait
350 #undef pthread_cond_timedwait
351 #undef pthread_cond_t
352 #undef pthread_attr_getstacksize
358 #if defined(HAVE_NONPOSIX_PTHREAD_MUTEX_INIT)
362 int my_pthread_mutex_init(pthread_mutex_t *mp,
const pthread_mutexattr_t *attr)
366 error=pthread_mutex_init(mp,pthread_mutexattr_default);
368 error=pthread_mutex_init(mp,*attr);
372 int my_pthread_cond_init(pthread_cond_t *mp,
const pthread_condattr_t *attr)
376 error=pthread_cond_init(mp,pthread_condattr_default);
378 error=pthread_cond_init(mp,*attr);
395 #if defined(HPUX10) || defined(HAVE_BROKEN_PTHREAD_COND_TIMEDWAIT)
397 int my_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
400 int error=pthread_cond_timedwait(cond, mutex, abstime);
414 void my_pthread_attr_getstacksize(pthread_attr_t *connection_attrib,
417 *stack_size= pthread_attr_getstacksize(*connection_attrib);
422 #ifdef HAVE_POSIX1003_4a_MUTEX
451 int my_pthread_mutex_trylock(pthread_mutex_t *mutex)
453 int error= pthread_mutex_trylock(mutex);
466 int pthread_dummy(
int ret)