@@ -30,7 +30,10 @@ pthread_key(struct st_my_thread_var, THR_KEY_mysys);
3030#endif /* USE_TLS */
3131pthread_mutex_t THR_LOCK_malloc ,THR_LOCK_open ,
3232 THR_LOCK_lock ,THR_LOCK_isam ,THR_LOCK_myisam ,THR_LOCK_heap ,
33- THR_LOCK_net , THR_LOCK_charset ;
33+ THR_LOCK_net , THR_LOCK_charset , THR_LOCK_threads ;
34+ pthread_cond_t THR_COND_threads ;
35+ uint THR_thread_count = 0 ;
36+ uint my_thread_end_wait_time = 5 ;
3437#if !defined(HAVE_LOCALTIME_R ) || !defined(HAVE_GMTIME_R )
3538pthread_mutex_t LOCK_localtime_r ;
3639#endif
@@ -79,7 +82,7 @@ my_bool my_thread_global_init(void)
7982#endif
8083#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
8184 /*
82- Set mutex type to "errorcheck" a.k.a "adaptive"
85+ Set mutex type to "errorcheck"
8386 */
8487 pthread_mutexattr_init (& my_errorcheck_mutexattr );
8588 pthread_mutexattr_settype (& my_errorcheck_mutexattr ,
@@ -94,7 +97,7 @@ my_bool my_thread_global_init(void)
9497 pthread_mutex_init (& THR_LOCK_heap ,MY_MUTEX_INIT_FAST );
9598 pthread_mutex_init (& THR_LOCK_net ,MY_MUTEX_INIT_FAST );
9699 pthread_mutex_init (& THR_LOCK_charset ,MY_MUTEX_INIT_FAST );
97- #if defined( __WIN__ )
100+ #if defined( __WIN__ ) || defined( OS2 )
98101 win_pthread_init ();
99102#endif
100103#if !defined(HAVE_LOCALTIME_R ) || !defined(HAVE_GMTIME_R )
@@ -114,6 +117,25 @@ my_bool my_thread_global_init(void)
114117
115118void my_thread_global_end (void )
116119{
120+ struct timespec abstime ;
121+ set_timespec (abstime , my_thread_end_wait_time );
122+ my_bool all_threads_killed = 1 ;
123+
124+ pthread_mutex_lock (& THR_LOCK_threads );
125+ while (THR_thread_count )
126+ {
127+ int error = pthread_cond_timedwait (& THR_COND_threads , & THR_LOCK_threads ,
128+ & abstime );
129+ if (error == ETIMEDOUT || error == ETIME )
130+ {
131+ if (THR_thread_count )
132+ fprintf (stderr ,"error in my_thread_global_end(): %d threads didn't exit\n" ,
133+ THR_thread_count );
134+ all_threads_killed = 0 ;
135+ }
136+ }
137+ pthread_mutex_unlock (& THR_LOCK_threads );
138+
117139 pthread_key_delete (THR_KEY_mysys );
118140#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
119141 pthread_mutexattr_destroy (& my_fast_mutexattr );
@@ -129,6 +151,11 @@ void my_thread_global_end(void)
129151 pthread_mutex_destroy (& THR_LOCK_heap );
130152 pthread_mutex_destroy (& THR_LOCK_net );
131153 pthread_mutex_destroy (& THR_LOCK_charset );
154+ if (all_threads_killed )
155+ {
156+ pthread_mutex_destroy (& THR_LOCK_threads );
157+ pthread_cond_destroy (& THR_COND_threads );
158+ }
132159#if !defined(HAVE_LOCALTIME_R ) || !defined(HAVE_GMTIME_R )
133160 pthread_mutex_destroy (& LOCK_localtime_r );
134161#endif
@@ -154,9 +181,6 @@ my_bool my_thread_init(void)
154181#ifdef EXTRA_DEBUG_THREADS
155182 fprintf (stderr ,"my_thread_init(): thread_id=%ld\n" ,pthread_self ());
156183#endif
157- #if !defined(__WIN__ ) || defined(USE_TLS ) || ! defined(SAFE_MUTEX )
158- pthread_mutex_lock (& THR_LOCK_lock );
159- #endif
160184
161185#if !defined(__WIN__ ) || defined(USE_TLS )
162186 if (my_pthread_getspecific (struct st_my_thread_var * ,THR_KEY_mysys ))
@@ -174,26 +198,26 @@ my_bool my_thread_init(void)
174198 }
175199 pthread_setspecific (THR_KEY_mysys ,tmp );
176200
177- #else
201+ #else /* defined(__WIN__) && !(defined(USE_TLS) */
178202 /*
179203 Skip initialization if the thread specific variable is already initialized
180204 */
181205 if (THR_KEY_mysys .id )
182206 goto end ;
183207 tmp = & THR_KEY_mysys ;
184208#endif
185- tmp -> id = ++ thread_id ;
186209#if defined(__WIN__ ) && defined(EMBEDDED_LIBRARY )
187210 tmp -> thread_self = (pthread_t )getpid ();
188211#endif
189212 pthread_mutex_init (& tmp -> mutex ,MY_MUTEX_INIT_FAST );
190213 pthread_cond_init (& tmp -> suspend , NULL );
191214 tmp -> init = 1 ;
192215
216+ pthread_mutex_lock (& THR_LOCK_threads );
217+ tmp -> id = ++ thread_id ;
218+ ++ THR_thread_count ;
219+ pthread_mutex_unlock (& THR_LOCK_threads );
193220end :
194- #if !defined(__WIN__ ) || defined(USE_TLS ) || ! defined(SAFE_MUTEX )
195- pthread_mutex_unlock (& THR_LOCK_lock );
196- #endif
197221 return error ;
198222}
199223
@@ -232,6 +256,10 @@ void my_thread_end(void)
232256#if !defined(__WIN__ ) || defined(USE_TLS )
233257 pthread_setspecific (THR_KEY_mysys ,0 );
234258#endif
259+ pthread_mutex_lock (& THR_LOCK_threads );
260+ if (-- THR_thread_count == 0 )
261+ pthread_cond_signal (& THR_COND_threads );
262+ pthread_mutex_unlock (& THR_LOCK_threads );
235263}
236264
237265struct st_my_thread_var * _my_thread_var (void )
0 commit comments