Skip to content

Commit 42aab4b

Browse files
committed
runtime: M-targeted signals for libc-based OSes
For golang#10958, golang#24543. Change-Id: I82bee63b49e15bd5a53228eb85179814c80437ef Reviewed-on: https://go-review.googlesource.com/c/go/+/201403 Run-TryBot: Austin Clements <austin@google.com> Reviewed-by: Cherry Zhang <cherryyz@google.com>
1 parent 8714e39 commit 42aab4b

File tree

9 files changed

+117
-1
lines changed

9 files changed

+117
-1
lines changed

src/runtime/os2_aix.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ var (
6464
//go:cgo_import_dynamic libpthread_attr_setstackaddr pthread_attr_setstackaddr "libpthread.a/shr_xpg5_64.o"
6565
//go:cgo_import_dynamic libpthread_create pthread_create "libpthread.a/shr_xpg5_64.o"
6666
//go:cgo_import_dynamic libpthread_sigthreadmask sigthreadmask "libpthread.a/shr_xpg5_64.o"
67+
//go:cgo_import_dynamic libpthread_self pthread_self "libpthread.a/shr_xpg5_64.o"
68+
//go:cgo_import_dynamic libpthread_kill pthread_kill "libpthread.a/shr_xpg5_64.o"
6769

6870
//go:linkname libc__Errno libc__Errno
6971
//go:linkname libc_clock_gettime libc_clock_gettime
@@ -101,6 +103,8 @@ var (
101103
//go:linkname libpthread_attr_setstackaddr libpthread_attr_setstackaddr
102104
//go:linkname libpthread_create libpthread_create
103105
//go:linkname libpthread_sigthreadmask libpthread_sigthreadmask
106+
//go:linkname libpthread_self libpthread_self
107+
//go:linkname libpthread_kill libpthread_kill
104108

105109
var (
106110
//libc
@@ -139,7 +143,9 @@ var (
139143
libpthread_attr_setdetachstate,
140144
libpthread_attr_setstackaddr,
141145
libpthread_create,
142-
libpthread_sigthreadmask libFunc
146+
libpthread_sigthreadmask,
147+
libpthread_self,
148+
libpthread_kill libFunc
143149
)
144150

145151
type libFunc uintptr
@@ -724,3 +730,14 @@ func sigprocmask(how int32, new, old *sigset) {
724730
sigprocmask1(uintptr(how), uintptr(unsafe.Pointer(new)), uintptr(unsafe.Pointer(old)))
725731

726732
}
733+
734+
//go:nosplit
735+
func pthread_self() pthread {
736+
r, _ := syscall0(&libpthread_self)
737+
return pthread(r)
738+
}
739+
740+
//go:nosplit
741+
func signalM(mp *m, sig int) {
742+
syscall2(&libpthread_kill, uintptr(pthread(mp.procid)), uintptr(sig))
743+
}

src/runtime/os3_solaris.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
//go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "libc.so"
3030
//go:cgo_import_dynamic libc_pthread_attr_setstack pthread_attr_setstack "libc.so"
3131
//go:cgo_import_dynamic libc_pthread_create pthread_create "libc.so"
32+
//go:cgo_import_dynamic libc_pthread_self pthread_self "libc.so"
33+
//go:cgo_import_dynamic libc_pthread_kill pthread_kill "libc.so"
3234
//go:cgo_import_dynamic libc_raise raise "libc.so"
3335
//go:cgo_import_dynamic libc_read read "libc.so"
3436
//go:cgo_import_dynamic libc_select select "libc.so"
@@ -61,6 +63,8 @@ import (
6163
//go:linkname libc_pthread_attr_setdetachstate libc_pthread_attr_setdetachstate
6264
//go:linkname libc_pthread_attr_setstack libc_pthread_attr_setstack
6365
//go:linkname libc_pthread_create libc_pthread_create
66+
//go:linkname libc_pthread_self libc_pthread_self
67+
//go:linkname libc_pthread_kill libc_pthread_kill
6468
//go:linkname libc_raise libc_raise
6569
//go:linkname libc_read libc_read
6670
//go:linkname libc_select libc_select
@@ -94,6 +98,8 @@ var (
9498
libc_pthread_attr_setdetachstate,
9599
libc_pthread_attr_setstack,
96100
libc_pthread_create,
101+
libc_pthread_self,
102+
libc_pthread_kill,
97103
libc_raise,
98104
libc_read,
99105
libc_sched_yield,
@@ -214,6 +220,8 @@ func minit() {
214220
asmcgocall(unsafe.Pointer(funcPC(miniterrno)), unsafe.Pointer(&libc____errno))
215221

216222
minitSignals()
223+
224+
getg().m.procid = uint64(pthread_self())
217225
}
218226

219227
// Called from dropm to undo the effect of an minit.
@@ -434,6 +442,14 @@ func pthread_create(thread *pthread, attr *pthreadattr, fn uintptr, arg unsafe.P
434442
return int32(sysvicall4(&libc_pthread_create, uintptr(unsafe.Pointer(thread)), uintptr(unsafe.Pointer(attr)), uintptr(fn), uintptr(arg)))
435443
}
436444

445+
func pthread_self() pthread {
446+
return pthread(sysvicall0(&libc_pthread_self))
447+
}
448+
449+
func signalM(mp *m, sig int) {
450+
sysvicall2(&libc_pthread_kill, uintptr(pthread(mp.procid)), uintptr(sig))
451+
}
452+
437453
//go:nosplit
438454
//go:nowritebarrierrec
439455
func raise(sig uint32) /* int32 */ {

src/runtime/os_aix.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func miniterrno() {
175175
func minit() {
176176
miniterrno()
177177
minitSignals()
178+
getg().m.procid = uint64(pthread_self())
178179
}
179180

180181
func unminit() {

src/runtime/os_darwin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ func minit() {
295295
minitSignalStack()
296296
}
297297
minitSignalMask()
298+
getg().m.procid = uint64(pthread_self())
298299
}
299300

300301
// Called from dropm to undo the effect of an minit.
@@ -406,3 +407,7 @@ func sysargs(argc int32, argv **byte) {
406407
executablePath = executablePath[len(prefix):]
407408
}
408409
}
410+
411+
func signalM(mp *m, sig int) {
412+
pthread_kill(pthread(mp.procid), uint32(sig))
413+
}

src/runtime/sys_darwin.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ func pthread_self() (t pthread) {
162162
}
163163
func pthread_self_trampoline()
164164

165+
//go:nosplit
166+
//go:cgo_unsafe_args
167+
func pthread_kill(t pthread, sig uint32) {
168+
libcCall(unsafe.Pointer(funcPC(pthread_kill_trampoline)), unsafe.Pointer(&t))
169+
return
170+
}
171+
func pthread_kill_trampoline()
172+
165173
func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (unsafe.Pointer, int) {
166174
args := struct {
167175
addr unsafe.Pointer
@@ -415,6 +423,8 @@ func setNonblock(fd int32) {
415423
//go:cgo_import_dynamic libc_pthread_attr_getstacksize pthread_attr_getstacksize "/usr/lib/libSystem.B.dylib"
416424
//go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "/usr/lib/libSystem.B.dylib"
417425
//go:cgo_import_dynamic libc_pthread_create pthread_create "/usr/lib/libSystem.B.dylib"
426+
//go:cgo_import_dynamic libc_pthread_self pthread_self "/usr/lib/libSystem.B.dylib"
427+
//go:cgo_import_dynamic libc_pthread_kill pthread_kill "/usr/lib/libSystem.B.dylib"
418428
//go:cgo_import_dynamic libc_exit exit "/usr/lib/libSystem.B.dylib"
419429
//go:cgo_import_dynamic libc_raise raise "/usr/lib/libSystem.B.dylib"
420430

src/runtime/sys_darwin_386.s

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,31 @@ TEXT runtime·pthread_cond_signal_trampoline(SB),NOSPLIT,$0
653653
POPL BP
654654
RET
655655

656+
TEXT runtime·pthread_self_trampoline(SB),NOSPLIT,$0
657+
PUSHL BP
658+
MOVL SP, BP
659+
NOP SP // hide SP from vet
660+
CALL libc_pthread_self(SB)
661+
MOVL 8(SP), CX
662+
MOVL AX, 0(CX) // return value
663+
MOVL BP, SP
664+
POPL BP
665+
RET
666+
667+
TEXT runtime·pthread_kill_trampoline(SB),NOSPLIT,$0
668+
PUSHL BP
669+
MOVL SP, BP
670+
SUBL $8, SP
671+
MOVL 16(SP), CX
672+
MOVL 0(CX), AX // arg 1 thread
673+
MOVL AX, 0(SP)
674+
MOVL 4(CX), AX // arg 2 sig
675+
MOVL AX, 4(SP)
676+
CALL libc_pthread_kill(SB)
677+
MOVL BP, SP
678+
POPL BP
679+
RET
680+
656681
// syscall calls a function in libc on behalf of the syscall package.
657682
// syscall takes a pointer to a struct like:
658683
// struct {

src/runtime/sys_darwin_amd64.s

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,24 @@ TEXT runtime·pthread_cond_signal_trampoline(SB),NOSPLIT,$0
566566
POPQ BP
567567
RET
568568

569+
TEXT runtime·pthread_self_trampoline(SB),NOSPLIT,$0
570+
PUSHQ BP
571+
MOVQ SP, BP
572+
MOVQ DI, BX // BX is caller-save
573+
CALL libc_pthread_self(SB)
574+
MOVQ AX, 0(BX) // return value
575+
POPQ BP
576+
RET
577+
578+
TEXT runtime·pthread_kill_trampoline(SB),NOSPLIT,$0
579+
PUSHQ BP
580+
MOVQ SP, BP
581+
MOVQ 8(DI), SI // arg 2 sig
582+
MOVQ 0(DI), DI // arg 1 thread
583+
CALL libc_pthread_kill(SB)
584+
POPQ BP
585+
RET
586+
569587
// syscall calls a function in libc on behalf of the syscall package.
570588
// syscall takes a pointer to a struct like:
571589
// struct {

src/runtime/sys_darwin_arm.s

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,18 @@ TEXT runtime·pthread_cond_signal_trampoline(SB),NOSPLIT,$0
405405
BL libc_pthread_cond_signal(SB)
406406
RET
407407

408+
TEXT runtime·pthread_self_trampoline(SB),NOSPLIT,$0
409+
MOVW R0, R4 // R4 is callee-save
410+
BL libc_pthread_self(SB)
411+
MOVW R0, 0(R4) // return value
412+
RET
413+
414+
TEXT runtime·pthread_kill_trampoline(SB),NOSPLIT,$0
415+
MOVW 4(R0), R1 // arg 2 sig
416+
MOVW 0(R0), R0 // arg 1 thread
417+
BL libc_pthread_kill(SB)
418+
RET
419+
408420
// syscall calls a function in libc on behalf of the syscall package.
409421
// syscall takes a pointer to a struct like:
410422
// struct {

src/runtime/sys_darwin_arm64.s

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,18 @@ TEXT runtime·pthread_cond_signal_trampoline(SB),NOSPLIT,$0
471471
BL libc_pthread_cond_signal(SB)
472472
RET
473473

474+
TEXT runtime·pthread_self_trampoline(SB),NOSPLIT,$0
475+
MOVD R0, R19 // R19 is callee-save
476+
BL libc_pthread_self(SB)
477+
MOVD R0, 0(R19) // return value
478+
RET
479+
480+
TEXT runtime·pthread_kill_trampoline(SB),NOSPLIT,$0
481+
MOVD 8(R0), R1 // arg 2 sig
482+
MOVD 0(R0), R0 // arg 1 thread
483+
BL libc_pthread_kill(SB)
484+
RET
485+
474486
// syscall calls a function in libc on behalf of the syscall package.
475487
// syscall takes a pointer to a struct like:
476488
// struct {

0 commit comments

Comments
 (0)