This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [libgomp] Add Unix95 mutex support
> > * configure.tgt: Pass -DLIBGOMP_UNIX95 to the compiler
> > and -lposix4 to the linker on Solaris 2.5.1 and 2.6.
>
> Style nit: Can use AC_DEFINE instead?
>
> In general I don't like this kind of configure checks. Could it be
> possible to use AC_SEARCH_LIBS to look for pthread_mutex_init in -lposix4?
I thought configure.tgt was precisely aimed at getting rid of the configure
mess?
> Second, I see that pthread_mutexattr_t is used unconditionally:
>
> pthread_mutexattr_t attr;
>
> +#ifdef LIBGOMP_UNIX95
> + pthread_mutex_init (&lock->lock, NULL);
> +#else
> pthread_mutexattr_init (&attr);
> pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
> pthread_mutex_init (&lock->lock, &attr);
> - lock->count = 0;
> pthread_mutexattr_destroy (&attr);
> +#endif
You're right. A more annoying problem is that lock->owner is not initialized.
I've attached a revised patch, retested on SPARC/Solaris 2.5.1.
> So, first this is going to give an uninitialized warning, and second we
> could make the code conditional on the existence of
> PTHREAD_MUTEX_RECURSIVE, or something like this:
Is PTHREAD_MUTEX_RECURSIVE guaranteed to be a macro?
> Finally, does it harm to define _XOPEN_SOURCE unconditionally for
> Solaris 2.5.1/2.6?
I think you cannot reasonably define _XOPEN_SOURCE to 500 for Unix95.
--
Eric Botcazou
Index: configure.tgt
===================================================================
--- configure.tgt (revision 111111)
+++ configure.tgt (working copy)
@@ -91,7 +91,9 @@ case "${target}" in
XLDFLAGS="${XLDFLAGS} -lrt"
;;
- *)
+ *-*-solaris2.[56]*)
+ XCFLAGS="$(XCFLAGS) -DLIBGOMP_UNIX95"
+ XLDFLAGS="${XLDFLAGS} -lposix4"
;;
esac
Index: config/posix/omp-lock.h
===================================================================
--- config/posix/omp-lock.h (revision 111111)
+++ config/posix/omp-lock.h (working copy)
@@ -8,4 +8,12 @@
#include <pthread.h>
typedef pthread_mutex_t omp_lock_t;
-typedef struct { pthread_mutex_t lock; int count; } omp_nest_lock_t;
+
+typedef struct
+{
+ pthread_mutex_t lock;
+#ifdef LIBGOMP_UNIX95
+ pthread_t owner;
+#endif
+ int count;
+} omp_nest_lock_t;
Index: config/posix/lock.c
===================================================================
--- config/posix/lock.c (revision 111111)
+++ config/posix/lock.c (working copy)
@@ -33,8 +33,10 @@
to do better and streamline the locking as well as reduce the size
of the types exported. */
+#ifndef LIBGOMP_UNIX95
/* We need Unix98 extensions to get recursive locks. */
#define _XOPEN_SOURCE 500
+#endif
#include "libgomp.h"
@@ -72,13 +74,19 @@ omp_test_lock (omp_lock_t *lock)
void
omp_init_nest_lock (omp_nest_lock_t *lock)
{
+#ifdef LIBGOMP_UNIX95
+ pthread_mutex_init (&lock->lock, NULL);
+ lock->owner = (pthread_t) 0;
+#else
pthread_mutexattr_t attr;
pthread_mutexattr_init (&attr);
pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init (&lock->lock, &attr);
- lock->count = 0;
pthread_mutexattr_destroy (&attr);
+#endif
+
+ lock->count = 0;
}
void
@@ -90,7 +98,18 @@ omp_destroy_nest_lock (omp_nest_lock_t *
void
omp_set_nest_lock (omp_nest_lock_t *lock)
{
+#ifdef LIBGOMP_UNIX95
+ pthread_t me = pthread_self ();
+
+ if (lock->owner != me)
+ {
+ pthread_mutex_lock (&lock->lock);
+ lock->owner = me;
+ }
+#else
pthread_mutex_lock (&lock->lock);
+#endif
+
lock->count++;
}
@@ -98,15 +117,36 @@ void
omp_unset_nest_lock (omp_nest_lock_t *lock)
{
lock->count--;
+
+#ifdef LIBGOMP_UNIX95
+ if (lock->count == 0)
+ {
+ lock->owner = (pthread_t) 0;
+ pthread_mutex_unlock (&lock->lock);
+ }
+#else
pthread_mutex_unlock (&lock->lock);
+#endif
}
int
omp_test_nest_lock (omp_nest_lock_t *lock)
{
+#ifdef LIBGOMP_UNIX95
+ pthread_t me = pthread_self ();
+
+ if (lock->owner != me)
+ {
+ if (pthread_mutex_trylock (&lock->lock) != 0)
+ return 0;
+ lock->owner = me;
+ }
+ return ++lock->count;
+#else
if (pthread_mutex_trylock (&lock->lock) == 0)
return ++lock->count;
return 0;
+#endif
}
ialias (omp_init_lock)