Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

my_pthread.h

Go to the documentation of this file.
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 #if defined(HAVE_UNIXWARE7_POSIX)
00302 #undef HAVE_NONPOSIX_SIGWAIT
00303 #define HAVE_NONPOSIX_SIGWAIT   /* sigwait takes only 1 argument */
00304 #endif
00305 
00306 #ifndef HAVE_NONPOSIX_SIGWAIT
00307 #define my_sigwait(A,B) sigwait((A),(B))
00308 #else
00309 int my_sigwait(const sigset_t *set,int *sig);
00310 #endif
00311 
00312 #ifdef HAVE_NONPOSIX_PTHREAD_MUTEX_INIT
00313 #ifndef SAFE_MUTEX
00314 #define pthread_mutex_init(a,b) my_pthread_mutex_init((a),(b))
00315 extern int my_pthread_mutex_init(pthread_mutex_t *mp,
00316                                  const pthread_mutexattr_t *attr);
00317 #endif /* SAFE_MUTEX */
00318 #define pthread_cond_init(a,b) my_pthread_cond_init((a),(b))
00319 extern int my_pthread_cond_init(pthread_cond_t *mp,
00320                                 const pthread_condattr_t *attr);
00321 #endif /* HAVE_NONPOSIX_PTHREAD_MUTEX_INIT */
00322 
00323 #if defined(HAVE_SIGTHREADMASK) && !defined(HAVE_PTHREAD_SIGMASK)
00324 #define pthread_sigmask(A,B,C) sigthreadmask((A),(B),(C))
00325 #endif
00326 
00327 #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)
00328 int sigwait(sigset_t *setp, int *sigp);         /* Use our implemention */
00329 #endif
00330 #if !defined(HAVE_SIGSET) && !defined(HAVE_mit_thread) && !defined(sigset)
00331 #define sigset(A,B) do { struct sigaction s; sigset_t set;              \
00332                          sigemptyset(&set);                             \
00333                          s.sa_handler = (B);                            \
00334                          s.sa_mask    = set;                            \
00335                          s.sa_flags   = 0;                              \
00336                          sigaction((A), &s, (struct sigaction *) NULL); \
00337                        } while (0)
00338 #endif
00339 
00340 #ifndef my_pthread_setprio
00341 #if defined(HAVE_PTHREAD_SETPRIO_NP)            /* FSU threads */
00342 #define my_pthread_setprio(A,B) pthread_setprio_np((A),(B))
00343 #elif defined(HAVE_PTHREAD_SETPRIO)
00344 #define my_pthread_setprio(A,B) pthread_setprio((A),(B))
00345 #else
00346 extern void my_pthread_setprio(pthread_t thread_id,int prior);
00347 #endif
00348 #endif
00349 
00350 #ifndef my_pthread_attr_setprio
00351 #ifdef HAVE_PTHREAD_ATTR_SETPRIO
00352 #define my_pthread_attr_setprio(A,B) pthread_attr_setprio((A),(B))
00353 #else
00354 extern void my_pthread_attr_setprio(pthread_attr_t *attr, int priority);
00355 #endif
00356 #endif
00357 
00358 #if !defined(HAVE_PTHREAD_ATTR_SETSCOPE) || defined(HAVE_DEC_3_2_THREADS)
00359 #define pthread_attr_setscope(A,B)
00360 #undef  HAVE_GETHOSTBYADDR_R                    /* No definition */
00361 #endif
00362 
00363 #if defined(HAVE_BROKEN_PTHREAD_COND_TIMEDWAIT) && !defined(SAFE_MUTEX)
00364 extern int my_pthread_cond_timedwait(pthread_cond_t *cond,
00365                                      pthread_mutex_t *mutex,
00366                                      struct timespec *abstime);
00367 #define pthread_cond_timedwait(A,B,C) my_pthread_cond_timedwait((A),(B),(C))
00368 #endif
00369 
00370 #if defined(OS2)
00371 #define my_pthread_getspecific(T,A) ((T) &(A))
00372 #define pthread_setspecific(A,B) win_pthread_setspecific(&(A),(B),sizeof(A))
00373 #elif !defined( HAVE_NONPOSIX_PTHREAD_GETSPECIFIC)
00374 #define my_pthread_getspecific(A,B) ((A) pthread_getspecific(B))
00375 #else
00376 #define my_pthread_getspecific(A,B) ((A) my_pthread_getspecific_imp(B))
00377 void *my_pthread_getspecific_imp(pthread_key_t key);
00378 #endif /* OS2 */
00379 
00380 #ifndef HAVE_LOCALTIME_R
00381 struct tm *localtime_r(const time_t *clock, struct tm *res);
00382 #endif
00383 
00384 #ifndef HAVE_GMTIME_R
00385 struct tm *gmtime_r(const time_t *clock, struct tm *res);
00386 #endif
00387 
00388 #ifdef HAVE_PTHREAD_CONDATTR_CREATE
00389 /* DCE threads on HPUX 10.20 */
00390 #define pthread_condattr_init pthread_condattr_create
00391 #define pthread_condattr_destroy pthread_condattr_delete
00392 #endif
00393 
00394 /* FSU THREADS */
00395 #if !defined(HAVE_PTHREAD_KEY_DELETE) && !defined(pthread_key_delete)
00396 #define pthread_key_delete(A) pthread_dummy(0)
00397 #endif
00398 
00399 #ifdef HAVE_CTHREADS_WRAPPER                    /* For MacOSX */
00400 #define pthread_cond_destroy(A) pthread_dummy(0)
00401 #define pthread_mutex_destroy(A) pthread_dummy(0)
00402 #define pthread_attr_delete(A) pthread_dummy(0)
00403 #define pthread_condattr_delete(A) pthread_dummy(0)
00404 #define pthread_attr_setstacksize(A,B) pthread_dummy(0)
00405 #define pthread_equal(A,B) ((A) == (B))
00406 #define pthread_cond_timedwait(a,b,c) pthread_cond_wait((a),(b))
00407 #define pthread_attr_init(A) pthread_attr_create(A)
00408 #define pthread_attr_destroy(A) pthread_attr_delete(A)
00409 #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
00410 #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D))
00411 #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
00412 #define pthread_kill(A,B) pthread_dummy(0)
00413 #undef  pthread_detach_this_thread
00414 #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); }
00415 #endif
00416 
00417 #ifdef HAVE_DARWIN_THREADS
00418 #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
00419 #define pthread_kill(A,B) pthread_dummy(0)
00420 #define pthread_condattr_init(A) pthread_dummy(0)
00421 #define pthread_condattr_destroy(A) pthread_dummy(0)
00422 #define pthread_signal(A,B) pthread_dummy(0)
00423 #undef  pthread_detach_this_thread
00424 #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(tmp); }
00425 #undef sigset
00426 #define sigset(A,B) pthread_signal((A),(void (*)(int)) (B))
00427 #endif
00428 
00429 #if ((defined(HAVE_PTHREAD_ATTR_CREATE) && !defined(HAVE_SIGWAIT)) || defined(HAVE_DEC_3_2_THREADS)) && !defined(HAVE_CTHREADS_WRAPPER)
00430 /* This is set on AIX_3_2 and Siemens unix (and DEC OSF/1 3.2 too) */
00431 #define pthread_key_create(A,B) \
00432                 pthread_keycreate(A,(B) ?\
00433                                   (pthread_destructor_t) (B) :\
00434                                   (pthread_destructor_t) pthread_dummy)
00435 #define pthread_attr_init(A) pthread_attr_create(A)
00436 #define pthread_attr_destroy(A) pthread_attr_delete(A)
00437 #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
00438 #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D))
00439 #ifndef pthread_sigmask
00440 #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
00441 #endif
00442 #define pthread_kill(A,B) pthread_dummy(0)
00443 #undef  pthread_detach_this_thread
00444 #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); }
00445 #elif !defined(__NETWARE__) /* HAVE_PTHREAD_ATTR_CREATE && !HAVE_SIGWAIT */
00446 #define HAVE_PTHREAD_KILL
00447 #endif
00448 
00449 #endif /* defined(__WIN__) */
00450 
00451 #if defined(HPUX10) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS)
00452 #undef pthread_cond_timedwait
00453 #define pthread_cond_timedwait(a,b,c) my_pthread_cond_timedwait((a),(b),(c))
00454 int my_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
00455                               struct timespec *abstime);
00456 #endif
00457 
00458 #if defined(HPUX10)
00459 #define pthread_attr_getstacksize(A,B) my_pthread_attr_getstacksize(A,B)
00460 void my_pthread_attr_getstacksize(pthread_attr_t *attrib, size_t *size);
00461 #endif
00462 
00463 #if defined(HAVE_POSIX1003_4a_MUTEX) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS)
00464 #undef pthread_mutex_trylock
00465 #define pthread_mutex_trylock(a) my_pthread_mutex_trylock((a))
00466 int my_pthread_mutex_trylock(pthread_mutex_t *mutex);
00467 #endif
00468 
00469         /* safe_mutex adds checking to mutex for easier debugging */
00470 
00471 #if defined(__NETWARE__) && !defined(SAFE_MUTEX_DETECT_DESTROY)
00472 #define SAFE_MUTEX_DETECT_DESTROY
00473 #endif
00474 
00475 typedef struct st_safe_mutex_t
00476 {
00477   pthread_mutex_t global,mutex;
00478   const char *file;
00479   uint line,count;
00480   pthread_t thread;
00481 #ifdef SAFE_MUTEX_DETECT_DESTROY
00482   struct st_safe_mutex_info_t *info;    /* to track destroying of mutexes */
00483 #endif
00484 } safe_mutex_t;
00485 
00486 #ifdef SAFE_MUTEX_DETECT_DESTROY
00487 /*
00488   Used to track the destroying of mutexes. This needs to be a seperate
00489   structure because the safe_mutex_t structure could be freed before
00490   the mutexes are destroyed.
00491 */
00492 
00493 typedef struct st_safe_mutex_info_t
00494 {
00495   struct st_safe_mutex_info_t *next;
00496   struct st_safe_mutex_info_t *prev;
00497   const char *init_file;
00498   uint32 init_line;
00499 } safe_mutex_info_t;
00500 #endif /* SAFE_MUTEX_DETECT_DESTROY */
00501 
00502 int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr,
00503                     const char *file, uint line);
00504 int safe_mutex_lock(safe_mutex_t *mp,const char *file, uint line);
00505 int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line);
00506 int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint line);
00507 int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
00508                    uint line);
00509 int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
00510                         struct timespec *abstime, const char *file, uint line);
00511 void safe_mutex_global_init(void);
00512 void safe_mutex_end(FILE *file);
00513 
00514         /* Wrappers if safe mutex is actually used */
00515 #ifdef SAFE_MUTEX
00516 #undef pthread_mutex_init
00517 #undef pthread_mutex_lock
00518 #undef pthread_mutex_unlock
00519 #undef pthread_mutex_destroy
00520 #undef pthread_mutex_wait
00521 #undef pthread_mutex_timedwait
00522 #undef pthread_mutex_t
00523 #undef pthread_cond_wait
00524 #undef pthread_cond_timedwait
00525 #undef pthread_mutex_trylock
00526 #define pthread_mutex_init(A,B) safe_mutex_init((A),(B),__FILE__,__LINE__)
00527 #define pthread_mutex_lock(A) safe_mutex_lock((A),__FILE__,__LINE__)
00528 #define pthread_mutex_unlock(A) safe_mutex_unlock((A),__FILE__,__LINE__)
00529 #define pthread_mutex_destroy(A) safe_mutex_destroy((A),__FILE__,__LINE__)
00530 #define pthread_cond_wait(A,B) safe_cond_wait((A),(B),__FILE__,__LINE__)
00531 #define pthread_cond_timedwait(A,B,C) safe_cond_timedwait((A),(B),(C),__FILE__,__LINE__)
00532 #define pthread_mutex_trylock(A) pthread_mutex_lock(A)
00533 #define pthread_mutex_t safe_mutex_t
00534 #define safe_mutex_assert_owner(mp) DBUG_ASSERT((mp)->count > 0 && pthread_equal(pthread_self(),(mp)->thread))
00535 #else
00536 #define safe_mutex_assert_owner(mp)
00537 #endif /* SAFE_MUTEX */
00538 
00539         /* READ-WRITE thread locking */
00540 
00541 #ifdef HAVE_BROKEN_RWLOCK                       /* For OpenUnix */
00542 #undef HAVE_PTHREAD_RWLOCK_RDLOCK
00543 #undef HAVE_RWLOCK_INIT
00544 #undef HAVE_RWLOCK_T
00545 #endif
00546 
00547 #if defined(USE_MUTEX_INSTEAD_OF_RW_LOCKS)
00548 /* use these defs for simple mutex locking */
00549 #define rw_lock_t pthread_mutex_t
00550 #define my_rwlock_init(A,B) pthread_mutex_init((A),(B))
00551 #define rw_rdlock(A) pthread_mutex_lock((A))
00552 #define rw_wrlock(A) pthread_mutex_lock((A))
00553 #define rw_tryrdlock(A) pthread_mutex_trylock((A))
00554 #define rw_trywrlock(A) pthread_mutex_trylock((A))
00555 #define rw_unlock(A) pthread_mutex_unlock((A))
00556 #define rwlock_destroy(A) pthread_mutex_destroy((A))
00557 #elif defined(HAVE_PTHREAD_RWLOCK_RDLOCK)
00558 #define rw_lock_t pthread_rwlock_t
00559 #define my_rwlock_init(A,B) pthread_rwlock_init((A),(B))
00560 #define rw_rdlock(A) pthread_rwlock_rdlock(A)
00561 #define rw_wrlock(A) pthread_rwlock_wrlock(A)
00562 #define rw_tryrdlock(A) pthread_rwlock_tryrdlock((A))
00563 #define rw_trywrlock(A) pthread_rwlock_trywrlock((A))
00564 #define rw_unlock(A) pthread_rwlock_unlock(A)
00565 #define rwlock_destroy(A) pthread_rwlock_destroy(A)
00566 #elif defined(HAVE_RWLOCK_INIT)
00567 #ifdef HAVE_RWLOCK_T                            /* For example Solaris 2.6-> */
00568 #define rw_lock_t rwlock_t
00569 #endif
00570 #define my_rwlock_init(A,B) rwlock_init((A),USYNC_THREAD,0)
00571 #else
00572 /* Use our own version of read/write locks */
00573 typedef struct _my_rw_lock_t {
00574         pthread_mutex_t lock;           /* lock for structure           */
00575         pthread_cond_t  readers;        /* waiting readers              */
00576         pthread_cond_t  writers;        /* waiting writers              */
00577         int             state;          /* -1:writer,0:free,>0:readers  */
00578         int             waiters;        /* number of waiting writers    */
00579 } my_rw_lock_t;
00580 
00581 #define rw_lock_t my_rw_lock_t
00582 #define rw_rdlock(A) my_rw_rdlock((A))
00583 #define rw_wrlock(A) my_rw_wrlock((A))
00584 #define rw_tryrdlock(A) my_rw_tryrdlock((A))
00585 #define rw_trywrlock(A) my_rw_trywrlock((A))
00586 #define rw_unlock(A) my_rw_unlock((A))
00587 #define rwlock_destroy(A) my_rwlock_destroy((A))
00588 
00589 extern int my_rwlock_init(my_rw_lock_t *, void *);
00590 extern int my_rwlock_destroy(my_rw_lock_t *);
00591 extern int my_rw_rdlock(my_rw_lock_t *);
00592 extern int my_rw_wrlock(my_rw_lock_t *);
00593 extern int my_rw_unlock(my_rw_lock_t *);
00594 extern int my_rw_tryrdlock(my_rw_lock_t *);
00595 extern int my_rw_trywrlock(my_rw_lock_t *);
00596 #endif /* USE_MUTEX_INSTEAD_OF_RW_LOCKS */
00597 
00598 #define GETHOSTBYADDR_BUFF_SIZE 2048
00599 
00600 #ifndef HAVE_THR_SETCONCURRENCY
00601 #define thr_setconcurrency(A) pthread_dummy(0)
00602 #endif
00603 #if !defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && ! defined(pthread_attr_setstacksize)
00604 #define pthread_attr_setstacksize(A,B) pthread_dummy(0)
00605 #endif
00606 
00607 /* Define mutex types, see my_thr_init.c */
00608 #define MY_MUTEX_INIT_SLOW   NULL
00609 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
00610 extern pthread_mutexattr_t my_fast_mutexattr;
00611 #define MY_MUTEX_INIT_FAST &my_fast_mutexattr
00612 #else
00613 #define MY_MUTEX_INIT_FAST   NULL
00614 #endif
00615 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
00616 extern pthread_mutexattr_t my_errorcheck_mutexattr;
00617 #define MY_MUTEX_INIT_ERRCHK &my_errorcheck_mutexattr
00618 #else
00619 #define MY_MUTEX_INIT_ERRCHK   NULL
00620 #endif
00621 
00622 extern my_bool my_thread_global_init(void);
00623 extern void my_thread_global_end(void);
00624 extern my_bool my_thread_init(void);
00625 extern void my_thread_end(void);
00626 extern const char *my_thread_name(void);
00627 extern long my_thread_id(void);
00628 extern int pthread_no_free(void *);
00629 extern int pthread_dummy(int);
00630 
00631 /* All thread specific variables are in the following struct */
00632 
00633 #define THREAD_NAME_SIZE 10
00634 #ifndef DEFAULT_THREAD_STACK
00635 #if defined(__ia64__)
00636 /*
00637   MySQL can survive with 32K, but some glibc libraries require > 128K stack
00638   To resolve hostnames
00639 */
00640 #define DEFAULT_THREAD_STACK    (256*1024L)
00641 #else
00642 #define DEFAULT_THREAD_STACK    (192*1024)
00643 #endif
00644 #endif
00645 
00646 struct st_my_thread_var
00647 {
00648   int thr_errno;
00649   pthread_cond_t suspend;
00650   pthread_mutex_t mutex;
00651   pthread_mutex_t * volatile current_mutex;
00652   pthread_cond_t * volatile current_cond;
00653   pthread_t pthread_self;
00654   long id;
00655   int cmp_length;
00656   int volatile abort;
00657   my_bool init;
00658   struct st_my_thread_var *next,**prev;
00659   void *opt_info;
00660 #ifndef DBUG_OFF
00661   gptr dbug;
00662   char name[THREAD_NAME_SIZE+1];
00663 #endif
00664 };
00665 
00666 extern struct st_my_thread_var *_my_thread_var(void) __attribute__ ((const));
00667 #define my_thread_var (_my_thread_var())
00668 #define my_errno my_thread_var->thr_errno
00669 /*
00670   Keep track of shutdown,signal, and main threads so that my_end() will not
00671   report errors with them
00672 */
00673 extern pthread_t shutdown_th, main_th, signal_th;
00674 
00675         /* statistics_xxx functions are for not essential statistic */
00676 
00677 #ifndef thread_safe_increment
00678 #ifdef HAVE_ATOMIC_ADD
00679 #define thread_safe_increment(V,L) atomic_add(1,(atomic_t*) &V);
00680 #define thread_safe_add(V,C,L)     atomic_add((C),(atomic_t*) &V);
00681 #define thread_safe_sub(V,C,L)     atomic_sub((C),(atomic_t*) &V);
00682 #else
00683 #define thread_safe_increment(V,L) \
00684         pthread_mutex_lock((L)); (V)++; pthread_mutex_unlock((L));
00685 #define thread_safe_add(V,C,L) \
00686         pthread_mutex_lock((L)); (V)+=(C); pthread_mutex_unlock((L));
00687 #define thread_safe_sub(V,C,L) \
00688         pthread_mutex_lock((L)); (V)-=(C); pthread_mutex_unlock((L));
00689 #endif /* HAVE_ATOMIC_ADD */
00690 #ifdef SAFE_STATISTICS
00691 #define statistic_increment(V,L)   thread_safe_increment((V),(L))
00692 #define statistic_add(V,C,L)       thread_safe_add((V),(C),(L))
00693 #else
00694 #define statistic_increment(V,L) (V)++
00695 #define statistic_add(V,C,L)     (V)+=(C)
00696 #endif /* SAFE_STATISTICS */
00697 #endif /* thread_safe_increment */
00698 
00699 #ifdef  __cplusplus
00700 }
00701 #endif
00702 #endif /* _my_ptread_h */

Generated on Sun May 14 14:51:19 2006 by  doxygen 1.4.2