Eneboo - Documentación para desarrolladores
|
00001 /* Copyright (C) 2000 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 /* Defines to make different thread packages compatible */ 00018 00019 #ifndef _my_pthread_h 00020 #define _my_pthread_h 00021 00022 #include <errno.h> 00023 #ifndef ETIME 00024 #define ETIME ETIMEDOUT /* For FreeBSD */ 00025 #endif 00026 00027 #ifdef __cplusplus 00028 extern "C" { 00029 #endif /* __cplusplus */ 00030 00031 #if defined(__WIN__) || defined(OS2) 00032 00033 #ifdef OS2 00034 typedef ULONG HANDLE; 00035 typedef ULONG DWORD; 00036 typedef int sigset_t; 00037 #endif 00038 00039 #ifdef OS2 00040 typedef HMTX pthread_mutex_t; 00041 #else 00042 typedef CRITICAL_SECTION pthread_mutex_t; 00043 #endif 00044 typedef HANDLE pthread_t; 00045 typedef struct thread_attr { 00046 DWORD dwStackSize ; 00047 DWORD dwCreatingFlag ; 00048 int priority ; 00049 } pthread_attr_t ; 00050 00051 typedef struct { int dummy; } pthread_condattr_t; 00052 00053 /* Implementation of posix conditions */ 00054 00055 typedef struct st_pthread_link { 00056 DWORD thread_id; 00057 struct st_pthread_link *next; 00058 } pthread_link; 00059 00060 typedef struct { 00061 uint32 waiting; 00062 #ifdef OS2 00063 HEV semaphore; 00064 #else 00065 HANDLE semaphore; 00066 #endif 00067 } pthread_cond_t; 00068 00069 00070 #ifndef OS2 00071 struct timespec { /* For pthread_cond_timedwait() */ 00072 time_t tv_sec; 00073 long tv_nsec; 00074 }; 00075 #endif 00076 00077 typedef int pthread_mutexattr_t; 00078 #define win_pthread_self my_thread_var->pthread_self 00079 #ifdef OS2 00080 #define pthread_handler_decl(A,B) void * _Optlink A(void *B) 00081 typedef void * (_Optlink *pthread_handler)(void *); 00082 #else 00083 #define pthread_handler_decl(A,B) void * __cdecl A(void *B) 00084 typedef void * (__cdecl *pthread_handler)(void *); 00085 #endif 00086 00087 void win_pthread_init(void); 00088 int win_pthread_setspecific(void *A,void *B,uint length); 00089 int pthread_create(pthread_t *,pthread_attr_t *,pthread_handler,void *); 00090 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); 00091 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); 00092 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, 00093 struct timespec *abstime); 00094 int pthread_cond_signal(pthread_cond_t *cond); 00095 int pthread_cond_broadcast(pthread_cond_t *cond); 00096 int pthread_cond_destroy(pthread_cond_t *cond); 00097 int pthread_attr_init(pthread_attr_t *connect_att); 00098 int pthread_attr_setstacksize(pthread_attr_t *connect_att,DWORD stack); 00099 int pthread_attr_setprio(pthread_attr_t *connect_att,int priority); 00100 int pthread_attr_destroy(pthread_attr_t *connect_att); 00101 struct tm *localtime_r(const time_t *timep,struct tm *tmp); 00102 struct tm *gmtime_r(const time_t *timep,struct tm *tmp); 00103 00104 00105 void pthread_exit(void *a); /* was #define pthread_exit(A) ExitThread(A)*/ 00106 00107 #ifndef OS2 00108 #define ETIMEDOUT 145 /* Win32 doesn't have this */ 00109 #define getpid() GetCurrentThreadId() 00110 #endif 00111 #define pthread_self() win_pthread_self 00112 #define HAVE_LOCALTIME_R 1 00113 #define _REENTRANT 1 00114 #define HAVE_PTHREAD_ATTR_SETSTACKSIZE 1 00115 00116 #ifdef USE_TLS /* For LIBMYSQL.DLL */ 00117 #undef SAFE_MUTEX /* This will cause conflicts */ 00118 #define pthread_key(T,V) DWORD V 00119 #define pthread_key_create(A,B) ((*A=TlsAlloc())==0xFFFFFFFF) 00120 #define pthread_key_delete(A) TlsFree(A) 00121 #define pthread_getspecific(A) (TlsGetValue(A)) 00122 #define my_pthread_getspecific(T,A) ((T) TlsGetValue(A)) 00123 #define my_pthread_getspecific_ptr(T,V) ((T) TlsGetValue(V)) 00124 #define my_pthread_setspecific_ptr(T,V) (!TlsSetValue((T),(V))) 00125 #define pthread_setspecific(A,B) (!TlsSetValue((A),(B))) 00126 #else 00127 #define pthread_key(T,V) __declspec(thread) T V 00128 #define pthread_key_create(A,B) pthread_dummy(0) 00129 #define pthread_key_delete(A) pthread_dummy(0) 00130 #define pthread_getspecific(A) (&(A)) 00131 #define my_pthread_getspecific(T,A) (&(A)) 00132 #define my_pthread_getspecific_ptr(T,V) (V) 00133 #define my_pthread_setspecific_ptr(T,V) ((T)=(V),0) 00134 #define pthread_setspecific(A,B) win_pthread_setspecific(&(A),(B),sizeof(A)) 00135 #endif /* USE_TLS */ 00136 00137 #define pthread_equal(A,B) ((A) == (B)) 00138 #ifdef OS2 00139 extern int pthread_mutex_init (pthread_mutex_t *, const pthread_mutexattr_t *); 00140 extern int pthread_mutex_lock (pthread_mutex_t *); 00141 extern int pthread_mutex_unlock (pthread_mutex_t *); 00142 extern int pthread_mutex_destroy (pthread_mutex_t *); 00143 #define my_pthread_setprio(A,B) DosSetPriority(PRTYS_THREAD,PRTYC_NOCHANGE, B, A) 00144 #define pthread_kill(A,B) raise(B) 00145 #define pthread_exit(A) pthread_dummy() 00146 #else 00147 #define pthread_mutex_init(A,B) InitializeCriticalSection(A) 00148 #define pthread_mutex_lock(A) (EnterCriticalSection(A),0) 00149 #define pthread_mutex_trylock(A) (WaitForSingleObject((A), 0) == WAIT_TIMEOUT) 00150 #define pthread_mutex_unlock(A) LeaveCriticalSection(A) 00151 #define pthread_mutex_destroy(A) DeleteCriticalSection(A) 00152 #define my_pthread_setprio(A,B) SetThreadPriority(GetCurrentThread(), (B)) 00153 #define pthread_kill(A,B) pthread_dummy(0) 00154 #endif /* OS2 */ 00155 00156 /* Dummy defines for easier code */ 00157 #define pthread_attr_setdetachstate(A,B) pthread_dummy(0) 00158 #define my_pthread_attr_setprio(A,B) pthread_attr_setprio(A,B) 00159 #define pthread_attr_setscope(A,B) 00160 #define pthread_detach_this_thread() 00161 #define pthread_condattr_init(A) 00162 #define pthread_condattr_destroy(A) 00163 00164 /*Irena: compiler does not like this: */ 00165 /*#define my_pthread_getprio(pthread_t thread_id) pthread_dummy(0) */ 00166 #define my_pthread_getprio(thread_id) pthread_dummy(0) 00167 00168 #elif defined(HAVE_UNIXWARE7_THREADS) 00169 00170 #include <thread.h> 00171 #include <synch.h> 00172 00173 #ifndef _REENTRANT 00174 #define _REENTRANT 00175 #endif 00176 00177 #define HAVE_NONPOSIX_SIGWAIT 00178 #define pthread_t thread_t 00179 #define pthread_cond_t cond_t 00180 #define pthread_mutex_t mutex_t 00181 #define pthread_key_t thread_key_t 00182 typedef int pthread_attr_t; /* Needed by Unixware 7.0.0 */ 00183 00184 #define pthread_key_create(A,B) thr_keycreate((A),(B)) 00185 #define pthread_key_delete(A) thr_keydelete(A) 00186 00187 #define pthread_handler_decl(A,B) void *A(void *B) 00188 #define pthread_key(T,V) pthread_key_t V 00189 00190 void * my_pthread_getspecific_imp(pthread_key_t key); 00191 #define my_pthread_getspecific(A,B) ((A) my_pthread_getspecific_imp(B)) 00192 #define my_pthread_getspecific_ptr(T,V) my_pthread_getspecific(T,V) 00193 00194 #define pthread_setspecific(A,B) thr_setspecific(A,B) 00195 #define my_pthread_setspecific_ptr(T,V) pthread_setspecific(T,V) 00196 00197 #define pthread_create(A,B,C,D) thr_create(NULL,65536L,(C),(D),THR_DETACHED,(A)) 00198 #define pthread_cond_init(a,b) cond_init((a),USYNC_THREAD,NULL) 00199 #define pthread_cond_destroy(a) cond_destroy(a) 00200 #define pthread_cond_signal(a) cond_signal(a) 00201 #define pthread_cond_wait(a,b) cond_wait((a),(b)) 00202 #define pthread_cond_timedwait(a,b,c) cond_timedwait((a),(b),(c)) 00203 #define pthread_cond_broadcast(a) cond_broadcast(a) 00204 00205 #define pthread_mutex_init(a,b) mutex_init((a),USYNC_THREAD,NULL) 00206 #define pthread_mutex_lock(a) mutex_lock(a) 00207 #define pthread_mutex_unlock(a) mutex_unlock(a) 00208 #define pthread_mutex_destroy(a) mutex_destroy(a) 00209 00210 #define pthread_self() thr_self() 00211 #define pthread_exit(A) thr_exit(A) 00212 #define pthread_equal(A,B) (((A) == (B)) ? 1 : 0) 00213 #define pthread_kill(A,B) thr_kill((A),(B)) 00214 #define HAVE_PTHREAD_KILL 00215 00216 #define pthread_sigmask(A,B,C) thr_sigsetmask((A),(B),(C)) 00217 00218 extern int my_sigwait(const sigset_t *set,int *sig); 00219 00220 #define pthread_detach_this_thread() pthread_dummy(0) 00221 00222 #define pthread_attr_init(A) pthread_dummy(0) 00223 #define pthread_attr_destroy(A) pthread_dummy(0) 00224 #define pthread_attr_setscope(A,B) pthread_dummy(0) 00225 #define pthread_attr_setdetachstate(A,B) pthread_dummy(0) 00226 #define my_pthread_setprio(A,B) pthread_dummy (0) 00227 #define my_pthread_getprio(A) pthread_dummy (0) 00228 #define my_pthread_attr_setprio(A,B) pthread_dummy(0) 00229 00230 #else /* Normal threads */ 00231 00232 #ifdef HAVE_rts_threads 00233 #define sigwait org_sigwait 00234 #include <signal.h> 00235 #undef sigwait 00236 #endif 00237 #include <pthread.h> 00238 #ifndef _REENTRANT 00239 #define _REENTRANT 00240 #endif 00241 #ifdef HAVE_THR_SETCONCURRENCY 00242 #include <thread.h> /* Probably solaris */ 00243 #endif 00244 #ifdef HAVE_SCHED_H 00245 #include <sched.h> 00246 #endif 00247 #ifdef HAVE_SYNCH_H 00248 #include <synch.h> 00249 #endif 00250 #if defined(__EMX__) && (!defined(EMX_PTHREAD_REV) || (EMX_PTHREAD_REV < 2)) 00251 #error Requires at least rev 2 of EMX pthreads library. 00252 #endif 00253 00254 #ifdef __NETWARE__ 00255 void my_pthread_exit(void *status); 00256 #define pthread_exit(A) my_pthread_exit(A) 00257 #endif 00258 00259 extern int my_pthread_getprio(pthread_t thread_id); 00260 00261 #define pthread_key(T,V) pthread_key_t V 00262 #define my_pthread_getspecific_ptr(T,V) my_pthread_getspecific(T,(V)) 00263 #define my_pthread_setspecific_ptr(T,V) pthread_setspecific(T,(void*) (V)) 00264 #define pthread_detach_this_thread() 00265 #define pthread_handler_decl(A,B) void *A(void *B) 00266 typedef void *(* pthread_handler)(void *); 00267 00268 /* Test first for RTS or FSU threads */ 00269 00270 #if defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM) 00271 #define HAVE_rts_threads 00272 extern int my_pthread_create_detached; 00273 #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C)) 00274 #define PTHREAD_CREATE_DETACHED &my_pthread_create_detached 00275 #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_GLOBAL 00276 #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_LOCAL 00277 #define USE_ALARM_THREAD 00278 #elif defined(HAVE_mit_thread) 00279 #define USE_ALARM_THREAD 00280 #undef HAVE_LOCALTIME_R 00281 #define HAVE_LOCALTIME_R 00282 #undef HAVE_GMTIME_R 00283 #define HAVE_GMTIME_R 00284 #undef HAVE_PTHREAD_ATTR_SETSCOPE 00285 #define HAVE_PTHREAD_ATTR_SETSCOPE 00286 #undef HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE /* If we are running linux */ 00287 #undef HAVE_RWLOCK_T 00288 #undef HAVE_RWLOCK_INIT 00289 #undef HAVE_PTHREAD_RWLOCK_RDLOCK 00290 #undef HAVE_SNPRINTF 00291 00292 #define sigset(A,B) pthread_signal((A),(void (*)(int)) (B)) 00293 #define signal(A,B) pthread_signal((A),(void (*)(int)) (B)) 00294 #define my_pthread_attr_setprio(A,B) 00295 #endif /* defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM) */ 00296 00297 #if defined(_BSDI_VERSION) && _BSDI_VERSION < 199910 00298 int sigwait(sigset_t *set, int *sig); 00299 #endif 00300 00301 #ifndef HAVE_NONPOSIX_SIGWAIT 00302 #define my_sigwait(A,B) sigwait((A),(B)) 00303 #else 00304 int my_sigwait(const sigset_t *set,int *sig); 00305 #endif 00306 00307 #ifdef HAVE_NONPOSIX_PTHREAD_MUTEX_INIT 00308 #ifndef SAFE_MUTEX 00309 #define pthread_mutex_init(a,b) my_pthread_mutex_init((a),(b)) 00310 extern int my_pthread_mutex_init(pthread_mutex_t *mp, 00311 const pthread_mutexattr_t *attr); 00312 #endif /* SAFE_MUTEX */ 00313 #define pthread_cond_init(a,b) my_pthread_cond_init((a),(b)) 00314 extern int my_pthread_cond_init(pthread_cond_t *mp, 00315 const pthread_condattr_t *attr); 00316 #endif /* HAVE_NONPOSIX_PTHREAD_MUTEX_INIT */ 00317 00318 #if defined(HAVE_SIGTHREADMASK) && !defined(HAVE_PTHREAD_SIGMASK) 00319 #define pthread_sigmask(A,B,C) sigthreadmask((A),(B),(C)) 00320 #endif 00321 00322 #if !defined(HAVE_SIGWAIT) && !defined(HAVE_mit_thread) && !defined(HAVE_rts_threads) && !defined(sigwait) && !defined(alpha_linux_port) && !defined(HAVE_NONPOSIX_SIGWAIT) && !defined(HAVE_DEC_3_2_THREADS) && !defined(_AIX) 00323 int sigwait(sigset_t *setp, int *sigp); /* Use our implemention */ 00324 #endif 00325 #if !defined(HAVE_SIGSET) && !defined(HAVE_mit_thread) && !defined(sigset) 00326 #define sigset(A,B) do { struct sigaction s; sigset_t set; \ 00327 sigemptyset(&set); \ 00328 s.sa_handler = (B); \ 00329 s.sa_mask = set; \ 00330 s.sa_flags = 0; \ 00331 sigaction((A), &s, (struct sigaction *) NULL); \ 00332 } while (0) 00333 #endif 00334 00335 #ifndef my_pthread_setprio 00336 #if defined(HAVE_PTHREAD_SETPRIO_NP) /* FSU threads */ 00337 #define my_pthread_setprio(A,B) pthread_setprio_np((A),(B)) 00338 #elif defined(HAVE_PTHREAD_SETPRIO) 00339 #define my_pthread_setprio(A,B) pthread_setprio((A),(B)) 00340 #else 00341 extern void my_pthread_setprio(pthread_t thread_id,int prior); 00342 #endif 00343 #endif 00344 00345 #ifndef my_pthread_attr_setprio 00346 #ifdef HAVE_PTHREAD_ATTR_SETPRIO 00347 #define my_pthread_attr_setprio(A,B) pthread_attr_setprio((A),(B)) 00348 #else 00349 extern void my_pthread_attr_setprio(pthread_attr_t *attr, int priority); 00350 #endif 00351 #endif 00352 00353 #if !defined(HAVE_PTHREAD_ATTR_SETSCOPE) || defined(HAVE_DEC_3_2_THREADS) 00354 #define pthread_attr_setscope(A,B) 00355 #undef HAVE_GETHOSTBYADDR_R /* No definition */ 00356 #endif 00357 00358 #if defined(HAVE_BROKEN_PTHREAD_COND_TIMEDWAIT) && !defined(SAFE_MUTEX) 00359 extern int my_pthread_cond_timedwait(pthread_cond_t *cond, 00360 pthread_mutex_t *mutex, 00361 struct timespec *abstime); 00362 #define pthread_cond_timedwait(A,B,C) my_pthread_cond_timedwait((A),(B),(C)) 00363 #endif 00364 00365 #if defined(OS2) 00366 #define my_pthread_getspecific(T,A) ((T) &(A)) 00367 #define pthread_setspecific(A,B) win_pthread_setspecific(&(A),(B),sizeof(A)) 00368 #elif !defined( HAVE_NONPOSIX_PTHREAD_GETSPECIFIC) 00369 #define my_pthread_getspecific(A,B) ((A) pthread_getspecific(B)) 00370 #else 00371 #define my_pthread_getspecific(A,B) ((A) my_pthread_getspecific_imp(B)) 00372 void *my_pthread_getspecific_imp(pthread_key_t key); 00373 #endif /* OS2 */ 00374 00375 #ifndef HAVE_LOCALTIME_R 00376 struct tm *localtime_r(const time_t *clock, struct tm *res); 00377 #endif 00378 00379 #ifndef HAVE_GMTIME_R 00380 struct tm *gmtime_r(const time_t *clock, struct tm *res); 00381 #endif 00382 00383 #ifdef HAVE_PTHREAD_CONDATTR_CREATE 00384 /* DCE threads on HPUX 10.20 */ 00385 #define pthread_condattr_init pthread_condattr_create 00386 #define pthread_condattr_destroy pthread_condattr_delete 00387 #endif 00388 00389 /* FSU THREADS */ 00390 #if !defined(HAVE_PTHREAD_KEY_DELETE) && !defined(pthread_key_delete) 00391 #define pthread_key_delete(A) pthread_dummy(0) 00392 #endif 00393 00394 #ifdef HAVE_CTHREADS_WRAPPER /* For MacOSX */ 00395 #define pthread_cond_destroy(A) pthread_dummy(0) 00396 #define pthread_mutex_destroy(A) pthread_dummy(0) 00397 #define pthread_attr_delete(A) pthread_dummy(0) 00398 #define pthread_condattr_delete(A) pthread_dummy(0) 00399 #define pthread_attr_setstacksize(A,B) pthread_dummy(0) 00400 #define pthread_equal(A,B) ((A) == (B)) 00401 #define pthread_cond_timedwait(a,b,c) pthread_cond_wait((a),(b)) 00402 #define pthread_attr_init(A) pthread_attr_create(A) 00403 #define pthread_attr_destroy(A) pthread_attr_delete(A) 00404 #define pthread_attr_setdetachstate(A,B) pthread_dummy(0) 00405 #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D)) 00406 #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C)) 00407 #define pthread_kill(A,B) pthread_dummy(0) 00408 #undef pthread_detach_this_thread 00409 #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); } 00410 #endif 00411 00412 #ifdef HAVE_DARWIN_THREADS 00413 #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C)) 00414 #define pthread_kill(A,B) pthread_dummy(0) 00415 #define pthread_condattr_init(A) pthread_dummy(0) 00416 #define pthread_condattr_destroy(A) pthread_dummy(0) 00417 #define pthread_signal(A,B) pthread_dummy(0) 00418 #undef pthread_detach_this_thread 00419 #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(tmp); } 00420 #undef sigset 00421 #define sigset(A,B) pthread_signal((A),(void (*)(int)) (B)) 00422 #endif 00423 00424 #if ((defined(HAVE_PTHREAD_ATTR_CREATE) && !defined(HAVE_SIGWAIT)) || defined(HAVE_DEC_3_2_THREADS)) && !defined(HAVE_CTHREADS_WRAPPER) 00425 /* This is set on AIX_3_2 and Siemens unix (and DEC OSF/1 3.2 too) */ 00426 #define pthread_key_create(A,B) \ 00427 pthread_keycreate(A,(B) ?\ 00428 (pthread_destructor_t) (B) :\ 00429 (pthread_destructor_t) pthread_dummy) 00430 #define pthread_attr_init(A) pthread_attr_create(A) 00431 #define pthread_attr_destroy(A) pthread_attr_delete(A) 00432 #define pthread_attr_setdetachstate(A,B) pthread_dummy(0) 00433 #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D)) 00434 #ifndef pthread_sigmask 00435 #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C)) 00436 #endif 00437 #define pthread_kill(A,B) pthread_dummy(0) 00438 #undef pthread_detach_this_thread 00439 #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); } 00440 #elif !defined(__NETWARE__) /* HAVE_PTHREAD_ATTR_CREATE && !HAVE_SIGWAIT */ 00441 #define HAVE_PTHREAD_KILL 00442 #endif 00443 00444 #endif /* defined(__WIN__) */ 00445 00446 #if defined(HPUX10) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS) 00447 #undef pthread_cond_timedwait 00448 #define pthread_cond_timedwait(a,b,c) my_pthread_cond_timedwait((a),(b),(c)) 00449 int my_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, 00450 struct timespec *abstime); 00451 #endif 00452 00453 #if defined(HPUX10) 00454 #define pthread_attr_getstacksize(A,B) my_pthread_attr_getstacksize(A,B) 00455 void my_pthread_attr_getstacksize(pthread_attr_t *attrib, size_t *size); 00456 #endif 00457 00458 #if defined(HAVE_POSIX1003_4a_MUTEX) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS) 00459 #undef pthread_mutex_trylock 00460 #define pthread_mutex_trylock(a) my_pthread_mutex_trylock((a)) 00461 int my_pthread_mutex_trylock(pthread_mutex_t *mutex); 00462 #endif 00463 00464 /* safe_mutex adds checking to mutex for easier debugging */ 00465 00466 #if defined(__NETWARE__) && !defined(SAFE_MUTEX_DETECT_DESTROY) 00467 #define SAFE_MUTEX_DETECT_DESTROY 00468 #endif 00469 00470 typedef struct st_safe_mutex_t 00471 { 00472 pthread_mutex_t global,mutex; 00473 const char *file; 00474 uint line,count; 00475 pthread_t thread; 00476 #ifdef SAFE_MUTEX_DETECT_DESTROY 00477 struct st_safe_mutex_info_t *info; /* to track destroying of mutexes */ 00478 #endif 00479 } safe_mutex_t; 00480 00481 #ifdef SAFE_MUTEX_DETECT_DESTROY 00482 /* 00483 Used to track the destroying of mutexes. This needs to be a seperate 00484 structure because the safe_mutex_t structure could be freed before 00485 the mutexes are destroyed. 00486 */ 00487 00488 typedef struct st_safe_mutex_info_t 00489 { 00490 struct st_safe_mutex_info_t *next; 00491 struct st_safe_mutex_info_t *prev; 00492 const char *init_file; 00493 uint32 init_line; 00494 } safe_mutex_info_t; 00495 #endif /* SAFE_MUTEX_DETECT_DESTROY */ 00496 00497 int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr, 00498 const char *file, uint line); 00499 int safe_mutex_lock(safe_mutex_t *mp,const char *file, uint line); 00500 int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line); 00501 int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint line); 00502 int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file, 00503 uint line); 00504 int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp, 00505 struct timespec *abstime, const char *file, uint line); 00506 void safe_mutex_global_init(void); 00507 void safe_mutex_end(FILE *file); 00508 00509 /* Wrappers if safe mutex is actually used */ 00510 #ifdef SAFE_MUTEX 00511 #undef pthread_mutex_init 00512 #undef pthread_mutex_lock 00513 #undef pthread_mutex_unlock 00514 #undef pthread_mutex_destroy 00515 #undef pthread_mutex_wait 00516 #undef pthread_mutex_timedwait 00517 #undef pthread_mutex_t 00518 #undef pthread_cond_wait 00519 #undef pthread_cond_timedwait 00520 #undef pthread_mutex_trylock 00521 #define pthread_mutex_init(A,B) safe_mutex_init((A),(B),__FILE__,__LINE__) 00522 #define pthread_mutex_lock(A) safe_mutex_lock((A),__FILE__,__LINE__) 00523 #define pthread_mutex_unlock(A) safe_mutex_unlock((A),__FILE__,__LINE__) 00524 #define pthread_mutex_destroy(A) safe_mutex_destroy((A),__FILE__,__LINE__) 00525 #define pthread_cond_wait(A,B) safe_cond_wait((A),(B),__FILE__,__LINE__) 00526 #define pthread_cond_timedwait(A,B,C) safe_cond_timedwait((A),(B),(C),__FILE__,__LINE__) 00527 #define pthread_mutex_trylock(A) pthread_mutex_lock(A) 00528 #define pthread_mutex_t safe_mutex_t 00529 #define safe_mutex_assert_owner(mp) DBUG_ASSERT((mp)->count > 0 && pthread_equal(pthread_self(),(mp)->thread)) 00530 #else 00531 #define safe_mutex_assert_owner(mp) 00532 #endif /* SAFE_MUTEX */ 00533 00534 /* READ-WRITE thread locking */ 00535 00536 #ifdef HAVE_BROKEN_RWLOCK /* For OpenUnix */ 00537 #undef HAVE_PTHREAD_RWLOCK_RDLOCK 00538 #undef HAVE_RWLOCK_INIT 00539 #undef HAVE_RWLOCK_T 00540 #endif 00541 00542 #if defined(USE_MUTEX_INSTEAD_OF_RW_LOCKS) 00543 /* use these defs for simple mutex locking */ 00544 #define rw_lock_t pthread_mutex_t 00545 #define my_rwlock_init(A,B) pthread_mutex_init((A),(B)) 00546 #define rw_rdlock(A) pthread_mutex_lock((A)) 00547 #define rw_wrlock(A) pthread_mutex_lock((A)) 00548 #define rw_tryrdlock(A) pthread_mutex_trylock((A)) 00549 #define rw_trywrlock(A) pthread_mutex_trylock((A)) 00550 #define rw_unlock(A) pthread_mutex_unlock((A)) 00551 #define rwlock_destroy(A) pthread_mutex_destroy((A)) 00552 #elif defined(HAVE_PTHREAD_RWLOCK_RDLOCK) 00553 #define rw_lock_t pthread_rwlock_t 00554 #define my_rwlock_init(A,B) pthread_rwlock_init((A),(B)) 00555 #define rw_rdlock(A) pthread_rwlock_rdlock(A) 00556 #define rw_wrlock(A) pthread_rwlock_wrlock(A) 00557 #define rw_tryrdlock(A) pthread_rwlock_tryrdlock((A)) 00558 #define rw_trywrlock(A) pthread_rwlock_trywrlock((A)) 00559 #define rw_unlock(A) pthread_rwlock_unlock(A) 00560 #define rwlock_destroy(A) pthread_rwlock_destroy(A) 00561 #elif defined(HAVE_RWLOCK_INIT) 00562 #ifdef HAVE_RWLOCK_T /* For example Solaris 2.6-> */ 00563 #define rw_lock_t rwlock_t 00564 #endif 00565 #define my_rwlock_init(A,B) rwlock_init((A),USYNC_THREAD,0) 00566 #else 00567 /* Use our own version of read/write locks */ 00568 typedef struct _my_rw_lock_t { 00569 pthread_mutex_t lock; /* lock for structure */ 00570 pthread_cond_t readers; /* waiting readers */ 00571 pthread_cond_t writers; /* waiting writers */ 00572 int state; /* -1:writer,0:free,>0:readers */ 00573 int waiters; /* number of waiting writers */ 00574 } my_rw_lock_t; 00575 00576 #define rw_lock_t my_rw_lock_t 00577 #define rw_rdlock(A) my_rw_rdlock((A)) 00578 #define rw_wrlock(A) my_rw_wrlock((A)) 00579 #define rw_tryrdlock(A) my_rw_tryrdlock((A)) 00580 #define rw_trywrlock(A) my_rw_trywrlock((A)) 00581 #define rw_unlock(A) my_rw_unlock((A)) 00582 #define rwlock_destroy(A) my_rwlock_destroy((A)) 00583 00584 extern int my_rwlock_init(my_rw_lock_t *, void *); 00585 extern int my_rwlock_destroy(my_rw_lock_t *); 00586 extern int my_rw_rdlock(my_rw_lock_t *); 00587 extern int my_rw_wrlock(my_rw_lock_t *); 00588 extern int my_rw_unlock(my_rw_lock_t *); 00589 extern int my_rw_tryrdlock(my_rw_lock_t *); 00590 extern int my_rw_trywrlock(my_rw_lock_t *); 00591 #endif /* USE_MUTEX_INSTEAD_OF_RW_LOCKS */ 00592 00593 #define GETHOSTBYADDR_BUFF_SIZE 2048 00594 00595 #ifndef HAVE_THR_SETCONCURRENCY 00596 #define thr_setconcurrency(A) pthread_dummy(0) 00597 #endif 00598 #if !defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && ! defined(pthread_attr_setstacksize) 00599 #define pthread_attr_setstacksize(A,B) pthread_dummy(0) 00600 #endif 00601 00602 /* Define mutex types, see my_thr_init.c */ 00603 #define MY_MUTEX_INIT_SLOW NULL 00604 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 00605 extern pthread_mutexattr_t my_fast_mutexattr; 00606 #define MY_MUTEX_INIT_FAST &my_fast_mutexattr 00607 #else 00608 #define MY_MUTEX_INIT_FAST NULL 00609 #endif 00610 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP 00611 extern pthread_mutexattr_t my_errorcheck_mutexattr; 00612 #define MY_MUTEX_INIT_ERRCHK &my_errorcheck_mutexattr 00613 #else 00614 #define MY_MUTEX_INIT_ERRCHK NULL 00615 #endif 00616 00617 extern my_bool my_thread_global_init(void); 00618 extern void my_thread_global_end(void); 00619 extern my_bool my_thread_init(void); 00620 extern void my_thread_end(void); 00621 extern const char *my_thread_name(void); 00622 extern long my_thread_id(void); 00623 extern int pthread_no_free(void *); 00624 extern int pthread_dummy(int); 00625 00626 /* All thread specific variables are in the following struct */ 00627 00628 #define THREAD_NAME_SIZE 10 00629 #ifndef DEFAULT_THREAD_STACK 00630 #if defined(__ia64__) 00631 /* 00632 MySQL can survive with 32K, but some glibc libraries require > 128K stack 00633 To resolve hostnames 00634 */ 00635 #define DEFAULT_THREAD_STACK (256*1024L) 00636 #else 00637 #define DEFAULT_THREAD_STACK (192*1024) 00638 #endif 00639 #endif 00640 00641 struct st_my_thread_var 00642 { 00643 int thr_errno; 00644 pthread_cond_t suspend; 00645 pthread_mutex_t mutex; 00646 pthread_mutex_t * volatile current_mutex; 00647 pthread_cond_t * volatile current_cond; 00648 pthread_t pthread_self; 00649 long id; 00650 int cmp_length; 00651 int volatile abort; 00652 my_bool init; 00653 struct st_my_thread_var *next,**prev; 00654 void *opt_info; 00655 #ifndef DBUG_OFF 00656 gptr dbug; 00657 char name[THREAD_NAME_SIZE+1]; 00658 #endif 00659 }; 00660 00661 extern struct st_my_thread_var *_my_thread_var(void) __attribute__ ((const)); 00662 #define my_thread_var (_my_thread_var()) 00663 #define my_errno my_thread_var->thr_errno 00664 /* 00665 Keep track of shutdown,signal, and main threads so that my_end() will not 00666 report errors with them 00667 */ 00668 extern pthread_t shutdown_th, main_th, signal_th; 00669 00670 /* statistics_xxx functions are for not essential statistic */ 00671 00672 #ifndef thread_safe_increment 00673 #ifdef HAVE_ATOMIC_ADD 00674 #define thread_safe_increment(V,L) atomic_add(1,(atomic_t*) &V); 00675 #define thread_safe_add(V,C,L) atomic_add((C),(atomic_t*) &V); 00676 #define thread_safe_sub(V,C,L) atomic_sub((C),(atomic_t*) &V); 00677 #else 00678 #define thread_safe_increment(V,L) \ 00679 pthread_mutex_lock((L)); (V)++; pthread_mutex_unlock((L)); 00680 #define thread_safe_add(V,C,L) \ 00681 pthread_mutex_lock((L)); (V)+=(C); pthread_mutex_unlock((L)); 00682 #define thread_safe_sub(V,C,L) \ 00683 pthread_mutex_lock((L)); (V)-=(C); pthread_mutex_unlock((L)); 00684 #endif /* HAVE_ATOMIC_ADD */ 00685 #ifdef SAFE_STATISTICS 00686 #define statistic_increment(V,L) thread_safe_increment((V),(L)) 00687 #define statistic_add(V,C,L) thread_safe_add((V),(C),(L)) 00688 #else 00689 #define statistic_increment(V,L) (V)++ 00690 #define statistic_add(V,C,L) (V)+=(C) 00691 #endif /* SAFE_STATISTICS */ 00692 #endif /* thread_safe_increment */ 00693 00694 #ifdef __cplusplus 00695 } 00696 #endif 00697 #endif /* _my_ptread_h */