$OpenBSD: patch-configure_ac,v 1.3 2013/01/08 09:30:27 ajacoutot Exp $

From 277675a40e7252a1c5c479b0e0572d9b8a4014ae Mon Sep 17 00:00:00 2001
From: Simon McVittie <simon.mcvittie@collabora.co.uk>
Date: Mon, 13 Aug 2012 18:43:12 +0000
Subject: configure: redo pthread check to check for more things

--- configure.ac.orig	Fri Sep 28 21:41:40 2012
+++ configure.ac	Tue Jan  8 09:54:30 2013
@@ -953,15 +953,53 @@ AC_SUBST([XML_CFLAGS])
 AC_SUBST([XML_LIBS])
 
 # Thread lib detection
-AC_CHECK_FUNC(pthread_cond_timedwait,[AC_CHECK_LIB(pthread,pthread_cond_timedwait,
-                                                    [THREAD_LIBS="-lpthread"])])
+AC_ARG_VAR([THREAD_LIBS])
 save_libs="$LIBS"
 LIBS="$LIBS $THREAD_LIBS"
-AC_CHECK_FUNC(pthread_condattr_setclock,have_pthread_condattr_setclock=true,have_pthread_condattr_setclock=false)
-if test x$have_pthread_condattr_setclock = xtrue; then
-    AC_SEARCH_LIBS([clock_getres],[rt],[THREAD_LIBS="$THREAD_LIBS -lrt"])
-    AC_MSG_CHECKING([for CLOCK_MONOTONIC])
-    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <time.h>
+
+is_missing_pthread_function="is required when compiling D-Bus on Unix platforms, but is not in your libc or libpthread. Please open a bug on https://bugs.freedesktop.org/enter_bug.cgi?product=dbus with details of your platform."
+
+# Don't do these automatic checks if the user set THREAD_LIBS on the
+# configure command-line. If they did, we assume they're right.
+#
+# We also don't do these checks on Windows, because you don't need magical
+# linker flags to have threading support there.
+AS_IF([test "x$dbus_unix" = xyes && test "x$THREAD_LIBS" = x],
+  [
+    # Mandatory pthread functions. In principle, some of these could be made
+    # optional if there are platforms that don't have them.
+    #
+    # Currently, we only look in -lpthread.
+    # In principle we might need to look in -lpthreads, -lthreads, ...
+    # as well - please file a bug if your platform needs this.
+    AC_SEARCH_LIBS([pthread_cond_timedwait],
+        [pthread],
+        [THREAD_LIBS="$LIBS"],
+        [AC_MSG_ERROR([pthread_cond_timedwait $is_missing_pthread_function])],
+        [])
+    AC_SEARCH_LIBS([pthread_mutexattr_init],
+        [pthread],
+        [THREAD_LIBS="$LIBS"],
+        [AC_MSG_ERROR([pthread_mutexattr_init $is_missing_pthread_function])],
+        [])
+    AC_SEARCH_LIBS([pthread_mutexattr_settype],
+        [pthread],
+        [THREAD_LIBS="$LIBS"],
+        [AC_MSG_ERROR([pthread_mutexattr_settype $is_missing_pthread_function])],
+        [])
+
+    # Optional, for monotonic clocks. Because it's optional, this check
+    # is non-fatal if we don't find it.
+    AC_SEARCH_LIBS([pthread_condattr_setclock],
+        [pthread],
+        [THREAD_LIBS="$LIBS"])
+
+    AS_IF([test "x$ac_cv_search_pthread_condattr_setclock" != xno],
+      [
+        AC_SEARCH_LIBS([clock_getres], [rt], [THREAD_LIBS="$LIBS"])
+        AC_MSG_CHECKING([for CLOCK_MONOTONIC])
+        AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
+[[#include <time.h>
 #include <pthread.h>
 ]], [[
 struct timespec monotonic_timer;
@@ -970,15 +1008,17 @@ pthread_condattr_init (&attr);
 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
 clock_getres (CLOCK_MONOTONIC,&monotonic_timer);
 ]])],
-[have_clock_monotonic=true],
-[have_clock_monotonic=false])
-if test x$have_clock_monotonic = xtrue; then
-    AC_MSG_RESULT([found])
-    AC_DEFINE(HAVE_MONOTONIC_CLOCK, 1, [Define if we have CLOCK_MONOTONIC])
-else
-    AC_MSG_RESULT([not found])
-fi
-fi
+            [have_clock_monotonic=true],
+            [have_clock_monotonic=false])
+        AS_IF([test x$have_clock_monotonic = xtrue],
+	  [
+            AC_MSG_RESULT([found])
+            AC_DEFINE(HAVE_MONOTONIC_CLOCK, 1, [Define if we have CLOCK_MONOTONIC])
+	  ],
+          [AC_MSG_RESULT([not found])])
+      ]) dnl have pthread_condattr_setclock
+  ]) dnl on Unix
+
 LIBS="$save_libs"
 
 AC_SUBST([THREAD_LIBS])
