1
0
mirror of https://github.com/golang/go synced 2024-11-22 19:05:01 -07:00
go/misc/cgo/test
doujiang24 ef0dedce87 runtime/cgo: store M for C-created thread in pthread key
In a C thread, it's necessary to acquire an extra M by using needm while invoking a Go function from C. But, needm and dropm are heavy costs due to the signal-related syscalls.
So, we change to not dropm while returning back to C, which means binding the extra M to the C thread until it exits, to avoid needm and dropm on each C to Go call.
Instead, we only dropm while the C thread exits, so the extra M won't leak.

When invoking a Go function from C:
Allocate a pthread variable using pthread_key_create, only once per shared object, and register a thread-exit-time destructor.
And store the g0 of the current m into the thread-specified value of the pthread key,  only once per C thread, so that the destructor will put the extra M back onto the extra M list while the C thread exits.

When returning back to C:
Skip dropm in cgocallback, when the pthread variable has been created, so that the extra M will be reused the next time invoke a Go function from C.

This is purely a performance optimization. The old version, in which needm & dropm happen on each cgo call, is still correct too, and we have to keep the old version on systems with cgo but without pthreads, like Windows.

This optimization is significant, and the specific value depends on the OS system and CPU, but in general, it can be considered as 10x faster, for a simple Go function call from a C thread.

For the newly added BenchmarkCGoInCThread, some benchmark results:
1. it's 28x faster, from 3395 ns/op to 121 ns/op, in darwin OS & Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
2. it's 6.5x faster, from 1495 ns/op to 230 ns/op, in Linux OS & Intel(R) Xeon(R) CPU E5-2630 0 @ 2.30GHz

Fixes #51676

Change-Id: I380702fe2f9b6b401b2d6f04b0aba990f4b9ee6c
GitHub-Last-Rev: 93dc64ad98
GitHub-Pull-Request: golang/go#51679
Reviewed-on: https://go-review.googlesource.com/c/go/+/392854
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: thepudds <thepudds1460@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
2023-03-24 16:00:24 +00:00
..
gcc68255 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue8756 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue8828 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue9026 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue9400 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue9510a misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue9510b misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue20266 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue23555a misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue23555b misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue24161arg misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue24161e0 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue24161e1 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue24161e2 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue24161res misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue26213 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue26430 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue26743 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue27054 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue27340 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue29563 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue30527 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue41761a misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue43639 misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue52611a misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue52611b misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
backdoor.go
buildid_linux.go
callback_c_gc.c
callback_c_gccgo.c
callback_c.c
callback.go
cgo_linux_test.go misc/cgo/test: skip Test9400 on Alpine 2022-11-07 21:15:29 +00:00
cgo_stubs_android_test.go
cgo_test.go runtime/cgo: store M for C-created thread in pthread key 2023-03-24 16:00:24 +00:00
cgo_thread_lock.go
cgo_unix_test.go
cthread_unix.c runtime/cgo: store M for C-created thread in pthread key 2023-03-24 16:00:24 +00:00
cthread_windows.c runtime/cgo: store M for C-created thread in pthread key 2023-03-24 16:00:24 +00:00
gcc68255.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue1435.go
issue4029.c
issue4029.go
issue4029w.go
issue4273.c
issue4273b.c
issue4339.c
issue4339.h
issue5548_c.c
issue5740a.c
issue5740b.c
issue6833_c.c
issue6907export_c.c
issue6997_linux.c
issue6997_linux.go
issue7234_test.go
issue8148.c
issue8148.go
issue8331.h
issue8517_windows.c
issue8517_windows.go
issue8517.go
issue8694.go
issue8756.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue8811.c
issue8828.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue9026.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue9400_linux.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue9510.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue18146.go
issue20266.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue20910.c
issue21897.go
issue21897b.go
issue23555.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue24161_darwin_test.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue26430.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue26743.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue27340.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue29563.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue30527.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue31891.c
issue41761.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue42018_windows.go
issue42018.go
issue42495.go
issue43639.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
issue52611.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
setgid2_linux.go
setgid_linux.go
sigaltstack.go
sigprocmask.c
sigprocmask.go
test26213.go misc/cgo/test: simplify for module mode 2022-11-04 20:47:59 +00:00
test_unix.go
test_windows.go
test.go cmd/cgo: walk {FuncType,TypeSpec}.TypeParams fields 2022-11-30 21:45:10 +00:00
testx.c
testx.go runtime/cgo: store M for C-created thread in pthread key 2023-03-24 16:00:24 +00:00
typeparam.go