Ver código fonte

Added support for thread names on FreeBSD, NetBSD, OpenBSD, Darwin, OSF1 and others.

Evgeny Grin (Karlson2k) 9 anos atrás
pai
commit
f1316455d7
3 arquivos alterados com 90 adições e 19 exclusões
  1. 60 16
      configure.ac
  2. 28 2
      src/microhttpd/mhd_threads.c
  3. 2 1
      src/microhttpd/mhd_threads.h

+ 60 - 16
configure.ac

@@ -302,6 +302,66 @@ AM_CONDITIONAL([USE_POSIX_THREADS], [test "x$USE_THREADS" = "xposix"])
 AM_CONDITIONAL([USE_W32_THREADS], [test "x$USE_THREADS" = "xw32"])
 AC_MSG_RESULT([[$USE_THREADS]])
 
+if test "x$HAVE_POSIX_THREADS" = "xyes"; then
+  # Check for pthread_setname_np()
+  SAVE_LIBS="$LIBS"
+  SAVE_CFLAGS="$CFLAGS"
+  LIBS="$PTHREAD_LIBS $LIBS"
+  CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
+  AC_CHECK_HEADERS([pthread_np.h])
+
+  AC_MSG_CHECKING([[for pthread_setname_np(3) in NetBSD or OSF1 form]])
+  AC_LINK_IFELSE(
+    [AC_LANG_PROGRAM([[
+#include <pthread.h>
+#ifdef HAVE_PTHREAD_NP_H
+#include <pthread_np.h>
+#endif
+]], [[int res = pthread_setname_np(pthread_self(), "name", 0);]])],
+    [AC_DEFINE([[HAVE_PTHREAD_SETNAME_NP_NETBSD]], [[1]], [Define if you have NetBSD form (or OSF1 form) of pthread_setname_np(3) function.])
+     AC_MSG_RESULT([[yes]])],
+    [AC_MSG_RESULT([[no]])
+
+     AC_MSG_CHECKING([[for pthread_setname_np(3) in GNU/Linux form]])
+     AC_LINK_IFELSE(
+       [AC_LANG_PROGRAM([[
+#include <pthread.h>
+#ifdef HAVE_PTHREAD_NP_H
+#include <pthread_np.h>
+#endif
+]], [[int res = pthread_setname_np(pthread_self(), "name");]])],
+        [AC_DEFINE([[HAVE_PTHREAD_SETNAME_NP_GNU]], [[1]], [Define if you have GNU/Linux form of pthread_setname_np(3) function.])
+         AC_MSG_RESULT([[yes]])],
+        [AC_MSG_RESULT([[no]])
+
+         AC_MSG_CHECKING([[for pthread_setname_np(3) in Darwin form]])
+         AC_LINK_IFELSE(
+           [AC_LANG_PROGRAM([[
+#include <pthread.h>
+#ifdef HAVE_PTHREAD_NP_H
+#include <pthread_np.h>
+#endif
+]], [[int res = pthread_setname_np("name");]])],
+           [AC_DEFINE([[HAVE_PTHREAD_SETNAME_NP_DARWIN]], [[1]], [Define if you have Darwin form of pthread_setname_np(3) function.])
+            AC_MSG_RESULT([[yes]])],
+           [AC_MSG_RESULT([[no]])
+
+            AC_MSG_CHECKING([[for pthread_setname_np(3) in FreeBSD form]])
+            AC_LINK_IFELSE(
+              [AC_LANG_PROGRAM([[
+#include <pthread.h>
+#ifdef HAVE_PTHREAD_NP_H
+#include <pthread_np.h>
+#endif
+]], [[pthread_set_name_np(pthread_self(), "name");]])],
+              [AC_DEFINE([[HAVE_PTHREAD_SET_NAME_NP_FREEBSD]], [[1]], [Define if you have FreeBSD form of pthread_set_name_np(3) function.])
+               AC_MSG_RESULT([[yes]])],
+              [AC_MSG_RESULT([[no]])] ) ]) ]) ])
+
+  LIBS="$SAVE_LIBS"
+  CFLAGS="$SAVE_CFLAGS"
+fi
+
 
 AM_CONDITIONAL(HAVE_W32, [test "x$os_is_native_w32" = "xyes"])
 w32_shared_lib_exp=no
@@ -405,22 +465,6 @@ fd = epoll_create1(EPOLL_CLOEXEC);]])],
     AC_DEFINE([[HAVE_EPOLL_CREATE1]], [[1]], [Define if you have epoll_create1 function.])])
 fi
 
-if test "x$HAVE_POSIX_THREADS" = "xyes"; then
-  # Check for pthread_setname_np()
-  SAVE_LIBS="$LIBS"
-  SAVE_CFLAGS="$CFLAGS"
-  LIBS="$PTHREAD_LIBS $LIBS"
-  CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
-  AC_MSG_CHECKING([[for pthread_setname_np]])
-  AC_LINK_IFELSE(
-    [AC_LANG_PROGRAM([[#include <pthread.h>]], [[  pthread_setname_np(pthread_self(), "name")]])],
-    [AC_DEFINE([[HAVE_PTHREAD_SETNAME_NP]], [[1]], [Define if you have pthread_setname_np function.])
-     AC_MSG_RESULT([[yes]])],
-    [AC_MSG_RESULT([[no]])] )
-  LIBS="$SAVE_LIBS"
-  CFLAGS="$SAVE_CFLAGS"
-fi
-
 # Check for headers that are ALWAYS required
 AC_CHECK_HEADERS([fcntl.h math.h errno.h limits.h stdio.h locale.h sys/stat.h sys/types.h],,AC_MSG_ERROR([Compiling libmicrohttpd requires standard UNIX headers files]))
 

+ 28 - 2
src/microhttpd/mhd_threads.c

@@ -31,6 +31,9 @@
 #endif
 #ifdef MHD_USE_THREAD_NAME_
 #include <stdlib.h>
+#ifdef HAVE_PTHREAD_NP_H
+#include <pthread_np.h>
+#endif /* HAVE_PTHREAD_NP_H */
 #endif /* MHD_USE_THREAD_NAME_ */
 #include <errno.h>
 
@@ -51,7 +54,8 @@ typedef DWORD MHD_thread_ID_;
 #else  /* MHD_USE_THREAD_NAME_ */
 
 #if defined(MHD_USE_POSIX_THREADS)
-#ifdef HAVE_PTHREAD_SETNAME_NP
+#if defined(HAVE_PTHREAD_SETNAME_NP_GNU) || defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD) \
+    || defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
 /**
  * Set thread name
  * @param thread_id ID of thread
@@ -63,16 +67,38 @@ static int MHD_set_thread_name_(const MHD_thread_ID_ thread_id, const char *thre
   if (NULL == thread_name)
     return 0;
 
+#if defined(HAVE_PTHREAD_SETNAME_NP_GNU)
   return !pthread_setname_np (thread_id, thread_name);
+#elif defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD)
+  /* FreeBSD and OpenBSD use different name and void return type */
+  pthread_set_name_np (thread_id, thread_name);
+  return !0;
+#elif defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
+  /* NetBSD use 3 arguments: second argument is string in printf-like format,
+   *                         third argument is single argument for printf;
+   * OSF1 use 3 arguments too, but last one always must be zero (NULL).
+   * MHD doesn't use '%' in thread names, so both form are used in same way.
+   */
+  return !pthread_setname_np (thread_id, thread_name, 0);
+#endif /* HAVE_PTHREAD_SETNAME_NP_NETBSD */
 }
 
+
 /**
  * Set current thread name
  * @param n name to set
  * @return non-zero on success, zero otherwise
  */
 #define MHD_set_cur_thread_name_(n) MHD_set_thread_name_(pthread_self(),(n))
-#endif /* HAVE_PTHREAD_SETNAME_NP */
+#elif defined(HAVE_PTHREAD_SETNAME_NP_DARWIN)
+
+/**
+ * Set current thread name
+ * @param n name to set
+ * @return non-zero on success, zero otherwise
+ */
+#define MHD_set_cur_thread_name_(n) (!(pthread_setname_np((n))))
+#endif /* HAVE_PTHREAD_SETNAME_NP_DARWIN */
 
 #elif defined(MHD_USE_W32_THREADS)
 #ifndef _MSC_FULL_VER

+ 2 - 1
src/microhttpd/mhd_threads.h

@@ -58,7 +58,8 @@
 
 #ifndef MHD_NO_THREAD_NAMES
 #  if defined(MHD_USE_POSIX_THREADS)
-#    ifdef HAVE_PTHREAD_SETNAME_NP
+#    if defined(HAVE_PTHREAD_SETNAME_NP_GNU) || defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD) \
+        defined(HAVE_PTHREAD_SETNAME_NP_DARWIN) || defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
 #      define MHD_USE_THREAD_NAME_
 #    endif /* HAVE_PTHREAD_SETNAME_NP */
 #  elif defined(MHD_USE_W32_THREADS)