-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tinygo support, goroutine hack change, fix panic for invalid stderr, …
…add test (#66) * Fix panic for invalid stderr, add test * auto switch to stdout for wasm - manual tested * Throw the towel on goroutine, use the original directly, maintained for changes in linkname * add alternative used to benchmark * happy linters, happy life * Add comment about the mutex * review comments, thanks @ccoVeille for the thorough look * Update logger_test.go * Update logger_test.go indicate why we don't just use io.Discard, some more cc @ccoVeille * increase coverage where possible, ie not in init() * Apply suggestions from code review Co-authored-by: ccoVeille <[email protected]> * more explanations of --------- Co-authored-by: ccoVeille <[email protected]>
- Loading branch information
Showing
8 changed files
with
101 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
fortio.org/struct2env v0.4.1 h1:rJludAMO5eBvpWplWEQNqoVDFZr4RWMQX7RUapgZyc0= | ||
fortio.org/struct2env v0.4.1/go.mod h1:lENUe70UwA1zDUCX+8AsO663QCFqYaprk5lnPhjD410= | ||
github.com/kortschak/goroutine v1.1.2 h1:lhllcCuERxMIK5cYr8yohZZScL1na+JM5JYPRclWjck= | ||
github.com/kortschak/goroutine v1.1.2/go.mod h1:zKpXs1FWN/6mXasDQzfl7g0LrGFIOiA6cLs9eXKyaMY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,14 @@ | ||
// Copyright ©2020 Dan Kortschak. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
//go:build !tinygo | ||
|
||
// Package goroutine provides a single function that will return the runtime's | ||
// ID number for the calling goroutine. | ||
// | ||
// The implementation is derived from Laevus Dexter's comment in Gophers' Slack #darkarts, | ||
// https://gophers.slack.com/archives/C1C1YSQBT/p1593885226448300 post which linked to | ||
// this playground snippet https://play.golang.org/p/CSOp9wyzydP. | ||
package goroutine | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
"github.com/kortschak/goroutine" // Rely on and forward to the original rather than maintain our own copy. | ||
) | ||
|
||
const IsTinyGo = false | ||
|
||
// ID returns the runtime ID of the calling goroutine. | ||
func ID() int64 { | ||
return *(*int64)(add(getg(), goidoff)) | ||
} | ||
|
||
//go:nosplit | ||
func getg() unsafe.Pointer { | ||
return *(*unsafe.Pointer)(add(getm(), curgoff)) | ||
return goroutine.ID() | ||
} | ||
|
||
//go:linkname add runtime.add | ||
//go:nosplit | ||
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer | ||
|
||
//go:linkname getm runtime.getm | ||
//go:nosplit | ||
func getm() unsafe.Pointer | ||
|
||
var ( | ||
curgoff = offset("*runtime.m", "curg") | ||
goidoff = offset("*runtime.g", "goid") | ||
) | ||
|
||
// offset returns the offset into typ for the given field. | ||
func offset(typ, field string) uintptr { | ||
rt := toType(typesByString(typ)[0]) | ||
f, _ := rt.Elem().FieldByName(field) | ||
return f.Offset | ||
} | ||
|
||
//go:linkname typesByString reflect.typesByString | ||
func typesByString(s string) []unsafe.Pointer | ||
|
||
//go:linkname toType reflect.toType | ||
func toType(t unsafe.Pointer) reflect.Type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//go:build tinygo | ||
|
||
package goroutine | ||
|
||
import ( | ||
"sync" | ||
"unsafe" | ||
) | ||
|
||
const IsTinyGo = true | ||
|
||
var ( | ||
counter int64 | ||
mapping = make(map[uintptr]int64) | ||
// TinyGo at the moment is single threaded, so this is not needed, but it's good to have anyway | ||
// in case that changes. It does add ~5ns (from 20ns vs 4ns big go) but it's better to be correct. | ||
// In theory, the mutex could be noop on platforms where everything is single threaded. | ||
lock sync.Mutex | ||
) | ||
|
||
func ID() int64 { | ||
task := uintptr(currentTask()) | ||
lock.Lock() // explicit minimal critical section without using defer, on purpose. | ||
if id, ok := mapping[task]; ok { | ||
lock.Unlock() | ||
return id | ||
} | ||
counter++ | ||
mapping[task] = counter | ||
lock.Unlock() | ||
return counter | ||
// or, super fast but ugly large numbers/pointers: | ||
//return int64(uintptr(currentTask())) | ||
} | ||
|
||
// Call https://github.com/tinygo-org/tinygo/blob/v0.32.0/src/internal/task/task_stack.go#L39 | ||
// | ||
//go:linkname currentTask internal/task.Current | ||
func currentTask() unsafe.Pointer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters