Skip to content

Commit 1090c8c

Browse files
committed
Expose pthread_gettid_np(3) under _GNU_SOURCE
Looks like Glibc also added this function (and with a compatible prototype even!) -- something that I didn't know about, and has seemingly only done **years** after I already added it to KOS
1 parent aeb5b4d commit 1090c8c

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

kos/include/pthread.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* HASH CRC-32:0x5ecc7c65 */
1+
/* HASH CRC-32:0x825f2384 */
22
/* Copyright (c) 2019-2025 Griefer@Work *
33
* *
44
* This software is provided 'as-is', without any express or implied *
@@ -1258,28 +1258,32 @@ __CREDIRECT(__ATTR_IN(2),__errno_t,__NOTHROW_NCX,pthread_setname_np,(pthread_t _
12581258
#endif /* !... */
12591259
#endif /* !__pthread_setname_np_defined */
12601260
#endif /* __USE_GNU */
1261-
#ifdef __USE_KOS
1261+
#if defined(__USE_KOS) || defined(__USE_GNU)
12621262
#ifndef __pthread_gettid_np_defined
12631263
#define __pthread_gettid_np_defined
12641264
#ifdef __CRT_HAVE_pthread_gettid_np
12651265
/* >> pthread_gettid_np(3)
12661266
* Return the TID of the given `self'.
12671267
* If `self' has already terminated, 0 is returned
12681268
* @return: * : The TID of the given thread
1269-
* @return: 0 : The given `self' has already terminated */
1269+
* @return: 0 : The given `self' has already terminated (KOS)
1270+
* @return: -1: The given `self' has already terminated (GLibc) */
12701271
__CDECLARE(__ATTR_PURE __ATTR_WUNUSED,__pid_t,__NOTHROW_NCX,pthread_gettid_np,(pthread_t __self),(__self))
12711272
#elif defined(__CRT_HAVE_pthread_getunique_np) || defined(__CRT_HAVE_pthread_threadid_np)
12721273
#include <libc/local/pthread/pthread_gettid_np.h>
12731274
/* >> pthread_gettid_np(3)
12741275
* Return the TID of the given `self'.
12751276
* If `self' has already terminated, 0 is returned
12761277
* @return: * : The TID of the given thread
1277-
* @return: 0 : The given `self' has already terminated */
1278+
* @return: 0 : The given `self' has already terminated (KOS)
1279+
* @return: -1: The given `self' has already terminated (GLibc) */
12781280
__NAMESPACE_LOCAL_USING_OR_IMPL(pthread_gettid_np, __FORCELOCAL __ATTR_ARTIFICIAL __ATTR_PURE __ATTR_WUNUSED __pid_t __NOTHROW_NCX(__LIBCCALL pthread_gettid_np)(pthread_t __self) { return (__NAMESPACE_LOCAL_SYM __LIBC_LOCAL_NAME(pthread_gettid_np))(__self); })
12791281
#else /* ... */
12801282
#undef __pthread_gettid_np_defined
12811283
#endif /* !... */
12821284
#endif /* !__pthread_gettid_np_defined */
1285+
#endif /* __USE_KOS || __USE_GNU */
1286+
#ifdef __USE_KOS
12831287
#if !defined(__pthread_getpidfd_np_defined) && defined(__CRT_HAVE_pthread_getpidfd_np)
12841288
#define __pthread_getpidfd_np_defined
12851289
/* >> pthread_getpidfd_np(3)

kos/src/libc/auto/pthread.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* HASH CRC-32:0xdc0f4352 */
1+
/* HASH CRC-32:0xb6a8daab */
22
/* Copyright (c) 2019-2025 Griefer@Work *
33
* *
44
* This software is provided 'as-is', without any express or implied *
@@ -267,7 +267,8 @@ INTDEF ATTR_IN(2) errno_t NOTHROW_NCX(LIBDCALL libd_pthread_setname_np)(pthread_
267267
* Return the TID of the given `self'.
268268
* If `self' has already terminated, 0 is returned
269269
* @return: * : The TID of the given thread
270-
* @return: 0 : The given `self' has already terminated */
270+
* @return: 0 : The given `self' has already terminated (KOS)
271+
* @return: -1: The given `self' has already terminated (GLibc) */
271272
INTDEF ATTR_PURE WUNUSED pid_t NOTHROW_NCX(LIBDCALL libd_pthread_gettid_np)(pthread_t self);
272273
/* >> pthread_getpidfd_np(3)
273274
* Return a PIDfd for `self'. If not already allocated, allocate a PIDfd lazily.

kos/src/libc/magic/pthread.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,12 +1290,13 @@ int pthread_equal(pthread_t thr1, pthread_t thr2) {
12901290
%#endif /* __USE_GNU */
12911291

12921292

1293-
%#ifdef __USE_KOS
1293+
%#if defined(__USE_KOS) || defined(__USE_GNU)
12941294
@@>> pthread_gettid_np(3)
12951295
@@Return the TID of the given `self'.
12961296
@@If `self' has already terminated, 0 is returned
12971297
@@@return: * : The TID of the given thread
1298-
@@@return: 0 : The given `self' has already terminated
1298+
@@@return: 0 : The given `self' has already terminated (KOS)
1299+
@@@return: -1: The given `self' has already terminated (GLibc)
12991300
[[userimpl, guard, pure, wunused]]
13001301
[[section(".text.crt{|.dos}.sched.pthread.ext.kos.tid")]]
13011302
[[decl_include("<bits/types.h>", "<bits/crt/pthreadtypes.h>")]]
@@ -1307,7 +1308,10 @@ int pthread_equal(pthread_t thr1, pthread_t thr2) {
13071308
result = 0;
13081309
return result;
13091310
}
1311+
%#endif /* __USE_KOS || __USE_GNU */
1312+
13101313

1314+
%#ifdef __USE_KOS
13111315
@@>> pthread_getpidfd_np(3)
13121316
@@Return a PIDfd for `self'. If not already allocated, allocate a PIDfd lazily.
13131317
@@To guaranty that a PIDfd is available for a given thread, you can make use of

kos/src/libc/user/pthread.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -977,18 +977,19 @@ NOTHROW(LIBCCALL libc_pthread_self)(void)
977977
}
978978
/*[[[end:libc_pthread_self]]]*/
979979

980-
/*[[[head:libc_pthread_gettid_np,hash:CRC-32=0x8b5628fb]]]*/
980+
/*[[[head:libc_pthread_gettid_np,hash:CRC-32=0xa9aa40ae]]]*/
981981
/* >> pthread_gettid_np(3)
982982
* Return the TID of the given `self'.
983983
* If `self' has already terminated, 0 is returned
984984
* @return: * : The TID of the given thread
985-
* @return: 0 : The given `self' has already terminated */
985+
* @return: 0 : The given `self' has already terminated (KOS)
986+
* @return: -1: The given `self' has already terminated (GLibc) */
986987
INTERN ATTR_SECTION(".text.crt.sched.pthread.ext.kos.tid") ATTR_PURE WUNUSED pid_t
987988
NOTHROW_NCX(LIBCCALL libc_pthread_gettid_np)(pthread_t self)
988989
/*[[[body:libc_pthread_gettid_np]]]*/
989990
{
990991
pid_t result;
991-
result = self->pt_tid;
992+
result = atomic_read(&self->pt_tid);
992993
return result;
993994
}
994995
/*[[[end:libc_pthread_gettid_np]]]*/

kos/src/libc/user/pthread.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* HASH CRC-32:0x71b0ae1e */
1+
/* HASH CRC-32:0x63719ace */
22
/* Copyright (c) 2019-2025 Griefer@Work *
33
* *
44
* This software is provided 'as-is', without any express or implied *
@@ -277,7 +277,8 @@ INTDEF ATTR_IN(2) errno_t NOTHROW_NCX(LIBCCALL libc_pthread_setname_np)(pthread_
277277
* Return the TID of the given `self'.
278278
* If `self' has already terminated, 0 is returned
279279
* @return: * : The TID of the given thread
280-
* @return: 0 : The given `self' has already terminated */
280+
* @return: 0 : The given `self' has already terminated (KOS)
281+
* @return: -1: The given `self' has already terminated (GLibc) */
281282
INTDEF ATTR_PURE WUNUSED pid_t NOTHROW_NCX(LIBCCALL libc_pthread_gettid_np)(pthread_t self);
282283
/* >> pthread_getpidfd_np(3)
283284
* Return a PIDfd for `self'. If not already allocated, allocate a PIDfd lazily.

0 commit comments

Comments
 (0)