summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-10 13:52:43 +0000
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-10 13:52:43 +0000
commitf1316455d7f9abbf68cb227d4df1124e6a979454 (patch)
tree936bb6a2a1e5c4640b226d726742ba3a801811d3
parente18185c55f8d1b6d6efe1b6a35b4c8c7bd48754a (diff)
Added support for thread names on FreeBSD, NetBSD, OpenBSD, Darwin, OSF1 and others.
-rw-r--r--configure.ac76
-rw-r--r--src/microhttpd/mhd_threads.c30
-rw-r--r--src/microhttpd/mhd_threads.h3
3 files changed, 90 insertions, 19 deletions
diff --git a/configure.ac b/configure.ac
index b9918093..53c67de6 100644
--- a/configure.ac
+++ b/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]))
diff --git a/src/microhttpd/mhd_threads.c b/src/microhttpd/mhd_threads.c
index 07da4960..713c8348 100644
--- a/src/microhttpd/mhd_threads.c
+++ b/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
diff --git a/src/microhttpd/mhd_threads.h b/src/microhttpd/mhd_threads.h
index 23f21a28..01a7f5f0 100644
--- a/src/microhttpd/mhd_threads.h
+++ b/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)