Bug in Objective C runtime

Mark M._Kettenis kettenis@phys.uva.nl
Thu Nov 13 07:46:00 GMT 1997


Hi all,

I found a bug in Objective C runtime, if it is configured to use POSIX
compliant threads.  It manifests itself if the `pthread_mutex_t' and
`pthread_cond_t' types are more than 4 bytes long.  This is the case
with Linuxthreads 0.6 as included with recent glibc snapshots.  The
following patch corrects the problems and should work with all POSIX
threads implementations.

Mark Kettenis

PS I suspect that there are similar problems in some of the other
backends too.


--- egcs-971023/gcc/objc/thr-posix.c~	Mon Aug 11 17:58:12 1997
+++ egcs-971023/gcc/objc/thr-posix.c	Sun Nov  2 19:55:29 1997
@@ -129,41 +129,48 @@
 int
 __objc_mutex_allocate(objc_mutex_t mutex)
 {
-  if (pthread_mutex_init((pthread_mutex_t *)(&(mutex->backend)), NULL))
-    return -1;
-  else
-    return 0;
+  mutex->backend = objc_malloc(sizeof (pthread_mutex_t));
+  
+  if (pthread_mutex_init((pthread_mutex_t *)mutex->backend, NULL))
+    {
+      objc_free(mutex->backend);
+      return -1;
+    }
+
+  return 0;
 }
 
 /* Deallocate a mutex. */
 int
 __objc_mutex_deallocate(objc_mutex_t mutex)
 {
-  if (pthread_mutex_destroy((pthread_mutex_t *)(&(mutex->backend))))
+  if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
     return -1;
-  else
-    return 0;
+
+  objc_free(mutex->backend);
+  mutex->backend = NULL;
+  return 0;
 }
 
 /* Grab a lock on a mutex. */
 int
 __objc_mutex_lock(objc_mutex_t mutex)
 {
-  return pthread_mutex_lock((pthread_mutex_t *)(&(mutex->backend)));
+  return pthread_mutex_lock((pthread_mutex_t *)mutex->backend);
 }
 
 /* Try to grab a lock on a mutex. */
 int
 __objc_mutex_trylock(objc_mutex_t mutex)
 {
-  return pthread_mutex_trylock((pthread_mutex_t *)(&(mutex->backend)));
+  return pthread_mutex_trylock((pthread_mutex_t *)mutex->backend);
 }
 
 /* Unlock the mutex */
 int
 __objc_mutex_unlock(objc_mutex_t mutex)
 {
-  return pthread_mutex_unlock((pthread_mutex_t *)(&(mutex->backend)));
+  return pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
 }
 
 /* Backend condition mutex functions */
@@ -172,25 +179,35 @@
 int
 __objc_condition_allocate(objc_condition_t condition)
 {
-  if (pthread_cond_init((pthread_cond_t *)(&(condition->backend)), NULL))
-    return -1;
-  else
-    return 0;
+  condition->backend = objc_malloc(sizeof(pthread_cond_t));
+  
+  if (pthread_cond_init((pthread_cond_t *)condition->backend, NULL))
+    {
+      objc_free (condition->backend);
+      return -1;
+    }
+  
+  return 0;
 }
 
 /* Deallocate a condition. */
 int
 __objc_condition_deallocate(objc_condition_t condition)
 {
-  return pthread_cond_destroy((pthread_cond_t *)(&(condition->backend)));
+  if (pthread_cond_destroy((pthread_cond_t *)condition->backend))
+    return -1;
+
+  objc_free(conditin->backend);
+  condition->backend = NULL;
+  return 0;
 }
 
 /* Wait on the condition */
 int
 __objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
 {
-  return pthread_cond_wait((pthread_cond_t *)(&(condition->backend)),
-			   (pthread_mutex_t *)(&(mutex->backend)));
+  return pthread_cond_wait((pthread_cond_t *)condition->backend,
+			   (pthread_mutex_t *)mutex->backend);
 }
 
 /* Wake up all threads waiting on this condition. */




More information about the Gcc-bugs mailing list