port: fix pthread autoconf detection to support FreeBSD
[lttng-ust.git] / liblttng-ust / compat.h
index 003b480aa01d27dc428877dc9935963335e5340d..7d1ee590326fd1b9616569420b32846d4e458f7f 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <pthread.h>
+
 /*
- * lttng_ust_getprocname.
+ * Limit imposed by Linux UST-sessiond ABI.
  */
-#ifdef __linux__
+#define LTTNG_UST_PROCNAME_LEN 17
 
-#include <sys/prctl.h>
+#define LTTNG_UST_PROCNAME_SUFFIX "-ust"
 
-#define LTTNG_UST_PROCNAME_LEN 17
 
+#if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
 static inline
-void lttng_ust_getprocname(char *name)
+int lttng_pthread_setname_np(const char *name)
 {
-       (void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0);
+        return pthread_setname_np(pthread_self(), name);
 }
 
-/*
- * If pthread_setname_np is available.
- */
-#ifdef HAVE_PTHREAD_SETNAME_NP
 static inline
-int lttng_pthread_setname_np(pthread_t thread, const char *name)
+int lttng_pthread_getname_np(char *name, size_t len)
 {
-       return pthread_setname_np(thread, name);
+        return pthread_getname_np(pthread_self(), name, len);
 }
-#endif
-
-#elif defined(__FreeBSD__)
-
-#include <stdlib.h>
-#include <string.h>
-
-/*
- * Limit imposed by Linux UST-sessiond ABI.
- */
-#define LTTNG_UST_PROCNAME_LEN 17
-
-/*
- * Acts like linux prctl, the string is not necessarily 0-terminated if
- * 16-byte long.
- */
+#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
 static inline
-void lttng_ust_getprocname(char *name)
+int lttng_pthread_setname_np(const char *name)
 {
-       const char *bsd_name;
-
-       bsd_name = getprogname();
-       if (!bsd_name)
-               name[0] = '\0';
-       else
-               strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1);
+        return pthread_setname_np(name);
 }
 
+static inline
+int lttng_pthread_getname_np(char *name, size_t len)
+{
+        return pthread_getname_np(name, len);
+}
+#else
 /*
- * If pthread_set_name_np is available.
+ * For platforms without thread name support, do nothing.
  */
-#ifdef HAVE_PTHREAD_SET_NAME_NP
 static inline
-int lttng_pthread_setname_np(pthread_t thread, const char *name)
+int lttng_pthread_setname_np(const char *name)
 {
-       return pthread_set_name_np(thread, name);
+        return -ENOSYS;
 }
-#endif
 
+static inline
+int lttng_pthread_getname_np(char *name, size_t len)
+{
+        return -ENOSYS;
+}
 #endif
 
-/*
- * If a pthread setname/set_name function is available, declare
- * the setustprocname() function that will add '-ust' to the end
- * of the current process name, while truncating it if needed.
- */
-#if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SETNAME_NP)
-#define LTTNG_UST_PROCNAME_SUFFIX "-ust"
-
-#include <pthread.h>
+static inline
+void lttng_ust_getprocname(char *name)
+{
+       lttng_pthread_getname_np(name, LTTNG_UST_PROCNAME_LEN);
+}
 
 static inline
 int lttng_ust_setustprocname(void)
@@ -102,7 +84,7 @@ int lttng_ust_setustprocname(void)
        char name[LTTNG_UST_PROCNAME_LEN];
        int limit = LTTNG_UST_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX) - 1;
 
-       lttng_ust_getprocname(name);
+       lttng_pthread_getname_np(name, LTTNG_UST_PROCNAME_LEN);
 
        len = strlen(name);
        if (len > limit) {
@@ -114,18 +96,12 @@ int lttng_ust_setustprocname(void)
                goto error;
        }
 
-       ret = lttng_pthread_setname_np(pthread_self(), name);
+       ret = lttng_pthread_setname_np(name);
 
 error:
        return ret;
 }
-#else
-static inline
-int lttng_ust_setustprocname(void)
-{
-       return 0;
-}
-#endif
+
 
 #include <errno.h>
 
This page took 0.024991 seconds and 4 git commands to generate.