Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout scm
uses: actions/checkout@v4
uses: actions/checkout@v5

- name: Set up Go
uses: actions/setup-go@v5
Expand All @@ -25,14 +25,14 @@ jobs:
cache: false

- name: Lint
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v8

CodeQL:
needs: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout scm
uses: actions/checkout@v4
uses: actions/checkout@v5

- name: Set up Go
uses: actions/setup-go@v5
Expand Down Expand Up @@ -191,17 +191,14 @@ jobs:
runs-on: ubuntu-latest
- os: windows
runs-on: windows-latest
- os: windows
go: 1.18
runs-on: windows-2019
- os: freebsd
runs-on: ubuntu-latest
- os: js
runs-on: ubuntu-latest

steps:
- name: Checkout scm
uses: actions/checkout@v4
uses: actions/checkout@v5

- name: Set up Go
uses: actions/setup-go@v5
Expand Down Expand Up @@ -300,6 +297,14 @@ jobs:
GOARCH: ${{ matrix.arch }}
run: go test -v -coverprofile='coverage.txt' -covermode=atomic ./...

- name: 'Downgrade gcc for go1.18 on [windows] arch [amd64]'
if: ${{ matrix.os == 'windows' && contains(fromJson('["amd64"]'), matrix.arch) && matrix.go == '1.18' }}
run: |
Invoke-WebRequest -Uri 'https://jaist.dl.sourceforge.net/project/mingw-w64/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-win32/sjlj/x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0.7z?viasf=1' -OutFile gcc8.7z
Remove-Item 'C:\mingw64' -Recurse -Force
7z x gcc8.7z -oC:\ -y
gcc --version

- name: 'Test on [windows] arch [amd64]'
if: ${{ matrix.os == 'windows' && contains(fromJson('["amd64"]'), matrix.arch) }}
env:
Expand Down
19 changes: 12 additions & 7 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout scm
uses: actions/checkout@v4
uses: actions/checkout@v5

- name: Set up Go
uses: actions/setup-go@v5
Expand All @@ -25,14 +25,14 @@ jobs:
cache: false

- name: Lint
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v8

CodeQL:
needs: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout scm
uses: actions/checkout@v4
uses: actions/checkout@v5

- name: Set up Go
uses: actions/setup-go@v5
Expand Down Expand Up @@ -191,17 +191,14 @@ jobs:
runs-on: ubuntu-latest
- os: windows
runs-on: windows-latest
- os: windows
go: 1.18
runs-on: windows-2019
- os: freebsd
runs-on: ubuntu-latest
- os: js
runs-on: ubuntu-latest

steps:
- name: Checkout scm
uses: actions/checkout@v4
uses: actions/checkout@v5

- name: Set up Go
uses: actions/setup-go@v5
Expand Down Expand Up @@ -304,6 +301,14 @@ jobs:
GOARCH: ${{ matrix.arch }}
run: go test -v -coverprofile='coverage.txt' -covermode=atomic -a -toolexec='routinex -v' ./...

- name: 'Downgrade gcc for go1.18 on [windows] arch [amd64]'
if: ${{ matrix.os == 'windows' && contains(fromJson('["amd64"]'), matrix.arch) && matrix.go == '1.18' }}
run: |
Invoke-WebRequest -Uri 'https://jaist.dl.sourceforge.net/project/mingw-w64/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-win32/sjlj/x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0.7z?viasf=1' -OutFile gcc8.7z
Remove-Item 'C:\mingw64' -Recurse -Force
7z x gcc8.7z -oC:\ -y
gcc --version

- name: 'Test on [windows] arch [amd64]'
if: ${{ matrix.os == 'windows' && contains(fromJson('["amd64"]'), matrix.arch) }}
env:
Expand Down
60 changes: 30 additions & 30 deletions api_routine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestWrapTask_HasContext(t *testing.T) {
}

func TestWrapTask_Complete_ThenFail(t *testing.T) {
tracker := NewFileTracker(&os.Stdout)
tracker := NewFileTracker(os.Stdout)
tracker.Begin()
defer tracker.End()
//
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestWrapWaitTask_HasContext_Cancel(t *testing.T) {
}

func TestWrapWaitTask_Complete_ThenFail(t *testing.T) {
tracker := NewFileTracker(&os.Stdout)
tracker := NewFileTracker(os.Stdout)
tracker.Begin()
defer tracker.End()
//
Expand Down Expand Up @@ -553,7 +553,7 @@ func TestWrapWaitResultTask_HasContext_Cancel(t *testing.T) {
}

func TestWrapWaitResultTask_Complete_ThenFail(t *testing.T) {
tracker := NewFileTracker(&os.Stdout)
tracker := NewFileTracker(os.Stdout)
tracker.Begin()
defer tracker.End()
//
Expand Down Expand Up @@ -587,7 +587,7 @@ func TestWrapWaitResultTask_Complete_ThenFail(t *testing.T) {
}

func TestGo_Error(t *testing.T) {
tracker := NewFileTracker(&os.Stdout)
tracker := NewFileTracker(os.Stdout)
tracker.Begin()
defer tracker.End()
//
Expand Down Expand Up @@ -617,11 +617,7 @@ func TestGo_Error(t *testing.T) {
//
line = lines[2]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.inheritedTask.run()"))
if routinexEnabled {
assert.True(t, strings.HasSuffix(line, "routine.go:53"))
} else {
assert.True(t, strings.HasSuffix(line, "routine.go:45"))
}
assert.True(t, strings.HasSuffix(line, "routine.go:24"))
//
line = lines[3]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.(*futureTask[...]).Run()"))
Expand Down Expand Up @@ -727,15 +723,11 @@ func TestGoWait_Error(t *testing.T) {
//
line = lines[1]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestGoWait_Error."))
assert.True(t, strings.HasSuffix(line, "api_routine_test.go:710"))
assert.True(t, strings.HasSuffix(line, "api_routine_test.go:706"))
//
line = lines[2]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.inheritedWaitTask.run()"))
if routinexEnabled {
assert.True(t, strings.HasSuffix(line, "routine.go:85"))
} else {
assert.True(t, strings.HasSuffix(line, "routine.go:77"))
}
assert.True(t, strings.HasSuffix(line, "routine.go:44"))
//
line = lines[3]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.(*futureTask[...]).Run()"))
Expand Down Expand Up @@ -833,15 +825,11 @@ func TestGoWaitResult_Error(t *testing.T) {
//
line = lines[1]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestGoWaitResult_Error."))
assert.True(t, strings.HasSuffix(line, "api_routine_test.go:814"))
assert.True(t, strings.HasSuffix(line, "api_routine_test.go:806"))
//
line = lines[2]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.inheritedWaitResultTask[...].run()"))
if routinexEnabled {
assert.True(t, strings.HasSuffix(line, "routine.go:116"))
} else {
assert.True(t, strings.HasSuffix(line, "routine.go:109"))
}
assert.True(t, strings.HasSuffix(line, "routine.go:64"))
//
lineOffset := 0
if len(lines) == 7 {
Expand Down Expand Up @@ -927,12 +915,12 @@ func TestGoWaitResult_Cross(t *testing.T) {
//===

type FileTracker struct {
target **os.File
oldValue *os.File
tempValue *os.File
target *os.File
oldValue os.File
tempValue os.File
}

func NewFileTracker(target **os.File) *FileTracker {
func NewFileTracker(target *os.File) *FileTracker {
return &FileTracker{target: target, oldValue: *target}
}

Expand All @@ -941,8 +929,8 @@ func (f *FileTracker) Begin() {
if err != nil {
panic(err)
}
*f.target = file
f.tempValue = file
*f.target = *file
f.tempValue = *file
}

func (f *FileTracker) End() {
Expand All @@ -959,7 +947,7 @@ func (f *FileTracker) Value() string {
if _, err := f.tempValue.Seek(0, io.SeekStart); err != nil {
panic(err)
}
buff, err := io.ReadAll(f.tempValue)
buff, err := io.ReadAll(&f.tempValue)
if err != nil {
panic(err)
}
Expand All @@ -968,10 +956,22 @@ func (f *FileTracker) Value() string {

func TestFileTracker(t *testing.T) {
origin := os.Stdout
tracker := NewFileTracker(&os.Stdout)
tracker := NewFileTracker(os.Stdout)
tracker.Begin()
_, _ = fmt.Fprintln(os.Stdout, "hello world")
assert.Equal(t, "hello world\n", tracker.Value())
tracker.End()
assert.Equal(t, "/dev/stdout", origin.Name())
assert.Same(t, origin, os.Stdout)
}

func TestFileTrackerRef(t *testing.T) {
origin := os.Stdout
tracker := NewFileTracker(os.Stdout)
tracker.Begin()
fmt.Println("hello world")
_, _ = fmt.Fprintln(origin, "hello world")
assert.Equal(t, "hello world\n", tracker.Value())
tracker.End()
assert.Equal(t, "/dev/stdout", origin.Name())
assert.Same(t, origin, os.Stdout)
}
2 changes: 1 addition & 1 deletion g/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func typeByString(str string) reflect.Type {
h := int(uint(i+j) >> 1) // avoid overflow when computing h
// i ≤ h < j
face.data = resolveTypeOff(section, offs[h])
if !(typ.String() >= s) {
if !(typ.String() >= s) { //nolint:staticcheck
i = h + 1 // preserves f(i-1) == false
} else {
j = h // preserves f(j) == true
Expand Down
75 changes: 11 additions & 64 deletions routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,7 @@ package routine

import "fmt"

type inherited struct {
}

//go:norace
func (inherited) reset() {
t := currentThread(false)
if t != nil {
t.threadLocals = nil
t.inheritableThreadLocals = nil
}
}

//go:norace
func (inherited) restore(t *thread, threadLocalsBackup, inheritableThreadLocalsBackup *threadLocalMap) {
t.threadLocals = threadLocalsBackup
t.inheritableThreadLocals = inheritableThreadLocalsBackup
}

type inheritedTask struct {
inherited
context *threadLocalMap
function Runnable
}
Expand All @@ -38,25 +19,13 @@ func (it inheritedTask) run(task FutureTask[any]) any {
}
}()
// restore
t := currentThread(it.context != nil)
if t == nil {
//copied is nil
defer it.reset()
it.function()
return nil
} else {
threadLocalsBackup := t.threadLocals
inheritableThreadLocalsBackup := t.inheritableThreadLocals
defer it.restore(t, threadLocalsBackup, inheritableThreadLocalsBackup)
t.threadLocals = nil
t.inheritableThreadLocals = it.context
it.function()
return nil
}
defer restoreInheritedMap(it.context)()
// exec
it.function()
return nil
}

type inheritedWaitTask struct {
inherited
context *threadLocalMap
function CancelRunnable
}
Expand All @@ -70,25 +39,13 @@ func (iwt inheritedWaitTask) run(task FutureTask[any]) any {
}
}()
// restore
t := currentThread(iwt.context != nil)
if t == nil {
//copied is nil
defer iwt.reset()
iwt.function(task)
return nil
} else {
threadLocalsBackup := t.threadLocals
inheritableThreadLocalsBackup := t.inheritableThreadLocals
defer iwt.restore(t, threadLocalsBackup, inheritableThreadLocalsBackup)
t.threadLocals = nil
t.inheritableThreadLocals = iwt.context
iwt.function(task)
return nil
}
defer restoreInheritedMap(iwt.context)()
// exec
iwt.function(task)
return nil
}

type inheritedWaitResultTask[TResult any] struct {
inherited
context *threadLocalMap
function CancelCallable[TResult]
}
Expand All @@ -102,17 +59,7 @@ func (iwrt inheritedWaitResultTask[TResult]) run(task FutureTask[TResult]) TResu
}
}()
// restore
t := currentThread(iwrt.context != nil)
if t == nil {
//copied is nil
defer iwrt.reset()
return iwrt.function(task)
} else {
threadLocalsBackup := t.threadLocals
inheritableThreadLocalsBackup := t.inheritableThreadLocals
defer iwrt.restore(t, threadLocalsBackup, inheritableThreadLocalsBackup)
t.threadLocals = nil
t.inheritableThreadLocals = iwrt.context
return iwrt.function(task)
}
defer restoreInheritedMap(iwrt.context)()
// exec
return iwrt.function(task)
}
Loading
Loading