You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

755 lines
25 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
25 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
24 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
Fixed compiler warnings Fixed compile-pentium64 scripts Fixed wrong estimate of update_with_key_prefix in sql-bench Merge bk-internal.mysql.com:/home/bk/mysql-5.1 into mysql.com:/home/my/mysql-5.1 Fixed unsafe define of uint4korr() Fixed that --extern works with mysql-test-run.pl Small trivial cleanups This also fixes a bug in counting number of rows that are updated when we have many simultanous queries Move all connection handling and command exectuion main loop from sql_parse.cc to sql_connection.cc Split handle_one_connection() into reusable sub functions. Split create_new_thread() into reusable sub functions. Added thread_scheduler; Preliminary interface code for future thread_handling code. Use 'my_thread_id' for internal thread id's Make thr_alarm_kill() to depend on thread_id instead of thread Make thr_abort_locks_for_thread() depend on thread_id instead of thread In store_globals(), set my_thread_var->id to be thd->thread_id. Use my_thread_var->id as basis for my_thread_name() The above changes makes the connection we have between THD and threads more soft. Added a lot of DBUG_PRINT() and DBUG_ASSERT() functions Fixed compiler warnings Fixed core dumps when running with --debug Removed setting of signal masks (was never used) Made event code call pthread_exit() (portability fix) Fixed that event code doesn't call DBUG_xxx functions before my_thread_init() is called. Made handling of thread_id and thd->variables.pseudo_thread_id uniform. Removed one common 'not freed memory' warning from mysqltest Fixed a couple of usage of not initialized warnings (unlikely cases) Suppress compiler warnings from bdb and (for the moment) warnings from ndb
19 years ago
26 years ago
Fixed compiler warnings Fixed compile-pentium64 scripts Fixed wrong estimate of update_with_key_prefix in sql-bench Merge bk-internal.mysql.com:/home/bk/mysql-5.1 into mysql.com:/home/my/mysql-5.1 Fixed unsafe define of uint4korr() Fixed that --extern works with mysql-test-run.pl Small trivial cleanups This also fixes a bug in counting number of rows that are updated when we have many simultanous queries Move all connection handling and command exectuion main loop from sql_parse.cc to sql_connection.cc Split handle_one_connection() into reusable sub functions. Split create_new_thread() into reusable sub functions. Added thread_scheduler; Preliminary interface code for future thread_handling code. Use 'my_thread_id' for internal thread id's Make thr_alarm_kill() to depend on thread_id instead of thread Make thr_abort_locks_for_thread() depend on thread_id instead of thread In store_globals(), set my_thread_var->id to be thd->thread_id. Use my_thread_var->id as basis for my_thread_name() The above changes makes the connection we have between THD and threads more soft. Added a lot of DBUG_PRINT() and DBUG_ASSERT() functions Fixed compiler warnings Fixed core dumps when running with --debug Removed setting of signal masks (was never used) Made event code call pthread_exit() (portability fix) Fixed that event code doesn't call DBUG_xxx functions before my_thread_init() is called. Made handling of thread_id and thd->variables.pseudo_thread_id uniform. Removed one common 'not freed memory' warning from mysqltest Fixed a couple of usage of not initialized warnings (unlikely cases) Suppress compiler warnings from bdb and (for the moment) warnings from ndb
19 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
Fixed compiler warnings Fixed compile-pentium64 scripts Fixed wrong estimate of update_with_key_prefix in sql-bench Merge bk-internal.mysql.com:/home/bk/mysql-5.1 into mysql.com:/home/my/mysql-5.1 Fixed unsafe define of uint4korr() Fixed that --extern works with mysql-test-run.pl Small trivial cleanups This also fixes a bug in counting number of rows that are updated when we have many simultanous queries Move all connection handling and command exectuion main loop from sql_parse.cc to sql_connection.cc Split handle_one_connection() into reusable sub functions. Split create_new_thread() into reusable sub functions. Added thread_scheduler; Preliminary interface code for future thread_handling code. Use 'my_thread_id' for internal thread id's Make thr_alarm_kill() to depend on thread_id instead of thread Make thr_abort_locks_for_thread() depend on thread_id instead of thread In store_globals(), set my_thread_var->id to be thd->thread_id. Use my_thread_var->id as basis for my_thread_name() The above changes makes the connection we have between THD and threads more soft. Added a lot of DBUG_PRINT() and DBUG_ASSERT() functions Fixed compiler warnings Fixed core dumps when running with --debug Removed setting of signal masks (was never used) Made event code call pthread_exit() (portability fix) Fixed that event code doesn't call DBUG_xxx functions before my_thread_init() is called. Made handling of thread_id and thd->variables.pseudo_thread_id uniform. Removed one common 'not freed memory' warning from mysqltest Fixed a couple of usage of not initialized warnings (unlikely cases) Suppress compiler warnings from bdb and (for the moment) warnings from ndb
19 years ago
26 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. /* Copyright (C) 2000 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /* Defines to make different thread packages compatible */
  13. #ifndef _my_pthread_h
  14. #define _my_pthread_h
  15. #ifndef ETIME
  16. #define ETIME ETIMEDOUT /* For FreeBSD */
  17. #endif
  18. #ifdef __cplusplus
  19. #define EXTERNC extern "C"
  20. extern "C" {
  21. #else
  22. #define EXTERNC
  23. #endif /* __cplusplus */
  24. #if defined(__WIN__)
  25. typedef CRITICAL_SECTION pthread_mutex_t;
  26. typedef DWORD pthread_t;
  27. typedef struct thread_attr {
  28. DWORD dwStackSize ;
  29. DWORD dwCreatingFlag ;
  30. int priority ;
  31. } pthread_attr_t ;
  32. typedef struct { int dummy; } pthread_condattr_t;
  33. /* Implementation of posix conditions */
  34. typedef struct st_pthread_link {
  35. DWORD thread_id;
  36. struct st_pthread_link *next;
  37. } pthread_link;
  38. typedef struct {
  39. uint32 waiting;
  40. CRITICAL_SECTION lock_waiting;
  41. enum {
  42. SIGNAL= 0,
  43. BROADCAST= 1,
  44. MAX_EVENTS= 2
  45. } EVENTS;
  46. HANDLE events[MAX_EVENTS];
  47. HANDLE broadcast_block_event;
  48. } pthread_cond_t;
  49. typedef int pthread_mutexattr_t;
  50. #define pthread_self() GetCurrentThreadId()
  51. #define pthread_handler_t EXTERNC void * __cdecl
  52. typedef void * (__cdecl *pthread_handler)(void *);
  53. /*
  54. Struct and macros to be used in combination with the
  55. windows implementation of pthread_cond_timedwait
  56. */
  57. /*
  58. Declare a union to make sure FILETIME is properly aligned
  59. so it can be used directly as a 64 bit value. The value
  60. stored is in 100ns units.
  61. */
  62. union ft64 {
  63. FILETIME ft;
  64. __int64 i64;
  65. };
  66. struct timespec {
  67. union ft64 tv;
  68. /* The max timeout value in millisecond for pthread_cond_timedwait */
  69. long max_timeout_msec;
  70. };
  71. #define set_timespec(ABSTIME,SEC) { \
  72. GetSystemTimeAsFileTime(&((ABSTIME).tv.ft)); \
  73. (ABSTIME).tv.i64+= (__int64)(SEC)*10000000; \
  74. (ABSTIME).max_timeout_msec= (long)((SEC)*1000); \
  75. }
  76. #define set_timespec_nsec(ABSTIME,NSEC) { \
  77. GetSystemTimeAsFileTime(&((ABSTIME).tv.ft)); \
  78. (ABSTIME).tv.i64+= (__int64)(NSEC)/100; \
  79. (ABSTIME).max_timeout_msec= (long)((NSEC)/1000000); \
  80. }
  81. int win_pthread_mutex_trylock(pthread_mutex_t *mutex);
  82. int pthread_create(pthread_t *,pthread_attr_t *,pthread_handler,void *);
  83. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
  84. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
  85. int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  86. struct timespec *abstime);
  87. int pthread_cond_signal(pthread_cond_t *cond);
  88. int pthread_cond_broadcast(pthread_cond_t *cond);
  89. int pthread_cond_destroy(pthread_cond_t *cond);
  90. int pthread_attr_init(pthread_attr_t *connect_att);
  91. int pthread_attr_setstacksize(pthread_attr_t *connect_att,DWORD stack);
  92. int pthread_attr_setprio(pthread_attr_t *connect_att,int priority);
  93. int pthread_attr_destroy(pthread_attr_t *connect_att);
  94. struct tm *localtime_r(const time_t *timep,struct tm *tmp);
  95. struct tm *gmtime_r(const time_t *timep,struct tm *tmp);
  96. void pthread_exit(void *a);
  97. int pthread_join(pthread_t thread, void **value_ptr);
  98. #define ETIMEDOUT 145 /* Win32 doesn't have this */
  99. #define HAVE_LOCALTIME_R 1
  100. #define _REENTRANT 1
  101. #define HAVE_PTHREAD_ATTR_SETSTACKSIZE 1
  102. #undef SAFE_MUTEX /* This will cause conflicts */
  103. #define pthread_key(T,V) DWORD V
  104. #define pthread_key_create(A,B) ((*A=TlsAlloc())==0xFFFFFFFF)
  105. #define pthread_key_delete(A) TlsFree(A)
  106. #define my_pthread_setspecific_ptr(T,V) (!TlsSetValue((T),(V)))
  107. #define pthread_setspecific(A,B) (!TlsSetValue((A),(B)))
  108. #define pthread_getspecific(A) (TlsGetValue(A))
  109. #define my_pthread_getspecific(T,A) ((T) TlsGetValue(A))
  110. #define my_pthread_getspecific_ptr(T,V) ((T) TlsGetValue(V))
  111. #define pthread_equal(A,B) ((A) == (B))
  112. #define pthread_mutex_init(A,B) (InitializeCriticalSection(A),0)
  113. #define pthread_mutex_lock(A) (EnterCriticalSection(A),0)
  114. #define pthread_mutex_trylock(A) win_pthread_mutex_trylock((A))
  115. #define pthread_mutex_unlock(A) LeaveCriticalSection(A)
  116. #define pthread_mutex_destroy(A) DeleteCriticalSection(A)
  117. #define my_pthread_setprio(A,B) SetThreadPriority(GetCurrentThread(), (B))
  118. #define pthread_kill(A,B) pthread_dummy((A) ? 0 : ESRCH)
  119. /* Dummy defines for easier code */
  120. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  121. #define my_pthread_attr_setprio(A,B) pthread_attr_setprio(A,B)
  122. #define pthread_attr_setscope(A,B)
  123. #define pthread_detach_this_thread()
  124. #define pthread_condattr_init(A)
  125. #define pthread_condattr_destroy(A)
  126. #define my_pthread_getprio(thread_id) pthread_dummy(0)
  127. #else /* Normal threads */
  128. #ifdef HAVE_rts_threads
  129. #define sigwait org_sigwait
  130. #include <signal.h>
  131. #undef sigwait
  132. #endif
  133. #include <pthread.h>
  134. #ifndef _REENTRANT
  135. #define _REENTRANT
  136. #endif
  137. #ifdef HAVE_THR_SETCONCURRENCY
  138. #include <thread.h> /* Probably solaris */
  139. #endif
  140. #ifdef HAVE_SCHED_H
  141. #include <sched.h>
  142. #endif
  143. #ifdef HAVE_SYNCH_H
  144. #include <synch.h>
  145. #endif
  146. #ifdef __NETWARE__
  147. void my_pthread_exit(void *status);
  148. #define pthread_exit(A) my_pthread_exit(A)
  149. #endif
  150. extern int my_pthread_getprio(pthread_t thread_id);
  151. #define pthread_key(T,V) pthread_key_t V
  152. #define my_pthread_getspecific_ptr(T,V) my_pthread_getspecific(T,(V))
  153. #define my_pthread_setspecific_ptr(T,V) pthread_setspecific(T,(void*) (V))
  154. #define pthread_detach_this_thread()
  155. #define pthread_handler_t EXTERNC void *
  156. typedef void *(* pthread_handler)(void *);
  157. /* Test first for RTS or FSU threads */
  158. #if defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM)
  159. #define HAVE_rts_threads
  160. extern int my_pthread_create_detached;
  161. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  162. #define PTHREAD_CREATE_DETACHED &my_pthread_create_detached
  163. #define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_GLOBAL
  164. #define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_LOCAL
  165. #define USE_ALARM_THREAD
  166. #endif /* defined(PTHREAD_SCOPE_GLOBAL) && !defined(PTHREAD_SCOPE_SYSTEM) */
  167. #if defined(_BSDI_VERSION) && _BSDI_VERSION < 199910
  168. int sigwait(sigset_t *set, int *sig);
  169. #endif
  170. #ifndef HAVE_NONPOSIX_SIGWAIT
  171. #define my_sigwait(A,B) sigwait((A),(B))
  172. #else
  173. int my_sigwait(const sigset_t *set,int *sig);
  174. #endif
  175. #ifdef HAVE_NONPOSIX_PTHREAD_MUTEX_INIT
  176. #ifndef SAFE_MUTEX
  177. #define pthread_mutex_init(a,b) my_pthread_mutex_init((a),(b))
  178. extern int my_pthread_mutex_init(pthread_mutex_t *mp,
  179. const pthread_mutexattr_t *attr);
  180. #endif /* SAFE_MUTEX */
  181. #define pthread_cond_init(a,b) my_pthread_cond_init((a),(b))
  182. extern int my_pthread_cond_init(pthread_cond_t *mp,
  183. const pthread_condattr_t *attr);
  184. #endif /* HAVE_NONPOSIX_PTHREAD_MUTEX_INIT */
  185. #if defined(HAVE_SIGTHREADMASK) && !defined(HAVE_PTHREAD_SIGMASK)
  186. #define pthread_sigmask(A,B,C) sigthreadmask((A),(B),(C))
  187. #endif
  188. #if !defined(HAVE_SIGWAIT) && !defined(HAVE_rts_threads) && !defined(sigwait) && !defined(alpha_linux_port) && !defined(HAVE_NONPOSIX_SIGWAIT) && !defined(HAVE_DEC_3_2_THREADS) && !defined(_AIX)
  189. int sigwait(sigset_t *setp, int *sigp); /* Use our implemention */
  190. #endif
  191. /*
  192. We define my_sigset() and use that instead of the system sigset() so that
  193. we can favor an implementation based on sigaction(). On some systems, such
  194. as Mac OS X, sigset() results in flags such as SA_RESTART being set, and
  195. we want to make sure that no such flags are set.
  196. */
  197. #if defined(HAVE_SIGACTION) && !defined(my_sigset)
  198. #define my_sigset(A,B) do { struct sigaction l_s; sigset_t l_set; int l_rc; \
  199. DBUG_ASSERT((A) != 0); \
  200. sigemptyset(&l_set); \
  201. l_s.sa_handler = (B); \
  202. l_s.sa_mask = l_set; \
  203. l_s.sa_flags = 0; \
  204. l_rc= sigaction((A), &l_s, (struct sigaction *) NULL);\
  205. DBUG_ASSERT(l_rc == 0); \
  206. } while (0)
  207. #elif defined(HAVE_SIGSET) && !defined(my_sigset)
  208. #define my_sigset(A,B) sigset((A),(B))
  209. #elif !defined(my_sigset)
  210. #define my_sigset(A,B) signal((A),(B))
  211. #endif
  212. #ifndef my_pthread_setprio
  213. #if defined(HAVE_PTHREAD_SETPRIO_NP) /* FSU threads */
  214. #define my_pthread_setprio(A,B) pthread_setprio_np((A),(B))
  215. #elif defined(HAVE_PTHREAD_SETPRIO)
  216. #define my_pthread_setprio(A,B) pthread_setprio((A),(B))
  217. #elif defined(HAVE_PTHREAD_SETSCHEDPRIO) && !defined (__GNUC__)
  218. /*
  219. Workaround for bug on Solaris where pthread.h have bug in GNU
  220. version of pthread.h => configure says yes, header files says
  221. no. So not used with gcc and issue is Solaris only, so will
  222. be used on Solaris using SunStudio.
  223. */
  224. #define my_pthread_setprio(A,B) pthread_setschedprio((A),(B))
  225. #else
  226. extern void my_pthread_setprio(pthread_t thread_id,int prior);
  227. #endif
  228. #endif
  229. #ifndef my_pthread_attr_setprio
  230. #ifdef HAVE_PTHREAD_ATTR_SETPRIO
  231. #define my_pthread_attr_setprio(A,B) pthread_attr_setprio((A),(B))
  232. #else
  233. extern void my_pthread_attr_setprio(pthread_attr_t *attr, int priority);
  234. #endif
  235. #endif
  236. #if !defined(HAVE_PTHREAD_ATTR_SETSCOPE) || defined(HAVE_DEC_3_2_THREADS)
  237. #define pthread_attr_setscope(A,B)
  238. #undef HAVE_GETHOSTBYADDR_R /* No definition */
  239. #endif
  240. #if defined(HAVE_BROKEN_PTHREAD_COND_TIMEDWAIT) && !defined(SAFE_MUTEX)
  241. extern int my_pthread_cond_timedwait(pthread_cond_t *cond,
  242. pthread_mutex_t *mutex,
  243. struct timespec *abstime);
  244. #define pthread_cond_timedwait(A,B,C) my_pthread_cond_timedwait((A),(B),(C))
  245. #endif
  246. #if !defined( HAVE_NONPOSIX_PTHREAD_GETSPECIFIC)
  247. #define my_pthread_getspecific(A,B) ((A) pthread_getspecific(B))
  248. #else
  249. #define my_pthread_getspecific(A,B) ((A) my_pthread_getspecific_imp(B))
  250. void *my_pthread_getspecific_imp(pthread_key_t key);
  251. #endif
  252. #ifndef HAVE_LOCALTIME_R
  253. struct tm *localtime_r(const time_t *clock, struct tm *res);
  254. #endif
  255. #ifndef HAVE_GMTIME_R
  256. struct tm *gmtime_r(const time_t *clock, struct tm *res);
  257. #endif
  258. #ifdef HAVE_PTHREAD_CONDATTR_CREATE
  259. /* DCE threads on HPUX 10.20 */
  260. #define pthread_condattr_init pthread_condattr_create
  261. #define pthread_condattr_destroy pthread_condattr_delete
  262. #endif
  263. /* FSU THREADS */
  264. #if !defined(HAVE_PTHREAD_KEY_DELETE) && !defined(pthread_key_delete)
  265. #define pthread_key_delete(A) pthread_dummy(0)
  266. #endif
  267. #ifdef HAVE_CTHREADS_WRAPPER /* For MacOSX */
  268. #define pthread_cond_destroy(A) pthread_dummy(0)
  269. #define pthread_mutex_destroy(A) pthread_dummy(0)
  270. #define pthread_attr_delete(A) pthread_dummy(0)
  271. #define pthread_condattr_delete(A) pthread_dummy(0)
  272. #define pthread_attr_setstacksize(A,B) pthread_dummy(0)
  273. #define pthread_equal(A,B) ((A) == (B))
  274. #define pthread_cond_timedwait(a,b,c) pthread_cond_wait((a),(b))
  275. #define pthread_attr_init(A) pthread_attr_create(A)
  276. #define pthread_attr_destroy(A) pthread_attr_delete(A)
  277. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  278. #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D))
  279. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  280. #define pthread_kill(A,B) pthread_dummy((A) ? 0 : ESRCH)
  281. #undef pthread_detach_this_thread
  282. #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); }
  283. #endif
  284. #ifdef HAVE_DARWIN5_THREADS
  285. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  286. #define pthread_kill(A,B) pthread_dummy((A) ? 0 : ESRCH)
  287. #define pthread_condattr_init(A) pthread_dummy(0)
  288. #define pthread_condattr_destroy(A) pthread_dummy(0)
  289. #undef pthread_detach_this_thread
  290. #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(tmp); }
  291. #endif
  292. #if ((defined(HAVE_PTHREAD_ATTR_CREATE) && !defined(HAVE_SIGWAIT)) || defined(HAVE_DEC_3_2_THREADS)) && !defined(HAVE_CTHREADS_WRAPPER)
  293. /* This is set on AIX_3_2 and Siemens unix (and DEC OSF/1 3.2 too) */
  294. #define pthread_key_create(A,B) \
  295. pthread_keycreate(A,(B) ?\
  296. (pthread_destructor_t) (B) :\
  297. (pthread_destructor_t) pthread_dummy)
  298. #define pthread_attr_init(A) pthread_attr_create(A)
  299. #define pthread_attr_destroy(A) pthread_attr_delete(A)
  300. #define pthread_attr_setdetachstate(A,B) pthread_dummy(0)
  301. #define pthread_create(A,B,C,D) pthread_create((A),*(B),(C),(D))
  302. #ifndef pthread_sigmask
  303. #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C))
  304. #endif
  305. #define pthread_kill(A,B) pthread_dummy((A) ? 0 : ESRCH)
  306. #undef pthread_detach_this_thread
  307. #define pthread_detach_this_thread() { pthread_t tmp=pthread_self() ; pthread_detach(&tmp); }
  308. #elif !defined(__NETWARE__) /* HAVE_PTHREAD_ATTR_CREATE && !HAVE_SIGWAIT */
  309. #define HAVE_PTHREAD_KILL
  310. #endif
  311. #endif /* defined(__WIN__) */
  312. #if defined(HPUX10) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS)
  313. #undef pthread_cond_timedwait
  314. #define pthread_cond_timedwait(a,b,c) my_pthread_cond_timedwait((a),(b),(c))
  315. int my_pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  316. struct timespec *abstime);
  317. #endif
  318. #if defined(HPUX10)
  319. #define pthread_attr_getstacksize(A,B) my_pthread_attr_getstacksize(A,B)
  320. void my_pthread_attr_getstacksize(pthread_attr_t *attrib, size_t *size);
  321. #endif
  322. #if defined(HAVE_POSIX1003_4a_MUTEX) && !defined(DONT_REMAP_PTHREAD_FUNCTIONS)
  323. #undef pthread_mutex_trylock
  324. #define pthread_mutex_trylock(a) my_pthread_mutex_trylock((a))
  325. int my_pthread_mutex_trylock(pthread_mutex_t *mutex);
  326. #endif
  327. /*
  328. The defines set_timespec and set_timespec_nsec should be used
  329. for calculating an absolute time at which
  330. pthread_cond_timedwait should timeout
  331. */
  332. #ifdef HAVE_TIMESPEC_TS_SEC
  333. #ifndef set_timespec
  334. #define set_timespec(ABSTIME,SEC) \
  335. { \
  336. (ABSTIME).ts_sec=time(0) + (time_t) (SEC); \
  337. (ABSTIME).ts_nsec=0; \
  338. }
  339. #endif /* !set_timespec */
  340. #ifndef set_timespec_nsec
  341. #define set_timespec_nsec(ABSTIME,NSEC) \
  342. { \
  343. ulonglong now= my_getsystime() + (NSEC/100); \
  344. (ABSTIME).ts_sec= (now / ULL(10000000)); \
  345. (ABSTIME).ts_nsec= (now % ULL(10000000) * 100 + ((NSEC) % 100)); \
  346. }
  347. #endif /* !set_timespec_nsec */
  348. #else
  349. #ifndef set_timespec
  350. #define set_timespec(ABSTIME,SEC) \
  351. {\
  352. struct timeval tv;\
  353. gettimeofday(&tv,0);\
  354. (ABSTIME).tv_sec=tv.tv_sec+(time_t) (SEC);\
  355. (ABSTIME).tv_nsec=tv.tv_usec*1000;\
  356. }
  357. #endif /* !set_timespec */
  358. #ifndef set_timespec_nsec
  359. #define set_timespec_nsec(ABSTIME,NSEC) \
  360. {\
  361. ulonglong now= my_getsystime() + (NSEC/100); \
  362. (ABSTIME).tv_sec= (time_t) (now / ULL(10000000)); \
  363. (ABSTIME).tv_nsec= (long) (now % ULL(10000000) * 100 + ((NSEC) % 100)); \
  364. }
  365. #endif /* !set_timespec_nsec */
  366. #endif /* HAVE_TIMESPEC_TS_SEC */
  367. /* safe_mutex adds checking to mutex for easier debugging */
  368. #if defined(__NETWARE__) && !defined(SAFE_MUTEX_DETECT_DESTROY)
  369. #define SAFE_MUTEX_DETECT_DESTROY
  370. #endif
  371. typedef struct st_safe_mutex_t
  372. {
  373. pthread_mutex_t global,mutex;
  374. const char *file;
  375. uint line,count;
  376. pthread_t thread;
  377. #ifdef SAFE_MUTEX_DETECT_DESTROY
  378. struct st_safe_mutex_info_t *info; /* to track destroying of mutexes */
  379. #endif
  380. } safe_mutex_t;
  381. #ifdef SAFE_MUTEX_DETECT_DESTROY
  382. /*
  383. Used to track the destroying of mutexes. This needs to be a seperate
  384. structure because the safe_mutex_t structure could be freed before
  385. the mutexes are destroyed.
  386. */
  387. typedef struct st_safe_mutex_info_t
  388. {
  389. struct st_safe_mutex_info_t *next;
  390. struct st_safe_mutex_info_t *prev;
  391. const char *init_file;
  392. uint32 init_line;
  393. } safe_mutex_info_t;
  394. #endif /* SAFE_MUTEX_DETECT_DESTROY */
  395. int safe_mutex_init(safe_mutex_t *mp, const pthread_mutexattr_t *attr,
  396. const char *file, uint line);
  397. int safe_mutex_lock(safe_mutex_t *mp, my_bool try_lock, const char *file, uint line);
  398. int safe_mutex_unlock(safe_mutex_t *mp,const char *file, uint line);
  399. int safe_mutex_destroy(safe_mutex_t *mp,const char *file, uint line);
  400. int safe_cond_wait(pthread_cond_t *cond, safe_mutex_t *mp,const char *file,
  401. uint line);
  402. int safe_cond_timedwait(pthread_cond_t *cond, safe_mutex_t *mp,
  403. struct timespec *abstime, const char *file, uint line);
  404. void safe_mutex_global_init(void);
  405. void safe_mutex_end(FILE *file);
  406. /* Wrappers if safe mutex is actually used */
  407. #ifdef SAFE_MUTEX
  408. #undef pthread_mutex_init
  409. #undef pthread_mutex_lock
  410. #undef pthread_mutex_unlock
  411. #undef pthread_mutex_destroy
  412. #undef pthread_mutex_wait
  413. #undef pthread_mutex_timedwait
  414. #undef pthread_mutex_t
  415. #undef pthread_cond_wait
  416. #undef pthread_cond_timedwait
  417. #undef pthread_mutex_trylock
  418. #define pthread_mutex_init(A,B) safe_mutex_init((A),(B),__FILE__,__LINE__)
  419. #define pthread_mutex_lock(A) safe_mutex_lock((A), FALSE, __FILE__, __LINE__)
  420. #define pthread_mutex_unlock(A) safe_mutex_unlock((A),__FILE__,__LINE__)
  421. #define pthread_mutex_destroy(A) safe_mutex_destroy((A),__FILE__,__LINE__)
  422. #define pthread_cond_wait(A,B) safe_cond_wait((A),(B),__FILE__,__LINE__)
  423. #define pthread_cond_timedwait(A,B,C) safe_cond_timedwait((A),(B),(C),__FILE__,__LINE__)
  424. #define pthread_mutex_trylock(A) safe_mutex_lock((A), TRUE, __FILE__, __LINE__)
  425. #define pthread_mutex_t safe_mutex_t
  426. #define safe_mutex_assert_owner(mp) \
  427. DBUG_ASSERT((mp)->count > 0 && \
  428. pthread_equal(pthread_self(), (mp)->thread))
  429. #define safe_mutex_assert_not_owner(mp) \
  430. DBUG_ASSERT(! (mp)->count || \
  431. ! pthread_equal(pthread_self(), (mp)->thread))
  432. #else
  433. #define safe_mutex_assert_owner(mp)
  434. #define safe_mutex_assert_not_owner(mp)
  435. #endif /* SAFE_MUTEX */
  436. #if defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
  437. typedef struct st_my_pthread_fastmutex_t
  438. {
  439. pthread_mutex_t mutex;
  440. uint spins;
  441. uint rng_state;
  442. } my_pthread_fastmutex_t;
  443. void fastmutex_global_init(void);
  444. int my_pthread_fastmutex_init(my_pthread_fastmutex_t *mp,
  445. const pthread_mutexattr_t *attr);
  446. int my_pthread_fastmutex_lock(my_pthread_fastmutex_t *mp);
  447. #undef pthread_mutex_init
  448. #undef pthread_mutex_lock
  449. #undef pthread_mutex_unlock
  450. #undef pthread_mutex_destroy
  451. #undef pthread_mutex_wait
  452. #undef pthread_mutex_timedwait
  453. #undef pthread_mutex_t
  454. #undef pthread_cond_wait
  455. #undef pthread_cond_timedwait
  456. #undef pthread_mutex_trylock
  457. #define pthread_mutex_init(A,B) my_pthread_fastmutex_init((A),(B))
  458. #define pthread_mutex_lock(A) my_pthread_fastmutex_lock(A)
  459. #define pthread_mutex_unlock(A) pthread_mutex_unlock(&(A)->mutex)
  460. #define pthread_mutex_destroy(A) pthread_mutex_destroy(&(A)->mutex)
  461. #define pthread_cond_wait(A,B) pthread_cond_wait((A),&(B)->mutex)
  462. #define pthread_cond_timedwait(A,B,C) pthread_cond_timedwait((A),&(B)->mutex,(C))
  463. #define pthread_mutex_trylock(A) pthread_mutex_trylock(&(A)->mutex)
  464. #define pthread_mutex_t my_pthread_fastmutex_t
  465. #endif /* defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX) */
  466. /* READ-WRITE thread locking */
  467. #ifdef HAVE_BROKEN_RWLOCK /* For OpenUnix */
  468. #undef HAVE_PTHREAD_RWLOCK_RDLOCK
  469. #undef HAVE_RWLOCK_INIT
  470. #undef HAVE_RWLOCK_T
  471. #endif
  472. #if defined(USE_MUTEX_INSTEAD_OF_RW_LOCKS)
  473. /* use these defs for simple mutex locking */
  474. #define rw_lock_t pthread_mutex_t
  475. #define my_rwlock_init(A,B) pthread_mutex_init((A),(B))
  476. #define rw_rdlock(A) pthread_mutex_lock((A))
  477. #define rw_wrlock(A) pthread_mutex_lock((A))
  478. #define rw_tryrdlock(A) pthread_mutex_trylock((A))
  479. #define rw_trywrlock(A) pthread_mutex_trylock((A))
  480. #define rw_unlock(A) pthread_mutex_unlock((A))
  481. #define rwlock_destroy(A) pthread_mutex_destroy((A))
  482. #elif defined(HAVE_PTHREAD_RWLOCK_RDLOCK)
  483. #define rw_lock_t pthread_rwlock_t
  484. #define my_rwlock_init(A,B) pthread_rwlock_init((A),(B))
  485. #define rw_rdlock(A) pthread_rwlock_rdlock(A)
  486. #define rw_wrlock(A) pthread_rwlock_wrlock(A)
  487. #define rw_tryrdlock(A) pthread_rwlock_tryrdlock((A))
  488. #define rw_trywrlock(A) pthread_rwlock_trywrlock((A))
  489. #define rw_unlock(A) pthread_rwlock_unlock(A)
  490. #define rwlock_destroy(A) pthread_rwlock_destroy(A)
  491. #elif defined(HAVE_RWLOCK_INIT)
  492. #ifdef HAVE_RWLOCK_T /* For example Solaris 2.6-> */
  493. #define rw_lock_t rwlock_t
  494. #endif
  495. #define my_rwlock_init(A,B) rwlock_init((A),USYNC_THREAD,0)
  496. #else
  497. /* Use our own version of read/write locks */
  498. typedef struct _my_rw_lock_t {
  499. pthread_mutex_t lock; /* lock for structure */
  500. pthread_cond_t readers; /* waiting readers */
  501. pthread_cond_t writers; /* waiting writers */
  502. int state; /* -1:writer,0:free,>0:readers */
  503. int waiters; /* number of waiting writers */
  504. } my_rw_lock_t;
  505. #define rw_lock_t my_rw_lock_t
  506. #define rw_rdlock(A) my_rw_rdlock((A))
  507. #define rw_wrlock(A) my_rw_wrlock((A))
  508. #define rw_tryrdlock(A) my_rw_tryrdlock((A))
  509. #define rw_trywrlock(A) my_rw_trywrlock((A))
  510. #define rw_unlock(A) my_rw_unlock((A))
  511. #define rwlock_destroy(A) my_rwlock_destroy((A))
  512. extern int my_rwlock_init(my_rw_lock_t *, void *);
  513. extern int my_rwlock_destroy(my_rw_lock_t *);
  514. extern int my_rw_rdlock(my_rw_lock_t *);
  515. extern int my_rw_wrlock(my_rw_lock_t *);
  516. extern int my_rw_unlock(my_rw_lock_t *);
  517. extern int my_rw_tryrdlock(my_rw_lock_t *);
  518. extern int my_rw_trywrlock(my_rw_lock_t *);
  519. #endif /* USE_MUTEX_INSTEAD_OF_RW_LOCKS */
  520. #define GETHOSTBYADDR_BUFF_SIZE 2048
  521. #ifndef HAVE_THR_SETCONCURRENCY
  522. #define thr_setconcurrency(A) pthread_dummy(0)
  523. #endif
  524. #if !defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) && ! defined(pthread_attr_setstacksize)
  525. #define pthread_attr_setstacksize(A,B) pthread_dummy(0)
  526. #endif
  527. /* Define mutex types, see my_thr_init.c */
  528. #define MY_MUTEX_INIT_SLOW NULL
  529. #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  530. extern pthread_mutexattr_t my_fast_mutexattr;
  531. #define MY_MUTEX_INIT_FAST &my_fast_mutexattr
  532. #else
  533. #define MY_MUTEX_INIT_FAST NULL
  534. #endif
  535. #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
  536. extern pthread_mutexattr_t my_errorcheck_mutexattr;
  537. #define MY_MUTEX_INIT_ERRCHK &my_errorcheck_mutexattr
  538. #else
  539. #define MY_MUTEX_INIT_ERRCHK NULL
  540. #endif
  541. #ifndef ESRCH
  542. /* Define it to something */
  543. #define ESRCH 1
  544. #endif
  545. typedef ulong my_thread_id;
  546. extern my_bool my_thread_global_init(void);
  547. extern void my_thread_global_end(void);
  548. extern my_bool my_thread_init(void);
  549. extern void my_thread_end(void);
  550. extern const char *my_thread_name(void);
  551. extern my_thread_id my_thread_dbug_id(void);
  552. extern int pthread_no_free(void *);
  553. extern int pthread_dummy(int);
  554. /* All thread specific variables are in the following struct */
  555. #define THREAD_NAME_SIZE 10
  556. #ifndef DEFAULT_THREAD_STACK
  557. #if SIZEOF_CHARP > 4
  558. /*
  559. MySQL can survive with 32K, but some glibc libraries require > 128K stack
  560. To resolve hostnames. Also recursive stored procedures needs stack.
  561. */
  562. #define DEFAULT_THREAD_STACK (256*1024L)
  563. #else
  564. #define DEFAULT_THREAD_STACK (192*1024)
  565. #endif
  566. #endif
  567. struct st_my_thread_var
  568. {
  569. int thr_errno;
  570. pthread_cond_t suspend;
  571. pthread_mutex_t mutex;
  572. pthread_mutex_t * volatile current_mutex;
  573. pthread_cond_t * volatile current_cond;
  574. pthread_t pthread_self;
  575. my_thread_id id;
  576. int cmp_length;
  577. int volatile abort;
  578. my_bool init;
  579. struct st_my_thread_var *next,**prev;
  580. void *opt_info;
  581. #ifndef DBUG_OFF
  582. void *dbug;
  583. char name[THREAD_NAME_SIZE+1];
  584. #endif
  585. };
  586. extern struct st_my_thread_var *_my_thread_var(void) __attribute__ ((const));
  587. extern uint my_thread_end_wait_time;
  588. #define my_thread_var (_my_thread_var())
  589. #define my_errno my_thread_var->thr_errno
  590. /*
  591. Keep track of shutdown,signal, and main threads so that my_end() will not
  592. report errors with them
  593. */
  594. /* Which kind of thread library is in use */
  595. #define THD_LIB_OTHER 1
  596. #define THD_LIB_NPTL 2
  597. #define THD_LIB_LT 4
  598. extern uint thd_lib_detected;
  599. /*
  600. thread_safe_xxx functions are for critical statistic or counters.
  601. The implementation is guaranteed to be thread safe, on all platforms.
  602. Note that the calling code should *not* assume the counter is protected
  603. by the mutex given, as the implementation of these helpers may change
  604. to use my_atomic operations instead.
  605. */
  606. /*
  607. Warning:
  608. When compiling without threads, this file is not included.
  609. See the *other* declarations of thread_safe_xxx in include/my_global.h
  610. Second warning:
  611. See include/config-win.h, for yet another implementation.
  612. */
  613. #ifdef THREAD
  614. #ifndef thread_safe_increment
  615. #define thread_safe_increment(V,L) \
  616. (pthread_mutex_lock((L)), (V)++, pthread_mutex_unlock((L)))
  617. #define thread_safe_decrement(V,L) \
  618. (pthread_mutex_lock((L)), (V)--, pthread_mutex_unlock((L)))
  619. #endif
  620. #ifndef thread_safe_add
  621. #define thread_safe_add(V,C,L) \
  622. (pthread_mutex_lock((L)), (V)+=(C), pthread_mutex_unlock((L)))
  623. #define thread_safe_sub(V,C,L) \
  624. (pthread_mutex_lock((L)), (V)-=(C), pthread_mutex_unlock((L)))
  625. #endif
  626. #endif
  627. /*
  628. statistics_xxx functions are for non critical statistic,
  629. maintained in global variables.
  630. When compiling with SAFE_STATISTICS:
  631. - race conditions can not occur.
  632. - some locking occurs, which may cause performance degradation.
  633. When compiling without SAFE_STATISTICS:
  634. - race conditions can occur, making the result slightly inaccurate.
  635. - the lock given is not honored.
  636. */
  637. #ifdef SAFE_STATISTICS
  638. #define statistic_increment(V,L) thread_safe_increment((V),(L))
  639. #define statistic_decrement(V,L) thread_safe_decrement((V),(L))
  640. #define statistic_add(V,C,L) thread_safe_add((V),(C),(L))
  641. #define statistic_sub(V,C,L) thread_safe_sub((V),(C),(L))
  642. #else
  643. #define statistic_decrement(V,L) (V)--
  644. #define statistic_increment(V,L) (V)++
  645. #define statistic_add(V,C,L) (V)+=(C)
  646. #define statistic_sub(V,C,L) (V)-=(C)
  647. #endif /* SAFE_STATISTICS */
  648. /*
  649. No locking needed, the counter is owned by the thread
  650. */
  651. #define status_var_increment(V) (V)++
  652. #define status_var_decrement(V) (V)--
  653. #define status_var_add(V,C) (V)+=(C)
  654. #define status_var_sub(V,C) (V)-=(C)
  655. #ifdef __cplusplus
  656. }
  657. #endif
  658. #endif /* _my_ptread_h */