-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
all: improve error message when CGO_ENABLED=0
- Loading branch information
Showing
14 changed files
with
402 additions
and
9 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,5 +1,5 @@ | ||
golang.design/x/mainthread v0.2.1 h1:IUGVW1acDfKoQtFeeS/RD/YYiKK8jxwkJXIQuKuL+ig= | ||
golang.design/x/mainthread v0.2.1/go.mod h1:vYX7cF2b3pTJMGM/hc13NmN6kblKnf4/IyvHeu259L0= | ||
golang.design/x/mainthread v0.3.0 h1:UwFus0lcPodNpMOGoQMe87jSFwbSsEY//CA7yVmu4j8= | ||
golang.design/x/mainthread v0.3.0/go.mod h1:vYX7cF2b3pTJMGM/hc13NmN6kblKnf4/IyvHeu259L0= | ||
golang.org/x/sys v0.0.0-20201022201747-fb209a7c41cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210122093101-04d7465088b8 h1:de2yTH1xuxjmGB7i6Z5o2z3RCHVa0XlpSZzjd8Fe6bE= | ||
golang.org/x/sys v0.0.0-20210122093101-04d7465088b8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
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
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,31 @@ | ||
// Copyright 2021 The golang.design Initiative Authors. | ||
// All rights reserved. Use of this source code is governed | ||
// by a MIT license that can be found in the LICENSE file. | ||
// | ||
// Written by Changkun Ou <changkun.de> | ||
|
||
//go:build !windows && !cgo | ||
|
||
package hotkey | ||
|
||
import "context" | ||
|
||
// Modifier represents a modifier | ||
type Modifier uint32 | ||
|
||
// Key represents a key. | ||
type Key uint8 | ||
|
||
func (hk *Hotkey) register() error { | ||
panic("hotkey: cannot use when CGO_ENABLED=0") | ||
} | ||
|
||
// unregister deregisteres a system hotkey. | ||
func (hk *Hotkey) unregister() { | ||
panic("hotkey: cannot use when CGO_ENABLED=0") | ||
} | ||
|
||
// handle handles the hotkey event loop. | ||
func (hk *Hotkey) handle(ctx context.Context) { | ||
panic("hotkey: cannot use when CGO_ENABLED=0") | ||
} |
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,29 @@ | ||
// Copyright 2021 The golang.design Initiative Authors. | ||
// All rights reserved. Use of this source code is governed | ||
// by a MIT license that can be found in the LICENSE file. | ||
// | ||
// Written by Changkun Ou <changkun.de> | ||
|
||
//go:build (linux || darwin) && !cgo | ||
|
||
package hotkey_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.design/x/hotkey" | ||
) | ||
|
||
// TestHotkey should always run success. | ||
// This is a test to run and for manually testing, registered combination: | ||
// Ctrl+Alt+A (Ctrl+Mod2+Mod4+A on Linux) | ||
func TestHotkey(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
return | ||
} | ||
t.Fatalf("expect to fail when CGO_ENABLED=0") | ||
}() | ||
|
||
hotkey.Register([]hotkey.Modifier{}, hotkey.Key(0)) | ||
} |
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,15 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Changkun Ou <contact@changkun.de> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,106 @@ | ||
# mainthread [![PkgGoDev](https://pkg.go.dev/badge/golang.design/x/mainthread)](https://pkg.go.dev/golang.design/x/mainthread) ![mainthread](https://github.com/golang-design/mainthread/workflows/mainthread/badge.svg?branch=main) ![](https://changkun.de/urlstat?mode=github&repo=golang-design/mainthread) | ||
|
||
schedule functions to run on the main thread | ||
|
||
```go | ||
import "golang.design/x/mainthread" | ||
``` | ||
|
||
## Features | ||
|
||
- Main thread scheduling | ||
- Schedule functions without memory allocation | ||
|
||
## API Usage | ||
|
||
Package mainthread offers facilities to schedule functions | ||
on the main thread. To use this package properly, one must | ||
call `mainthread.Init` from the main package. For example: | ||
|
||
```go | ||
package main | ||
|
||
import "golang.design/x/mainthread" | ||
|
||
func main() { mainthread.Init(fn) } | ||
|
||
// fn is the actual main function | ||
func fn() { | ||
// ... do stuff ... | ||
|
||
// mainthread.Call returns when f1 returns. Note that if f1 blocks | ||
// it will also block the execution of any subsequent calls on the | ||
// main thread. | ||
mainthread.Call(f1) | ||
|
||
// ... do stuff ... | ||
|
||
|
||
// mainthread.Go returns immediately and f2 is scheduled to be | ||
// executed in the future. | ||
mainthread.Go(f2) | ||
|
||
// ... do stuff ... | ||
} | ||
|
||
func f1() { ... } | ||
func f2() { ... } | ||
``` | ||
|
||
If the given function triggers a panic, and called via `mainthread.Call`, | ||
then the panic will be propagated to the same goroutine. One can capture | ||
that panic, when possible: | ||
|
||
```go | ||
defer func() { | ||
if r := recover(); r != nil { | ||
println(r) | ||
} | ||
}() | ||
|
||
mainthread.Call(func() { ... }) // if panic | ||
``` | ||
|
||
If the given function triggers a panic, and called via `mainthread.Go`, | ||
then the panic will be cached internally, until a call to the `Error()` method: | ||
|
||
```go | ||
mainthread.Go(func() { ... }) // if panics | ||
|
||
// ... do stuff ... | ||
|
||
if err := mainthread.Error(); err != nil { // can be captured here. | ||
println(err) | ||
} | ||
``` | ||
|
||
Note that a panic happens before `mainthread.Error()` returning the | ||
panicked error. If one needs to guarantee `mainthread.Error()` indeed | ||
captured the panic, a dummy function can be used as synchornization: | ||
|
||
```go | ||
mainthread.Go(func() { panic("die") }) // if panics | ||
mainthread.Call(func() {}) // for execution synchronization | ||
err := mainthread.Error() // err must be non-nil | ||
``` | ||
|
||
|
||
It is possible to cache up to a maximum of 42 panicked errors. | ||
More errors are ignored. | ||
|
||
## When do you need this package? | ||
|
||
Read this to learn more about the design purpose of this package: | ||
https://golang.design/research/zero-alloc-call-sched/ | ||
|
||
## Who is using this package? | ||
|
||
The initial purpose of building this package is to support writing | ||
graphical applications in Go. To know projects that are using this | ||
package, check our [wiki](https://github.com/golang-design/mainthread/wiki) | ||
page. | ||
|
||
|
||
## License | ||
|
||
MIT | © 2021 The golang.design Initiative Authors, written by [Changkun Ou](https://changkun.de). |
Oops, something went wrong.