OSDN Git Service

PR c/20187
[pf3gnuchains/gcc-fork.git] / gcc / gthr-solaris.h
index 33ddf7a..e37c628 100644 (file)
@@ -1,6 +1,6 @@
 /* Threads compatibility routines for libgcc2 and libobjc.  */
 /* Compile this one with gcc.  */
-/* Copyright (C) 1997, 1999, 2000 Free Software Foundation, Inc.
+/* Copyright (C) 1997, 1999, 2000, 2004 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -16,8 +16,8 @@ for more details.
 
 You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING.  If not, write to the Free
-Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.  */
+Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301, USA.  */
 
 /* As a special exception, if you link this library with other files,
    some of which are compiled with GCC, to produce an executable,
@@ -45,8 +45,15 @@ typedef struct {
 } __gthread_once_t;
 typedef mutex_t __gthread_mutex_t;
 
+typedef struct {
+  long depth;
+  thread_t owner;
+  mutex_t actual;
+} __gthread_recursive_mutex_t;
+
 #define __GTHREAD_ONCE_INIT { DEFAULTMUTEX, 0 }
 #define __GTHREAD_MUTEX_INIT DEFAULTMUTEX
+#define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
 
 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
 
@@ -111,7 +118,7 @@ static void *thread_local_storage = NULL;
 static inline int
 __gthread_objc_init_thread_system (void)
 {
-  /* Initialize the thread storage key */
+  /* Initialize the thread storage key */
   if (__gthread_active_p ()
       && thr_keycreate (&_objc_thread_storage, NULL) == 0)
     return 0;
@@ -409,8 +416,8 @@ __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
 {
   /* Solaris 2.5 contains thr_* routines no-op in libc, so test if we actually
      got a reasonable key value, and if not, fail.  */
-  *key = -1;
-  if (thr_keycreate (key, dtor) != 0 || *key == -1)
+  *key = (__gthread_key_t)-1;
+  if (thr_keycreate (key, dtor) != 0 || *key == (__gthread_key_t)-1)
     return -1;
   else
     return 0;
@@ -466,6 +473,65 @@ __gthread_mutex_unlock (__gthread_mutex_t *mutex)
     return 0;
 }
 
+static inline int
+__gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
+{
+  mutex->depth = 0;
+  mutex->owner = (thread_t) 0;
+  return mutex_init (&mutex->actual, USYNC_THREAD, 0);
+}
+
+static inline int
+__gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
+{
+  if (__gthread_active_p ())
+    {
+      thread_t me = thr_self ();
+
+      if (mutex->owner != me)
+       {
+         mutex_lock (&mutex->actual);
+         mutex->owner = me;
+       }
+
+      mutex->depth++;
+    }
+  return 0;
+}
+
+static inline int
+__gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
+{
+  if (__gthread_active_p ())
+    {
+      thread_t me = thr_self ();
+
+      if (mutex->owner != me)
+       {
+         if (mutex_trylock (&mutex->actual))
+           return 1;
+         mutex->owner = me;
+       }
+
+      mutex->depth++;
+    }
+  return 0;
+}
+
+static inline int
+__gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
+{
+  if (__gthread_active_p ())
+    {
+      if (--mutex->depth == 0)
+       {
+          mutex->owner = (thread_t) 0;
+          mutex_unlock (&mutex->actual);
+       }
+    }
+  return 0;
+}
+
 #endif /* _LIBOBJC */
 
 #endif /* ! GCC_GTHR_SOLARIS_H */