namhyung / uftrace

Function graph tracer for C/C++/Rust/Python
https://uftrace.github.io/slide/
GNU General Public License v2.0
2.95k stars 430 forks source link

Refactoring non-standard `pthread_*` to C11 Standard `thrd` #1795

Open paranlee opened 11 months ago

paranlee commented 11 months ago

C11 Standard thrd

thrdthread support in the C11 standard. Since the Linux kernel is also being refactored to C11, How we think about the proposal to refactor pthread_* to thrd for a future development?

honggyukim commented 11 months ago

This might be doable, but we must do heavy testing if we replace the existing pthread code. This might be a risky work without those testing.

namhyung commented 11 months ago

Do all compilers and C libraries support C11 thread APIs now?

paranlee commented 7 months ago

I tested thrd_* on GCC, Clang but i need to examining the evidence for the proof.

ISO thred.h and thrd_*

We can find n1548 standard [1].

7.25.5 Thread functions 7.25.5.1 The thrd_create function Synopsis

1 #include

int thrd_create(thrd_t thr, thrd_start_t func, void arg);

Description

2 The thrd_create function creates a new thread executing func(arg). If the thrd_create function succeeds, it sets the object pointed to by thr to the identifier of the newly created thread. (A thread’s identifier may be reused for a different thread once the original thread has exited and either been detached or joined to another thread.) The completion of the thrd_create function synchronizes with the beginning of the execution of the new thread.

Returns

3 The thrd_create function returns thrd_success on success, or thrd_nomem if no memory could be allocated for the thread requested, or thrd_error if the request could not be honored.

GCC

Support for the standard ISO/IEC 9899:2011 (C11) in GCC (draft n1570, PDF)

GCC 4.9 Changes: “ISO C11 support is now at a similar level of completeness to ISO C99 support [2].

Threading [Optional] | Library issue (glibc 2.28, musl 1.1.5)

Clang

thread.h Support commit [3]

Test Case For C11 Standard [4]

LIBC

Android Bionic

bionic - threads.h: Add C11 thread support [6].

  Product: glibc
  Component: nptl
  Version: 2.15

GLIBC

glibc - thread.h Support [5]

Compiler Standard Default C version is C11

2014: GCC it was a shift from using C89 to default to now using C11, Clang now uses C11 by default [7].

There clang -fsanitize=thread option, but not working on C11 issue [9].

NOTE: Many of -fsanitize=... [10]

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf [2] https://gcc.gnu.org/wiki/C11Status [3] https://github.com/llvm/llvm-project/commit/fe8017476c7788806859a0fbac0c82be38874b47 [4] https://github.com/llvm/llvm-project/tree/main/clang/test/C/C11 [5] https://sourceware.org/bugzilla/show_bug.cgi?id=14092#c10 [6] https://android.googlesource.com/platform/bionic/+/420671135237cdf40c98d2336fb8712b7f0a2861 [7] https://www.phoronix.com/news/MTgyMzM [8] https://reviews.llvm.org/rGab506adf7d3ced6abcaf42f92de3d6cd15fa19e8 [9] https://github.com/google/sanitizers/issues/1603 [10] https://github.com/llvm/llvm-project/commit/279a4d0d67c874e80c171666822f2fabdd6fa926