[{"id":3672981,"web_url":"http://patchwork.ozlabs.org/comment/3672981/","msgid":"<20260403041752.GE6254@frogsfrogsfrogs>","list_archive_url":null,"date":"2026-04-03T04:17:52","subject":"Re: [PATCH 2/3] libsupport: add a portable get_thread_id() function","submitter":{"id":77032,"url":"http://patchwork.ozlabs.org/api/people/77032/","name":"Darrick J. Wong","email":"djwong@kernel.org"},"content":"On Fri, Apr 03, 2026 at 12:03:27AM -0400, Theodore Ts'o wrote:\n> The gettid() system call is only available on Linux.  So create a new\n> function, get_thread_id() which implements a number of different ways\n> of providing a thread id as an integer.\n> \n> Use get_thread_id() instead of gettid() in fuse2fs.\n> \n> Signed-off-by: Theodore Ts'o <tytso@mit.edu>\n> ---\n>  configure               | 12 ++++++++++++\n>  configure.ac            |  2 ++\n>  lib/config.h.in         |  6 ++++++\n>  lib/support/Makefile.in | 13 +++++++++++--\n>  lib/support/thread.c    | 36 ++++++++++++++++++++++++++++++++++++\n>  lib/support/thread.h    |  5 +++++\n>  misc/fuse2fs.c          |  3 ++-\n>  7 files changed, 74 insertions(+), 3 deletions(-)\n>  create mode 100644 lib/support/thread.c\n>  create mode 100644 lib/support/thread.h\n> \n> diff --git a/configure b/configure\n> index b9a82dcec..b04b31aff 100755\n> --- a/configure\n> +++ b/configure\n> @@ -13749,6 +13749,12 @@ if test \"x$ac_cv_func_getrusage\" = xyes\n>  then :\n>    printf \"%s\\n\" \"#define HAVE_GETRUSAGE 1\" >>confdefs.h\n>  \n> +fi\n> +ac_fn_c_check_func \"$LINENO\" \"gettid\" \"ac_cv_func_gettid\"\n> +if test \"x$ac_cv_func_gettid\" = xyes\n> +then :\n> +  printf \"%s\\n\" \"#define HAVE_GETTID 1\" >>confdefs.h\n> +\n>  fi\n>  ac_fn_c_check_func \"$LINENO\" \"jrand48\" \"ac_cv_func_jrand48\"\n>  if test \"x$ac_cv_func_jrand48\" = xyes\n> @@ -13893,6 +13899,12 @@ if test \"x$ac_cv_func_pthread_setname_np\" = xyes\n>  then :\n>    printf \"%s\\n\" \"#define HAVE_PTHREAD_SETNAME_NP 1\" >>confdefs.h\n>  \n> +fi\n> +ac_fn_c_check_func \"$LINENO\" \"pthread_threadid_np\" \"ac_cv_func_pthread_threadid_np\"\n> +if test \"x$ac_cv_func_pthread_threadid_np\" = xyes\n> +then :\n> +  printf \"%s\\n\" \"#define HAVE_PTHREAD_THREADID_NP 1\" >>confdefs.h\n> +\n>  fi\n>  ac_fn_c_check_func \"$LINENO\" \"qsort_r\" \"ac_cv_func_qsort_r\"\n>  if test \"x$ac_cv_func_qsort_r\" = xyes\n> diff --git a/configure.ac b/configure.ac\n> index 2473879fd..4921f81f7 100644\n> --- a/configure.ac\n> +++ b/configure.ac\n> @@ -1246,6 +1246,7 @@ AC_CHECK_FUNCS(m4_flatten([\n>  \tgetrandom\n>  \tgetrlimit\n>  \tgetrusage\n> +\tgettid\n>  \tjrand48\n>  \tkeyctl\n>  \tllistxattr\n> @@ -1270,6 +1271,7 @@ AC_CHECK_FUNCS(m4_flatten([\n>  \tpread64\n>  \tpwrite64\n>  \tpthread_setname_np\n> +\tpthread_threadid_np\n>  \tqsort_r\n>  \tsecure_getenv\n>  \tsetmntent\n> diff --git a/lib/config.h.in b/lib/config.h.in\n> index c6cbced5f..f129abfe7 100644\n> --- a/lib/config.h.in\n> +++ b/lib/config.h.in\n> @@ -181,6 +181,9 @@\n>  /* Define if the GNU gettext() function is already present or preinstalled. */\n>  #undef HAVE_GETTEXT\n>  \n> +/* Define to 1 if you have the 'gettid' function. */\n> +#undef HAVE_GETTID\n> +\n>  /* Define to 1 if you have the GNU-style 'qsort_r' function. */\n>  #undef HAVE_GNU_QSORT_R\n>  \n> @@ -331,6 +334,9 @@\n>  /* Define to 1 if you have the 'pthread_setname_np' function. */\n>  #undef HAVE_PTHREAD_SETNAME_NP\n>  \n> +/* Define to 1 if you have the 'pthread_threadid_np' function. */\n> +#undef HAVE_PTHREAD_THREADID_NP\n> +\n>  /* Define to 1 if you have the 'pwrite' function. */\n>  #undef HAVE_PWRITE\n>  \n> diff --git a/lib/support/Makefile.in b/lib/support/Makefile.in\n> index 6383816fd..9aac9cf00 100644\n> --- a/lib/support/Makefile.in\n> +++ b/lib/support/Makefile.in\n> @@ -25,6 +25,7 @@ OBJS=\t\tbthread.o \\\n>  \t\tquotaio.o \\\n>  \t\tquotaio_v2.o \\\n>  \t\tquotaio_tree.o \\\n> +\t\tthread.o \\\n>  \t\tdict.o \\\n>  \t\tdevname.o\n>  \n> @@ -41,6 +42,7 @@ SRCS=\t\t$(srcdir)/argv_parse.c \\\n>  \t\t$(srcdir)/quotaio.c \\\n>  \t\t$(srcdir)/quotaio_tree.c \\\n>  \t\t$(srcdir)/quotaio_v2.c \\\n> +\t\t$(srcdir)/thread.c \\\n>  \t\t$(srcdir)/dict.c \\\n>  \t\t$(srcdir)/devname.c\n>  \n> @@ -81,10 +83,15 @@ test_cstring: $(srcdir)/cstring.c\n>  \t$(Q) $(CC) -o test_cstring -DDEBUG_PROGRAM $(srcdir)/cstring.c \\\n>  \t\t$(ALL_CFLAGS)\n>  \n> +test_thread: $(srcdir)/thread.c\n> +\t$(E) \" CC $@\"\n> +\t$(Q) $(CC) -o test_thread -DDEBUG_PROGRAM $(srcdir)/thread.c \\\n> +\t\t$(ALL_CFLAGS)\n> +\n>  clean::\n>  \t$(RM) -f \\#* *.s *.o *.a *~ *.bak core profiled/* \\\n>  \t\t../libsupport.a ../libsupport_p.a $(SMANPAGES) \\\n> -\t\tprof_err.c prof_err.h test_profile test_cstring\n> +\t\tprof_err.c prof_err.h test_profile test_cstring test_thread\n>  \n>  #fullcheck check:: tst_uuid\n>  #\tLD_LIBRARY_PATH=$(LIB) DYLD_LIBRARY_PATH=$(LIB) ./tst_uuid\n> @@ -111,7 +118,7 @@ $(OBJS):\n>  argv_parse.o: $(srcdir)/argv_parse.c $(top_builddir)/lib/config.h \\\n>   $(top_builddir)/lib/dirpaths.h $(srcdir)/argv_parse.h\n>  bthread.o: $(srcdir)/bthread.c $(top_builddir)/lib/config.h \\\n> - $(srcdir)/bthread.h\n> + $(top_builddir)/lib/dirpaths.h $(srcdir)/bthread.h\n>  cstring.o: $(srcdir)/cstring.c $(top_builddir)/lib/config.h \\\n>   $(top_builddir)/lib/dirpaths.h $(srcdir)/cstring.h\n>  mkquota.o: $(srcdir)/mkquota.c $(top_builddir)/lib/config.h \\\n> @@ -183,6 +190,8 @@ quotaio_v2.o: $(srcdir)/quotaio_v2.c $(top_builddir)/lib/config.h \\\n>   $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \\\n>   $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/dqblk_v2.h \\\n>   $(srcdir)/quotaio_tree.h\n> +thread.o: $(srcdir)/thread.c $(top_builddir)/lib/config.h \\\n> + $(top_builddir)/lib/dirpaths.h $(srcdir)/thread.h\n>  dict.o: $(srcdir)/dict.c $(top_builddir)/lib/config.h \\\n>   $(top_builddir)/lib/dirpaths.h $(srcdir)/dict.h\n>  devname.o: $(srcdir)/devname.c $(top_builddir)/lib/config.h \\\n> diff --git a/lib/support/thread.c b/lib/support/thread.c\n> new file mode 100644\n> index 000000000..a9a10940c\n> --- /dev/null\n> +++ b/lib/support/thread.c\n> @@ -0,0 +1,36 @@\n> +/*\n> + * thread.c - utility functions for Posix threads\n\nShould these new files have an explicit license specification?\n\nOther than that,\nReviewed-by: \"Darrick J. Wong\" <djwong@kernel.org>\n\n--D\n\n> + */\n> +\n> +#include \"config.h\"\n> +#ifdef HAVE_PTHREAD\n> +#include <pthread.h>\n> +#endif\n> +#include <stdint.h>\n> +#include <stdio.h>\n> +#include <unistd.h>\n> +\n> +#include \"support/thread.h\"\n> +\n> +uint64_t get_thread_id(void)\n> +{\n> +#if defined(HAVE_GETTID)\n> +\treturn gettid();\n> +#elif defined(HAVE_PTHREAD_THREADID_NP)\n> +\tuint64_t tid;\n> +\n> +\tif (pthread_threadid_np(NULL, &tid))\n> +\t\treturn tid;\n> +#elif defined(HAVE_PTHREAD)\n> +\treturn (__u64)(uintptr_t) pthread_self();\n> +#endif\n> +\treturn getpid();\n> +}\n> +\n> +#ifdef DEBUG_PROGRAM\n> +int main(int argc, char **argv)\n> +{\n> +\tprintf(\"Thread id: %llu\\n\", get_thread_id());\n> +\treturn 0;\n> +}\n> +#endif\n> diff --git a/lib/support/thread.h b/lib/support/thread.h\n> new file mode 100644\n> index 000000000..9a7f5c9db\n> --- /dev/null\n> +++ b/lib/support/thread.h\n> @@ -0,0 +1,5 @@\n> +/*\n> + * thread.h -- header file for thread utilities\n> + */\n> +\n> +uint64_t get_thread_id(void);\n> diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c\n> index 0b43ec0fb..dfbc98636 100644\n> --- a/misc/fuse2fs.c\n> +++ b/misc/fuse2fs.c\n> @@ -48,6 +48,7 @@\n>  #include \"ext2fs/ext2_fs.h\"\n>  #include \"ext2fs/ext2fsP.h\"\n>  #include \"support/bthread.h\"\n> +#include \"support/thread.h\"\n>  #if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)\n>  # define FUSE_PLATFORM_OPTS\t\"\"\n>  #else\n> @@ -148,7 +149,7 @@ static inline uint64_t round_down(uint64_t b, unsigned int align)\n>  \n>  #define dbg_printf(fuse2fs, format, ...) \\\n>  \twhile ((fuse2fs)->debug) { \\\n> -\t\tprintf(\"FUSE2FS (%s): tid=%d \" format, (fuse2fs)->shortdev, gettid(), ##__VA_ARGS__); \\\n> +\t\tprintf(\"FUSE2FS (%s): tid=%llu \" format, (fuse2fs)->shortdev, get_thread_id(), ##__VA_ARGS__); \\\n>  \t\tfflush(stdout); \\\n>  \t\tbreak; \\\n>  \t}\n> -- \n> 2.51.0\n>","headers":{"Return-Path":"\n <SRS0=pOWJ=CC=vger.kernel.org=linux-ext4+bounces-15617-patchwork-incoming=ozlabs.org@ozlabs.org>","X-Original-To":["incoming@patchwork.ozlabs.org","linux-ext4@vger.kernel.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","patchwork-incoming@ozlabs.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256\n header.s=k20201202 header.b=nZ6RflA1;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=ozlabs.org\n (client-ip=2404:9400:2221:ea00::3; helo=mail.ozlabs.org;\n envelope-from=srs0=powj=cc=vger.kernel.org=linux-ext4+bounces-15617-patchwork-incoming=ozlabs.org@ozlabs.org;\n receiver=patchwork.ozlabs.org)","gandalf.ozlabs.org;\n arc=pass smtp.remote-ip=172.234.253.10 arc.chain=subspace.kernel.org","gandalf.ozlabs.org;\n dmarc=pass (p=quarantine dis=none) header.from=kernel.org","gandalf.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256\n header.s=k20201202 header.b=nZ6RflA1;\n\tdkim-atps=neutral","gandalf.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=172.234.253.10; helo=sea.lore.kernel.org;\n envelope-from=linux-ext4+bounces-15617-patchwork-incoming=ozlabs.org@vger.kernel.org;\n receiver=ozlabs.org)","smtp.subspace.kernel.org;\n\tdkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org\n header.b=\"nZ6RflA1\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=10.30.226.201"],"Received":["from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fn5711Z0mz1xtJ\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 03 Apr 2026 15:18:05 +1100 (AEDT)","from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3])\n\tby gandalf.ozlabs.org (Postfix) with ESMTP id 4fn5711370z4wDN\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 03 Apr 2026 15:18:05 +1100 (AEDT)","by gandalf.ozlabs.org (Postfix)\n\tid 4fn5710w5kz4wSc; Fri, 03 Apr 2026 15:18:05 +1100 (AEDT)","from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519)\n\t(No client certificate requested)\n\tby gandalf.ozlabs.org (Postfix) with ESMTPS id 4fn56x4CpMz4wDN\n\tfor <patchwork-incoming@ozlabs.org>; Fri, 03 Apr 2026 15:18:01 +1100 (AEDT)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sea.lore.kernel.org (Postfix) with ESMTP id D1E6230330B0\n\tfor <patchwork-incoming@ozlabs.org>; Fri,  3 Apr 2026 04:17:54 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id 318D82E9757;\n\tFri,  3 Apr 2026 04:17:54 +0000 (UTC)","from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org\n [10.30.226.201])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id E9F8C27EFF7\n\tfor <linux-ext4@vger.kernel.org>; Fri,  3 Apr 2026 04:17:53 +0000 (UTC)","by smtp.kernel.org (Postfix) with ESMTPSA id 8F4EFC4CEF7;\n\tFri,  3 Apr 2026 04:17:53 +0000 (UTC)"],"ARC-Seal":["i=2; a=rsa-sha256; d=ozlabs.org; s=201707; t=1775189885; cv=pass;\n\tb=tAh1L8moNje6iUcmGKGEeh1FTpc6cFbMhkAvoue61dOoBIoWzaY/RjtZrO9hf8MTYTbzqBcl93zRFV3zg8S1PjjPmGV774tAyAMSBEMR/3RoGN3psnqZTU6TL9GYvgylD+WOl7AeekrhsteEzPS+bmjkbFQArlXBTHmRWpLK10WznhHFc9FXri3/b1/MFrN6FcGLyUy1wxbyLcOQSCNLr0i/IYxketsl3UeI1iK1wQJvsbcUNUBuE8Trlh5bigcvGmJnxO3bnJZTKSNjmVFr43oMP+75A5U9lDVDZQZyW8HAXK9P+hWXHg/t80Zv/ZYQeSB4y7R2Dv+a8k7K+uqTow==","i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1775189874; cv=none;\n b=FCQvIv8x36mHTTxpJq3If5YLpIAkf5M1n8oxPLLoGZquEmEz1TpAzMxJJPF69/y+ACeNE/lf8lEjfEcbRS5dhiOOmlvGQpZDG4epa6HoKWzRmf15XgQrNXvGl8pZJx2/Bg42VTzQNfqUQYhNhEVCOkVPCLVFBbvBiq9XjfCB29M="],"ARC-Message-Signature":["i=2; a=rsa-sha256; d=ozlabs.org; s=201707;\n\tt=1775189885; c=relaxed/relaxed;\n\tbh=kMbdrz8c5fmlaB9WqK0iQTaZnlEblobA8zrusd+Wpns=;\n\th=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version:\n\t Content-Type:Content-Disposition:In-Reply-To;\n b=mjlS4QH9II+0/AVWKQrQCrp9KWGf/OJDEQdmgUXx2dp9+0J0ZRyUfSJXM9Fwe5meBSTD+tQQBnZMZKEtsj6k8juAebfboOYyxfDsKCp6V+q1xTjfd0IUkXVfUc0qOjE8FcvWQqWEqFZ2X2kIjIHZh2H2nrAlummNtJPjD0EUmRBRuswxSZEBxuJYn+ZN+xTepVKP9fYsZHUWLCeycWHBDLA1quZw9ClPdn0tzPwsbWKJxkEid97Y1RjxQvGdKubbgnDRHw9C+tLSc5dBgRZG41+vGhQS057spjBO4IWe4fRgJWCwNH5yzIgA2y1QPzCfjSK4Wt4laKa5WNnvZA1YBg==","i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1775189874; c=relaxed/simple;\n\tbh=tFf3lVc1wUpm6X9ApxmN9DDZ+LLpf4mr1EAo+Mcqrrk=;\n\th=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version:\n\t Content-Type:Content-Disposition:In-Reply-To;\n b=VP7W/5JCMRO09EloZnzX6emNZC8c0uc1xotrZnwzKxc7aOzJFiAFrdTIY65RVu+7lLrEtFFlSERP4xOk/pKRoNZ2VIckvJ8ZBKJLlB7ADCUEjqpMwF4K999hSc3OwPkKptkEzdI/rRQekgd2ClzSuOKO+7hgwb0N2/YCfdQhMvo="],"ARC-Authentication-Results":["i=2; gandalf.ozlabs.org;\n dmarc=pass (p=quarantine dis=none) header.from=kernel.org;\n dkim=pass (2048-bit key;\n unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256\n header.s=k20201202 header.b=nZ6RflA1; dkim-atps=neutral;\n spf=pass (client-ip=172.234.253.10; helo=sea.lore.kernel.org;\n envelope-from=linux-ext4+bounces-15617-patchwork-incoming=ozlabs.org@vger.kernel.org;\n receiver=ozlabs.org) smtp.mailfrom=vger.kernel.org","i=1; smtp.subspace.kernel.org;\n dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org\n header.b=nZ6RflA1; arc=none smtp.client-ip=10.30.226.201"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;\n\ts=k20201202; t=1775189873;\n\tbh=tFf3lVc1wUpm6X9ApxmN9DDZ+LLpf4mr1EAo+Mcqrrk=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=nZ6RflA13CN5udTv/tJ/o78NyKwnI7K2F73ETbg+uhBRJyVbieJUblBPdRstW4bME\n\t 68NqvgWzSOdxSumMftuei79Z8as2IgCLAq2HpWDzGnG3cd6a6HHBfV2SZF+uhissV4\n\t GHe7dOqiZCHGDHKs/Q+rr8x2h8VbIHoET9rlD9QR/GFi1/Hl1UiV0KbxJHPsNbmYlw\n\t AXGoRxegi3HrT8k3dHb607IQfxf0nUM7MDYBgjsymsxIIYqYcGHKsRd2StBpnkgPPp\n\t IrgEiOEONBF1X/Pwaa0H2BixpbeGYKAVitokELqG2YgFIqnnCzC7AkEKw+xNUMSfRq\n\t 8mexdcQ4+Lmrg==","Date":"Thu, 2 Apr 2026 21:17:52 -0700","From":"\"Darrick J. Wong\" <djwong@kernel.org>","To":"Theodore Ts'o <tytso@mit.edu>","Cc":"Ext4 Developers List <linux-ext4@vger.kernel.org>","Subject":"Re: [PATCH 2/3] libsupport: add a portable get_thread_id() function","Message-ID":"<20260403041752.GE6254@frogsfrogsfrogs>","References":"<20260403040328.2385083-1-tytso@mit.edu>\n <20260403040328.2385083-3-tytso@mit.edu>","Precedence":"bulk","X-Mailing-List":"linux-ext4@vger.kernel.org","List-Id":"<linux-ext4.vger.kernel.org>","List-Subscribe":"<mailto:linux-ext4+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:linux-ext4+unsubscribe@vger.kernel.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<20260403040328.2385083-3-tytso@mit.edu>","X-Spam-Status":"No, score=-1.2 required=5.0 tests=ARC_SIGNED,ARC_VALID,\n\tDKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DMARC_PASS,\n\tMAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=disabled\n\tversion=4.0.1","X-Spam-Checker-Version":"SpamAssassin 4.0.1 (2024-03-25) on gandalf.ozlabs.org"}}]