]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
libsupport: add a portable get_thread_id() function
authorTheodore Ts'o <tytso@mit.edu>
Fri, 3 Apr 2026 02:54:13 +0000 (22:54 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 3 Apr 2026 03:27:15 +0000 (23:27 -0400)
The gettid() system call is only available on Linux.  So create a new
function, get_thread_id() which implements a number of different ways
of providing a thread id as an integer.

Use get_thread_id() instead of gettid() in fuse2fs.

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
configure
configure.ac
lib/config.h.in
lib/support/Makefile.in
lib/support/thread.c [new file with mode: 0644]
lib/support/thread.h [new file with mode: 0644]
misc/fuse2fs.c

index b9a82dceca38e8fe0190642d03fee2fb88676618..b04b31aff7fad12dc643842dec20ac9eb697be3b 100755 (executable)
--- a/configure
+++ b/configure
@@ -13749,6 +13749,12 @@ if test "x$ac_cv_func_getrusage" = xyes
 then :
   printf "%s\n" "#define HAVE_GETRUSAGE 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "gettid" "ac_cv_func_gettid"
+if test "x$ac_cv_func_gettid" = xyes
+then :
+  printf "%s\n" "#define HAVE_GETTID 1" >>confdefs.h
+
 fi
 ac_fn_c_check_func "$LINENO" "jrand48" "ac_cv_func_jrand48"
 if test "x$ac_cv_func_jrand48" = xyes
@@ -13893,6 +13899,12 @@ if test "x$ac_cv_func_pthread_setname_np" = xyes
 then :
   printf "%s\n" "#define HAVE_PTHREAD_SETNAME_NP 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "pthread_threadid_np" "ac_cv_func_pthread_threadid_np"
+if test "x$ac_cv_func_pthread_threadid_np" = xyes
+then :
+  printf "%s\n" "#define HAVE_PTHREAD_THREADID_NP 1" >>confdefs.h
+
 fi
 ac_fn_c_check_func "$LINENO" "qsort_r" "ac_cv_func_qsort_r"
 if test "x$ac_cv_func_qsort_r" = xyes
index 2473879fd286cfe3db3493509cc9e4c482877e26..4921f81f7194cb5498c53aca94082b45f26a1ff5 100644 (file)
@@ -1246,6 +1246,7 @@ AC_CHECK_FUNCS(m4_flatten([
        getrandom
        getrlimit
        getrusage
+       gettid
        jrand48
        keyctl
        llistxattr
@@ -1270,6 +1271,7 @@ AC_CHECK_FUNCS(m4_flatten([
        pread64
        pwrite64
        pthread_setname_np
+       pthread_threadid_np
        qsort_r
        secure_getenv
        setmntent
index c6cbced5f3afd7d4add1ec368008539e831fb192..f129abfe743e15ee3345f8c901076bed5665be26 100644 (file)
 /* Define if the GNU gettext() function is already present or preinstalled. */
 #undef HAVE_GETTEXT
 
+/* Define to 1 if you have the 'gettid' function. */
+#undef HAVE_GETTID
+
 /* Define to 1 if you have the GNU-style 'qsort_r' function. */
 #undef HAVE_GNU_QSORT_R
 
 /* Define to 1 if you have the 'pthread_setname_np' function. */
 #undef HAVE_PTHREAD_SETNAME_NP
 
+/* Define to 1 if you have the 'pthread_threadid_np' function. */
+#undef HAVE_PTHREAD_THREADID_NP
+
 /* Define to 1 if you have the 'pwrite' function. */
 #undef HAVE_PWRITE
 
index 6383816fd99cd4c62b063ca0cfd91386f590cc9c..9aac9cf00b8133b56881ca3db94e268b9bf4c27b 100644 (file)
@@ -25,6 +25,7 @@ OBJS=         bthread.o \
                quotaio.o \
                quotaio_v2.o \
                quotaio_tree.o \
+               thread.o \
                dict.o \
                devname.o
 
@@ -41,6 +42,7 @@ SRCS=         $(srcdir)/argv_parse.c \
                $(srcdir)/quotaio.c \
                $(srcdir)/quotaio_tree.c \
                $(srcdir)/quotaio_v2.c \
+               $(srcdir)/thread.c \
                $(srcdir)/dict.c \
                $(srcdir)/devname.c
 
@@ -81,10 +83,15 @@ test_cstring: $(srcdir)/cstring.c
        $(Q) $(CC) -o test_cstring -DDEBUG_PROGRAM $(srcdir)/cstring.c \
                $(ALL_CFLAGS)
 
+test_thread: $(srcdir)/thread.c
+       $(E) " CC $@"
+       $(Q) $(CC) -o test_thread -DDEBUG_PROGRAM $(srcdir)/thread.c \
+               $(ALL_CFLAGS)
+
 clean::
        $(RM) -f \#* *.s *.o *.a *~ *.bak core profiled/* \
                ../libsupport.a ../libsupport_p.a $(SMANPAGES) \
-               prof_err.c prof_err.h test_profile test_cstring
+               prof_err.c prof_err.h test_profile test_cstring test_thread
 
 #fullcheck check:: tst_uuid
 #      LD_LIBRARY_PATH=$(LIB) DYLD_LIBRARY_PATH=$(LIB) ./tst_uuid
@@ -111,7 +118,7 @@ $(OBJS):
 argv_parse.o: $(srcdir)/argv_parse.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(srcdir)/argv_parse.h
 bthread.o: $(srcdir)/bthread.c $(top_builddir)/lib/config.h \
- $(srcdir)/bthread.h
+ $(top_builddir)/lib/dirpaths.h $(srcdir)/bthread.h
 cstring.o: $(srcdir)/cstring.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(srcdir)/cstring.h
 mkquota.o: $(srcdir)/mkquota.c $(top_builddir)/lib/config.h \
@@ -183,6 +190,8 @@ quotaio_v2.o: $(srcdir)/quotaio_v2.c $(top_builddir)/lib/config.h \
  $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \
  $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/dqblk_v2.h \
  $(srcdir)/quotaio_tree.h
+thread.o: $(srcdir)/thread.c $(top_builddir)/lib/config.h \
+ $(top_builddir)/lib/dirpaths.h $(srcdir)/thread.h
 dict.o: $(srcdir)/dict.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(srcdir)/dict.h
 devname.o: $(srcdir)/devname.c $(top_builddir)/lib/config.h \
diff --git a/lib/support/thread.c b/lib/support/thread.c
new file mode 100644 (file)
index 0000000..a9a1094
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * thread.c - utility functions for Posix threads
+ */
+
+#include "config.h"
+#ifdef HAVE_PTHREAD
+#include <pthread.h>
+#endif
+#include <stdint.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "support/thread.h"
+
+uint64_t get_thread_id(void)
+{
+#if defined(HAVE_GETTID)
+       return gettid();
+#elif defined(HAVE_PTHREAD_THREADID_NP)
+       uint64_t tid;
+
+       if (pthread_threadid_np(NULL, &tid))
+               return tid;
+#elif defined(HAVE_PTHREAD)
+       return (__u64)(uintptr_t) pthread_self();
+#endif
+       return getpid();
+}
+
+#ifdef DEBUG_PROGRAM
+int main(int argc, char **argv)
+{
+       printf("Thread id: %llu\n", get_thread_id());
+       return 0;
+}
+#endif
diff --git a/lib/support/thread.h b/lib/support/thread.h
new file mode 100644 (file)
index 0000000..9a7f5c9
--- /dev/null
@@ -0,0 +1,5 @@
+/*
+ * thread.h -- header file for thread utilities
+ */
+
+uint64_t get_thread_id(void);
index 0b43ec0fb9a4791e4da4baacffa789137d93b22d..dfbc986363a00899ea9af9613b45dd1191b70e6e 100644 (file)
@@ -48,6 +48,7 @@
 #include "ext2fs/ext2_fs.h"
 #include "ext2fs/ext2fsP.h"
 #include "support/bthread.h"
+#include "support/thread.h"
 #if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
 # define FUSE_PLATFORM_OPTS    ""
 #else
@@ -148,7 +149,7 @@ static inline uint64_t round_down(uint64_t b, unsigned int align)
 
 #define dbg_printf(fuse2fs, format, ...) \
        while ((fuse2fs)->debug) { \
-               printf("FUSE2FS (%s): tid=%d " format, (fuse2fs)->shortdev, gettid(), ##__VA_ARGS__); \
+               printf("FUSE2FS (%s): tid=%llu " format, (fuse2fs)->shortdev, get_thread_id(), ##__VA_ARGS__); \
                fflush(stdout); \
                break; \
        }