Unbreak FreeBSD cross-build on MacOS broken in 051460b8b

MacOS used FreeBSD-compatible getprogname() and pthread_getname_np().
But pthread_getthreadid_np() does not exist on MacOS. This implements
libspl_gettid() using pthread_threadid_np() to get the thread id
of the current thread.

Tested with FreeBSD GitHub actions
freebsd-src/.github/workflows/cross-bootstrap-tools.yml

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Rob Norris <rob.norris@klarasystems.com>
Signed-off-by: Martin Matuska <mm@FreeBSD.org>
Closes #16167
This commit is contained in:
Martin Matuška 2024-05-09 16:42:51 +02:00 committed by Tony Hutter
parent 88686213c3
commit bc42d96d66
1 changed files with 20 additions and 1 deletions

View File

@ -41,9 +41,11 @@
#define libspl_getprogname() (program_invocation_short_name) #define libspl_getprogname() (program_invocation_short_name)
#define libspl_getthreadname(buf, len) \ #define libspl_getthreadname(buf, len) \
prctl(PR_GET_NAME, (unsigned long)(buf), 0, 0, 0) prctl(PR_GET_NAME, (unsigned long)(buf), 0, 0, 0)
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__) || defined(__APPLE__)
#if !defined(__APPLE__)
#include <pthread_np.h> #include <pthread_np.h>
#define libspl_gettid() pthread_getthreadid_np() #define libspl_gettid() pthread_getthreadid_np()
#endif
#define libspl_getprogname() getprogname() #define libspl_getprogname() getprogname()
#define libspl_getthreadname(buf, len) \ #define libspl_getthreadname(buf, len) \
pthread_getname_np(pthread_self(), buf, len); pthread_getname_np(pthread_self(), buf, len);
@ -98,6 +100,19 @@ libspl_dump_backtrace(void)
#define libspl_dump_backtrace() #define libspl_dump_backtrace()
#endif #endif
#if defined(__APPLE__)
static inline uint64_t
libspl_gettid(void)
{
uint64_t tid;
if (pthread_threadid_np(NULL, &tid) != 0)
tid = 0;
return (tid);
}
#endif
static boolean_t libspl_assert_ok = B_FALSE; static boolean_t libspl_assert_ok = B_FALSE;
void void
@ -128,7 +143,11 @@ libspl_assertf(const char *file, const char *func, int line,
fprintf(stderr, "\n" fprintf(stderr, "\n"
" PID: %-8u COMM: %s\n" " PID: %-8u COMM: %s\n"
#if defined(__APPLE__)
" TID: %-8" PRIu64 " NAME: %s\n",
#else
" TID: %-8u NAME: %s\n", " TID: %-8u NAME: %s\n",
#endif
getpid(), libspl_getprogname(), getpid(), libspl_getprogname(),
libspl_gettid(), tname); libspl_gettid(), tname);