develooper Front page | perl.perl5.porters | Postings from March 2003

[PATCH 5.8.1 @19053] thread.xs goofs

Thread Next
From:
Ilya Zakharevich
Date:
March 31, 2003 12:46
Subject:
[PATCH 5.8.1 @19053] thread.xs goofs
Message ID:
20030331204629.GA3672@math.berkeley.edu
ext/threads/threads.xs	-	was locking a mutex for a very long time;
				a Perl_croak() during this call would leave
				mutex locked.  Since the same mutex is used
				during exit() processing [Perl_ithread_hook()],
				Perl would not exit() in such a situation.

				I do not provide a proper fix (a mutex which
				guards only a linked list and a couple of 
				counters, nothing more), but just a workaround.

Thanks,
Ilya

--- ./ext/threads/threads.xs-pre	Sat Feb  1 14:28:04 2003
+++ ./ext/threads/threads.xs	Tue Mar 25 15:41:06 2003
@@ -78,7 +82,9 @@ ithread *threads;
 #define ithread_tid(thread)		((thread)->tid)
 #define ithread_yield(thread)		(YIELD);
 
-static perl_mutex create_destruct_mutex;  /* protects the creation and destruction of threads*/
+/* *Should* protect thread->next/prev, threads, known_threads, active_threads */
+static perl_mutex create_destruct_mutex;
+static int create_aborted = 0;
 
 I32 tid_counter = 0;
 I32 known_threads = 0;
@@ -154,7 +160,8 @@ int
 Perl_ithread_hook(pTHX)
 {
     int veto_cleanup = 0;
-    MUTEX_LOCK(&create_destruct_mutex);
+    if (!create_aborted)
+	MUTEX_LOCK(&create_destruct_mutex);
     if (aTHX == PL_curinterp && active_threads != 1) {
 	Perl_warn(aTHX_ "A thread exited while %" IVdf " other threads were still running",
 						(IV)active_threads);
@@ -372,8 +379,9 @@ Perl_ithread_create(pTHX_ SV *obj, char*
 	SV**            tmps_tmp = PL_tmps_stack;
 	I32             tmps_ix  = PL_tmps_ix;
 
-	PERL_THREAD_GETSPECIFIC(self_key,current_thread);
 	MUTEX_LOCK(&create_destruct_mutex);
+	create_aborted = 1;
+	PERL_THREAD_GETSPECIFIC(self_key,current_thread);
 	thread = PerlMemShared_malloc(sizeof(ithread));
 	Zero(thread,1,ithread);
 	thread->next = threads;
@@ -496,6 +504,7 @@ Perl_ithread_create(pTHX_ SV *obj, char*
 #endif
 	known_threads++;
 	active_threads++;
+	create_aborted = 0;
 	MUTEX_UNLOCK(&create_destruct_mutex);
 	sv_2mortal(params);
 

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About