objective-c mutex deallocation bug

Chris Ball cball@fmco.com
Wed May 26 17:31:00 GMT 1999


Problem Description:
	pthread_mutex_destroy() will fail or have undefined
	behaviour if called with a locked mutex.

	objc_mutex_deallocate() locks the mutex it is trying
	to release to establish ownership.  This lock is not
	released before calling __objc_mutex_deallocate() in
	thr-posix.c.

	Results:  All mutex deallocate/release calls fail 
		under linux when using the posix thread 
		library.


Versions Found to have Problem:
	gcc version egcs-2.90.29 980515 (egcs-1.0.3 release)
	to
	gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release)

	and probably many older releases.


Notes:
	The behaviour of pthread_mutex_destroy() was checked
	via man pages for solaris 2.6 and linux using the
	posix thread library version 0.7.


Solution:
	This patch to thr-posix.c fixes the problem.  This
	releases all locks on the mutex so that in the case
	of a recursive lock the pthread_mutex_destroy() call
	will be successful.


diff -c output:


*** thr-posix.c.new	Wed May 26 17:17:03 1999
--- thr-posix.c	Sat Dec  6 09:30:07 1997
***************
*** 145,164 ****
  int
  __objc_mutex_deallocate(objc_mutex_t mutex)
  {
- 	int count = 1;
- 
- 	/*
- 	 * Posix Threads specifically require that the thread be unlocked for
- 	 * pthread_mutex_destroy to work.
- 	 */
- 
- 	while ( count )
- 	{
- 		 if (( count = pthread_mutex_unlock((pthread_mutex_t *)mutex->backend))
- 			< 0 )
- 		  	return -1;
- 	}
- 
    if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
      return -1;
  
--- 145,150 ----



	Chris.



More information about the Gcc-bugs mailing list