Skip to content

Commit

Permalink
fix __xgo_on_gonewproc, see #67
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Apr 16, 2024
1 parent ccc82ab commit 56315be
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 18 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ go mod init demo
package demo_test

import (
"context"
"testing"

"github.com/xhd2015/xgo/runtime/mock"
Expand All @@ -86,7 +85,7 @@ func MyFunc() string {

func TestFuncMock(t *testing.T) {
mock.Patch(MyFunc, func() string {
return "mock func"
return "mock func"
})
text := MyFunc()
if text != "mock func" {
Expand Down
5 changes: 2 additions & 3 deletions README_zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ go mod init demo
package demo_test

import (
"context"
"testing"

"github.com/xhd2015/xgo/runtime/mock"
Expand All @@ -81,8 +80,8 @@ func MyFunc() string {
}

func TestFuncMock(t *testing.T) {
mock.Patch(MyFunc, func() string {
return "mock func"
mock.Patch(MyFunc, func() string {
return "mock func"
})
text := MyFunc()
if text != "mock func" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/xgo/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func patchRuntimeAndCompiler(origGoroot string, goroot string, xgoSrc string, go
}

// runtime
err := patchRuntimeAndTesting(goroot)
err := patchRuntimeAndTesting(goroot, goVersion)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/xgo/patch/runtime_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ __xgo_on_init_finished_callbacks = nil
`

const RuntimeProcGoroutineCreatedPatch = `for _, fn := range __xgo_on_gonewproc_callbacks {
fn(uintptr(unsafe.Pointer(newg)))
fn(uintptr(unsafe.Pointer(xgo_newg)))
}
return newg
`

// added after goroutine exit1
Expand Down
52 changes: 46 additions & 6 deletions cmd/xgo/patch_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ var runtimeFiles = []_FilePath{
timeSleep,
}

func patchRuntimeAndTesting(goroot string) error {
err := patchRuntimeProc(goroot)
func patchRuntimeAndTesting(goroot string, goVersion *goinfo.GoVersion) error {
err := patchRuntimeProc(goroot, goVersion)
if err != nil {
return err
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func addRuntimeFunctions(goroot string, goVersion *goinfo.GoVersion, xgoSrc stri
return true, os.WriteFile(dstFile, content, 0755)
}

func patchRuntimeProc(goroot string) error {
func patchRuntimeProc(goroot string, goVersion *goinfo.GoVersion) error {
procFile := filepath.Join(goroot, filepath.Join(runtimeProc...))
anchors := []string{
"func main() {",
Expand All @@ -148,12 +148,52 @@ func patchRuntimeProc(goroot string) error {
patch.RuntimeProcGoroutineExitPatch,
)

content = replaceContentAfter(content,
procDecl := `func newproc(fn`
newProc := `newg := newproc1(fn, gp, pc)`
if goVersion.Major == 1 && goVersion.Minor <= 17 {
procDecl = `func newproc(siz int32`

Check warning on line 154 in cmd/xgo/patch_runtime.go

View workflow job for this annotation

GitHub Actions / build

"siz" should be "six" or "size".
newProc = `newg := newproc1(fn, argp, siz, gp, pc)`

Check warning on line 155 in cmd/xgo/patch_runtime.go

View workflow job for this annotation

GitHub Actions / build

"siz" should be "six" or "size".
}

// see https://github.com/xhd2015/xgo/issues/67
content = addContentAtIndex(
content,
"/*<begin declare_xgo_newg>*/", "/*<end declare_xgo_newg>*/",
[]string{
procDecl,
`systemstack(func() {`,
newProc,
},
1,
true,
"var xgo_newg *g",
)
content = addContentAtIndex(
content,
"/*<begin set_xgo_newg>*/", "/*<end set_xgo_newg>*/",
[]string{
procDecl,
`systemstack(func() {`,
newProc,
"\n",
},
3,
false,
"xgo_newg = newg",
)

content = addContentAtIndex(content,
"/*<begin add_go_newproc_callback>*/", "/*<end add_go_newproc_callback>*/",
[]string{
"func newproc1(", "*g {",
procDecl,
`systemstack(func() {`,
newProc,
"\n",
"})",
"}",
},
"return newg",
5,
true,
patch.RuntimeProcGoroutineCreatedPatch,
)
return content, nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import "fmt"

const VERSION = "1.0.24"
const REVISION = "26299095b0e070cff5e5daa2d6e59cea598d1b35+1"
const NUMBER = 183
const REVISION = "ccc82ab8c3514f4c1a71039dad9ee7c30109935c+1"
const NUMBER = 184

func getRevision() string {
revSuffix := ""
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.24"
const REVISION = "26299095b0e070cff5e5daa2d6e59cea598d1b35+1"
const NUMBER = 183
const REVISION = "ccc82ab8c3514f4c1a71039dad9ee7c30109935c+1"
const NUMBER = 184

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down

0 comments on commit 56315be

Please sign in to comment.