]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: implement darwin raise with pthread_self and pthread_kill
authorElias Naur <elias.naur@gmail.com>
Mon, 30 Apr 2018 23:21:02 +0000 (01:21 +0200)
committerElias Naur <elias.naur@gmail.com>
Tue, 1 May 2018 00:37:36 +0000 (00:37 +0000)
Convert raise from raw syscalls to using the system pthread library.
As a bonus, raise will now target the current thread instead of the
process.

Updates #17490

Change-Id: I2e44f2000bf870e99a5b4dc5ff5e0799fba91bde
Reviewed-on: https://go-review.googlesource.com/110475
Run-TryBot: Elias Naur <elias.naur@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/runtime/os_darwin.go
src/runtime/sys_darwin.go
src/runtime/sys_darwin_386.s
src/runtime/sys_darwin_amd64.s

index 067b7debfa6b08b695565b0e9adf6eb76645d7b9..613725115d5e752ab310d709b919470d50b182df 100644 (file)
@@ -540,7 +540,12 @@ func sigtramp(fn uintptr, infostyle, sig uint32, info *siginfo, ctx unsafe.Point
 //go:noescape
 func setitimer(mode int32, new, old *itimerval)
 
-func raise(sig uint32)
+//go:nosplit
+func raise(sig uint32) {
+       tid := pthread_self()
+       pthread_kill(tid, int(sig))
+}
+
 func raiseproc(sig uint32)
 
 //extern SigTabTT runtime·sigtab[];
index afb2afcccaffd8134d9c1aa9e7e8d5e06579e7ae..35a9bd6f30c3238ca14b86db331ee458d15b6673 100644 (file)
@@ -53,6 +53,28 @@ func pthread_create(attr *pthreadattr, start uintptr, arg unsafe.Pointer) (t pth
 //go:noescape
 func pthread_create_trampoline(t *pthread, attr *pthreadattr, start uintptr, arg unsafe.Pointer) int32
 
+//go:nowritebarrier
+func pthread_kill(thread pthread, sig int) (errno int32) {
+       systemstack(func() {
+               errno = pthread_kill_trampoline(thread, sig)
+       })
+       return
+}
+
+//go:noescape
+func pthread_kill_trampoline(thread pthread, sig int) int32
+
+//go:nowritebarrier
+func pthread_self() (t pthread) {
+       systemstack(func() {
+               t = pthread_self_trampoline()
+       })
+       return
+}
+
+//go:noescape
+func pthread_self_trampoline() pthread
+
 // Tell the linker that the libc_* functions are to be found
 // in a system library, with the libc_ prefix missing.
 
@@ -61,6 +83,8 @@ func pthread_create_trampoline(t *pthread, attr *pthreadattr, start uintptr, arg
 //go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "/usr/lib/libSystem.B.dylib"
 //go:cgo_import_dynamic libc_pthread_create pthread_create "/usr/lib/libSystem.B.dylib"
 //go:cgo_import_dynamic libc_exit exit "/usr/lib/libSystem.B.dylib"
+//go:cgo_import_dynamic libc_pthread_kill pthread_kill "/usr/lib/libSystem.B.dylib"
+//go:cgo_import_dynamic libc_pthread_self pthread_self "/usr/lib/libSystem.B.dylib"
 
 // Magic incantation to get libSystem actually dynamically linked.
 // TODO: Why does the code require this?  See cmd/compile/internal/ld/go.go:210
index 319bcfc43c53bf4c37a17d53ff45efd9b1ce879a..07844a7eb1746f997f33cee1bb150d17dcd270a8 100644 (file)
@@ -61,11 +61,6 @@ TEXT runtime·write(SB),NOSPLIT,$0
        MOVL    AX, ret+12(FP)
        RET
 
-TEXT runtime·raise(SB),NOSPLIT,$0
-       // Ideally we'd send the signal to the current thread,
-       // not the whole process, but that's too hard on OS X.
-       JMP     runtime·raiseproc(SB)
-
 TEXT runtime·raiseproc(SB),NOSPLIT,$16
        MOVL    $20, AX // getpid
        INT     $0x80
@@ -575,3 +570,36 @@ TEXT runtime·pthread_create_trampoline(SB),NOSPLIT,$0-20
 
        MOVL    AX, ret+16(FP)
        RET
+
+TEXT runtime·pthread_self_trampoline(SB),NOSPLIT,$0-4
+       PUSHL   BP
+       MOVL    SP, BP
+
+       ANDL    $~15, SP
+
+       CALL    libc_pthread_self(SB)
+
+       MOVL    BP, SP
+       POPL    BP
+
+       MOVL    AX, ret+0(FP)
+       RET
+
+TEXT runtime·pthread_kill_trampoline(SB),NOSPLIT,$0-12
+       MOVL    thread+0(FP), AX
+       MOVL    sig+4(FP), CX
+       PUSHL   BP
+       MOVL    SP, BP
+
+       SUBL    $8, SP
+       ANDL    $~15, SP
+
+       MOVL    AX, 0(SP)
+       MOVL    CX, 4(SP)
+       CALL    libc_pthread_kill(SB)
+
+       MOVL    BP, SP
+       POPL    BP
+
+       MOVL    AX, ret+8(FP)
+       RET
index e17de9e035dee5199cc16674dbb08fb421872da1..039a5a613e57faae5ddafa88d7db340198ceb1db 100644 (file)
@@ -74,11 +74,6 @@ TEXT runtime·write(SB),NOSPLIT,$0
        MOVL    AX, ret+24(FP)
        RET
 
-TEXT runtime·raise(SB),NOSPLIT,$0
-       // Ideally we'd send the signal to the current thread,
-       // not the whole process, but that's too hard on OS X.
-       JMP     runtime·raiseproc(SB)
-
 TEXT runtime·raiseproc(SB),NOSPLIT,$24
        MOVL    $(0x2000000+20), AX // getpid
        SYSCALL
@@ -607,3 +602,25 @@ TEXT runtime·pthread_create_trampoline(SB),NOSPLIT,$0-36
        POPQ    BP
        MOVL    AX, ret+32(FP)
        RET
+
+TEXT runtime·pthread_self_trampoline(SB),NOSPLIT,$0-8
+       PUSHQ   BP
+       MOVQ    SP, BP
+       ANDQ    $~15, SP
+       CALL    libc_pthread_self(SB)
+       MOVQ    BP, SP
+       POPQ    BP
+       MOVQ    AX, ret+0(FP)
+       RET
+
+TEXT runtime·pthread_kill_trampoline(SB),NOSPLIT,$0-20
+       MOVQ    thread+0(FP), DI
+       MOVQ    sig+8(FP), SI
+       PUSHQ   BP
+       MOVQ    SP, BP
+       ANDQ    $~15, SP
+       CALL    libc_pthread_kill(SB)
+       MOVQ    BP, SP
+       POPQ    BP
+       MOVL    AX, ret+16(FP)
+       RET