From f5bbf4b51521b874306c8632a3f4f531f3883d61 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Thu, 15 Aug 2024 20:43:52 +0800 Subject: [PATCH 1/2] c/pthread: nogc --- c/pthread/_pthread/pthread_nogc.c | 9 ---- c/pthread/{_pthread => _wrap}/pthread_gc.c | 0 c/pthread/pthread.go | 50 ------------------ c/pthread/pthread_gc.go | 56 ++++++++++++++++++++ c/pthread/pthread_nogc.go | 61 ++++++++++++++++++++-- c/pthread/sync/{_pthd => _wrap}/pthd.c | 0 c/pthread/sync/sync.go | 2 +- 7 files changed, 115 insertions(+), 63 deletions(-) delete mode 100644 c/pthread/_pthread/pthread_nogc.c rename c/pthread/{_pthread => _wrap}/pthread_gc.c (100%) rename c/pthread/sync/{_pthd => _wrap}/pthd.c (100%) diff --git a/c/pthread/_pthread/pthread_nogc.c b/c/pthread/_pthread/pthread_nogc.c deleted file mode 100644 index fd352eea..00000000 --- a/c/pthread/_pthread/pthread_nogc.c +++ /dev/null @@ -1,9 +0,0 @@ -#include - -int llgoPthreadCreate(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) { - return pthread_create(thread, attr, start_routine, arg); -} - -int llgoPthreadJoin(pthread_t thread, void **retval) { - return pthread_join(thread, retval); -} diff --git a/c/pthread/_pthread/pthread_gc.c b/c/pthread/_wrap/pthread_gc.c similarity index 100% rename from c/pthread/_pthread/pthread_gc.c rename to c/pthread/_wrap/pthread_gc.c diff --git a/c/pthread/pthread.go b/c/pthread/pthread.go index 49ac2877..eb72d6f8 100644 --- a/c/pthread/pthread.go +++ b/c/pthread/pthread.go @@ -35,56 +35,6 @@ type aThread struct { // Thread represents a POSIX thread. type Thread = *aThread -// The pthread_create() function starts a new thread in the calling -// process. The new thread starts execution by invoking -// start_routine(); arg is passed as the sole argument of -// start_routine(). -// -// The new thread terminates in one of the following ways: -// -// - It calls pthread_exit(3), specifying an exit status value that -// is available to another thread in the same process that calls -// pthread_join(3). -// -// - It returns from start_routine(). This is equivalent to -// calling pthread_exit(3) with the value supplied in the return -// statement. -// -// - It is canceled (see pthread_cancel(3)). -// -// - Any of the threads in the process calls exit(3), or the main -// thread performs a return from main(). This causes the -// termination of all threads in the process. -// -// On success, pthread_create() returns 0; on error, it returns an -// error number, and the contents of *thread are undefined. -// -// See https://man7.org/linux/man-pages/man3/pthread_create.3.html -// -//go:linkname Create C.llgoPthreadCreate -func Create(pthread *Thread, attr *Attr, routine func(c.Pointer) c.Pointer, arg c.Pointer) c.Int - -// The pthread_join() function waits for the thread specified by -// thread to terminate. If that thread has already terminated, then -// pthread_join() returns immediately. The thread specified by -// thread must be joinable. -// -// If retval is not NULL, then pthread_join() copies the exit status -// of the target thread (i.e., the value that the target thread -// supplied to pthread_exit(3)) into the location pointed to by -// retval. If the target thread was canceled, then PTHREAD_CANCELED -// is placed in the location pointed to by retval. -// -// If multiple threads simultaneously try to join with the same -// thread, the results are undefined. If the thread calling -// pthread_join() is canceled, then the target thread will remain -// joinable (i.e., it will not be detached). -// -// See https://man7.org/linux/man-pages/man3/pthread_join.3.html -// -//go:linkname Join C.llgoPthreadJoin -func Join(thread Thread, retval *c.Pointer) c.Int - // The pthread_exit() function terminates the calling thread and // returns a value via retval that (if the thread is joinable) is // available to another thread in the same process that calls diff --git a/c/pthread/pthread_gc.go b/c/pthread/pthread_gc.go index c3d79f6c..f45f9d14 100644 --- a/c/pthread/pthread_gc.go +++ b/c/pthread/pthread_gc.go @@ -19,7 +19,63 @@ package pthread +import ( + _ "unsafe" + + "github.com/goplus/llgo/c" +) + const ( LLGoFiles = "$(pkg-config --cflags bdw-gc): _pthread/pthread_gc.c" LLGoPackage = "link: $(pkg-config --libs bdw-gc); -lgc" ) + +// The pthread_create() function starts a new thread in the calling +// process. The new thread starts execution by invoking +// start_routine(); arg is passed as the sole argument of +// start_routine(). +// +// The new thread terminates in one of the following ways: +// +// - It calls pthread_exit(3), specifying an exit status value that +// is available to another thread in the same process that calls +// pthread_join(3). +// +// - It returns from start_routine(). This is equivalent to +// calling pthread_exit(3) with the value supplied in the return +// statement. +// +// - It is canceled (see pthread_cancel(3)). +// +// - Any of the threads in the process calls exit(3), or the main +// thread performs a return from main(). This causes the +// termination of all threads in the process. +// +// On success, pthread_create() returns 0; on error, it returns an +// error number, and the contents of *thread are undefined. +// +// See https://man7.org/linux/man-pages/man3/pthread_create.3.html +// +//go:linkname Create C.llgoPthreadCreate +func Create(pthread *Thread, attr *Attr, routine func(c.Pointer) c.Pointer, arg c.Pointer) c.Int + +// The pthread_join() function waits for the thread specified by +// thread to terminate. If that thread has already terminated, then +// pthread_join() returns immediately. The thread specified by +// thread must be joinable. +// +// If retval is not NULL, then pthread_join() copies the exit status +// of the target thread (i.e., the value that the target thread +// supplied to pthread_exit(3)) into the location pointed to by +// retval. If the target thread was canceled, then PTHREAD_CANCELED +// is placed in the location pointed to by retval. +// +// If multiple threads simultaneously try to join with the same +// thread, the results are undefined. If the thread calling +// pthread_join() is canceled, then the target thread will remain +// joinable (i.e., it will not be detached). +// +// See https://man7.org/linux/man-pages/man3/pthread_join.3.html +// +//go:linkname Join C.llgoPthreadJoin +func Join(thread Thread, retval *c.Pointer) c.Int diff --git a/c/pthread/pthread_nogc.go b/c/pthread/pthread_nogc.go index bbf3549e..9e1da640 100644 --- a/c/pthread/pthread_nogc.go +++ b/c/pthread/pthread_nogc.go @@ -19,7 +19,62 @@ package pthread -const ( - LLGoFiles = "_pthread/pthread_nogc.c" - LLGoPackage = "link" +import ( + _ "unsafe" + + "github.com/goplus/llgo/c" ) + +const ( + LLGoPackage = "decl" +) + +// The pthread_create() function starts a new thread in the calling +// process. The new thread starts execution by invoking +// start_routine(); arg is passed as the sole argument of +// start_routine(). +// +// The new thread terminates in one of the following ways: +// +// - It calls pthread_exit(3), specifying an exit status value that +// is available to another thread in the same process that calls +// pthread_join(3). +// +// - It returns from start_routine(). This is equivalent to +// calling pthread_exit(3) with the value supplied in the return +// statement. +// +// - It is canceled (see pthread_cancel(3)). +// +// - Any of the threads in the process calls exit(3), or the main +// thread performs a return from main(). This causes the +// termination of all threads in the process. +// +// On success, pthread_create() returns 0; on error, it returns an +// error number, and the contents of *thread are undefined. +// +// See https://man7.org/linux/man-pages/man3/pthread_create.3.html +// +//go:linkname Create C.pthread_create +func Create(pthread *Thread, attr *Attr, routine func(c.Pointer) c.Pointer, arg c.Pointer) c.Int + +// The pthread_join() function waits for the thread specified by +// thread to terminate. If that thread has already terminated, then +// pthread_join() returns immediately. The thread specified by +// thread must be joinable. +// +// If retval is not NULL, then pthread_join() copies the exit status +// of the target thread (i.e., the value that the target thread +// supplied to pthread_exit(3)) into the location pointed to by +// retval. If the target thread was canceled, then PTHREAD_CANCELED +// is placed in the location pointed to by retval. +// +// If multiple threads simultaneously try to join with the same +// thread, the results are undefined. If the thread calling +// pthread_join() is canceled, then the target thread will remain +// joinable (i.e., it will not be detached). +// +// See https://man7.org/linux/man-pages/man3/pthread_join.3.html +// +//go:linkname Join C.pthread_join +func Join(thread Thread, retval *c.Pointer) c.Int diff --git a/c/pthread/sync/_pthd/pthd.c b/c/pthread/sync/_wrap/pthd.c similarity index 100% rename from c/pthread/sync/_pthd/pthd.c rename to c/pthread/sync/_wrap/pthd.c diff --git a/c/pthread/sync/sync.go b/c/pthread/sync/sync.go index b9e45d48..af0480ce 100644 --- a/c/pthread/sync/sync.go +++ b/c/pthread/sync/sync.go @@ -27,7 +27,7 @@ import ( ) const ( - LLGoFiles = "_pthd/pthd.c" + LLGoFiles = "_wrap/pthd.c" LLGoPackage = "link" ) From c0c5c87c29c79b38c9e772ed308ca813123e7eb3 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Thu, 15 Aug 2024 20:47:42 +0800 Subject: [PATCH 2/2] mv _pthread => _wrap --- c/pthread/pthread_gc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c/pthread/pthread_gc.go b/c/pthread/pthread_gc.go index f45f9d14..28b4e87b 100644 --- a/c/pthread/pthread_gc.go +++ b/c/pthread/pthread_gc.go @@ -26,7 +26,7 @@ import ( ) const ( - LLGoFiles = "$(pkg-config --cflags bdw-gc): _pthread/pthread_gc.c" + LLGoFiles = "$(pkg-config --cflags bdw-gc): _wrap/pthread_gc.c" LLGoPackage = "link: $(pkg-config --libs bdw-gc); -lgc" )