File tree Expand file tree Collapse file tree 5 files changed +115
-31
lines changed Expand file tree Collapse file tree 5 files changed +115
-31
lines changed Original file line number Diff line number Diff line change @@ -13,22 +13,35 @@ import "golang.design/x/mainthread"
1313
1414## API Usage
1515
16+ Package mainthread offers facilities to schedule functions on the
17+ maint hread. To use this package properly, one must call
18+ mainthread.Init from the main package. For example:
19+
1620``` go
1721package main
1822
1923import " golang.design/x/mainthread"
2024
21- func main () {
22- mainthread.Init (fn)
23- }
25+ func main () { mainthread.Init (fn) }
2426
27+ // fn is the actual main function
2528func fn () {
26- mainthread.Call (func () {
27- // ... runs on the main thread ...
28- })
29+ // mainthread.Call returns when f1 returns. Note that if f1 blocks
30+ // it will also block the execution of any subsequent calls on the
31+ // main thread.
32+ mainthread.Call (f1)
33+
34+ // ... do whatever you want to do ...
2935
30- // ... do what ever you want to do ...
36+ // mainthread.Go returns immediately and f2 is scheduled to be executed
37+ // in the future.
38+ mainthread.Go (f2)
39+
40+ // ... do whatever you want to do ...
3141}
42+
43+ func f1 () { ... }
44+ func f2 () { ... }
3245```
3346
3447## When do you need this package?
Original file line number Diff line number Diff line change 1+ // Copyright 2020-2021 The golang.design Initiative Authors.
2+ // All rights reserved. Use of this source code is governed
3+ // by a MIT license that can be found in the LICENSE file.
4+ //
5+ // Written by Changkun Ou <changkun.de>
6+
7+ package mainthread_test
8+
9+ import (
10+ "testing"
11+
12+ "golang.design/x/mainthread"
13+ )
14+
15+ func BenchmarkCall (b * testing.B ) {
16+ f := func () {}
17+ mainthread .Init (func () {
18+ b .ReportAllocs ()
19+ b .ResetTimer ()
20+ for i := 0 ; i < b .N ; i ++ {
21+ mainthread .Call (f )
22+ }
23+ })
24+ }
Original file line number Diff line number Diff line change 1+ // Copyright 2020-2021 The golang.design Initiative Authors.
2+ // All rights reserved. Use of this source code is governed
3+ // by a MIT license that can be found in the LICENSE file.
4+ //
5+ // Written by Changkun Ou <changkun.de>
6+
7+ package mainthread_test
8+
9+ import (
10+ "fmt"
11+
12+ "golang.design/x/mainthread"
13+ )
14+
15+ func ExampleInit () {
16+ mainthread .Init (func () {
17+ // ... Do stuff ...
18+ })
19+ // Output:
20+ }
21+
22+ func ExampleCall () {
23+ mainthread .Init (func () {
24+ mainthread .Call (func () {
25+ fmt .Println ("from main thread" )
26+ })
27+ })
28+ // Output: from main thread
29+ }
30+
31+ func ExampleGo () {
32+ mainthread .Init (func () {
33+ done := make (chan string )
34+ mainthread .Go (func () {
35+ done <- "main thread"
36+ })
37+ fmt .Println ("from" , <- done )
38+ })
39+ // Output: from main thread
40+ }
Original file line number Diff line number Diff line change 1- // Copyright 2021 The golang.design Initiative Authors.
1+ // Copyright 2020- 2021 The golang.design Initiative Authors.
22// All rights reserved. Use of this source code is governed
33// by a MIT license that can be found in the LICENSE file.
44//
55// Written by Changkun Ou <changkun.de>
66
7+ // Package mainthread offers facilities to schedule functions on the
8+ // maint hread. To use this package properly, one must call
9+ // mainthread.Init from the main package. For example:
10+ //
11+ // package main
12+ //
13+ // import "golang.design/x/mainthread"
14+ //
15+ // func main() { mainthread.Init(fn) }
16+ //
17+ // // fn is the actual main function
18+ // func fn() {
19+ // // mainthread.Call returns when f1 returns. Note that if f1
20+ // // blocks it will also block the execution of any subsequent
21+ // // calls on the main thread.
22+ // mainthread.Call(f1)
23+ //
24+ // // ... do whatever you want to do ...
25+ //
26+ // // mainthread.Go returns immediately and f2 is scheduled to be
27+ // // executed in the future.
28+ // mainthread.Go(f2)
29+ //
30+ // // ... do whatever you want to do ...
31+ // }
32+ //
33+ // func f1() { ... }
34+ // func f2() { ... }
735package mainthread // import "golang.design/x/mainthread"
836
937import (
Original file line number Diff line number Diff line change 1- // Copyright 2021 The golang.design Initiative Authors.
1+ // Copyright 2020- 2021 The golang.design Initiative Authors.
22// All rights reserved. Use of this source code is governed
33// by a MIT license that can be found in the LICENSE file.
44//
@@ -11,7 +11,6 @@ package mainthread_test
1111
1212import (
1313 "context"
14- "fmt"
1514 "os"
1615 "sync"
1716 "sync/atomic"
@@ -85,31 +84,11 @@ func TestGo(t *testing.T) {
8584 t .Fatalf ("mainthread.Go is not executing in parallel" )
8685 }
8786
88- ctxx , cancell := context .WithTimeout (context .Background (), time .Second )
87+ ctxx , cancell := context .WithTimeout (context .Background (), time .Second * 2 )
8988 defer cancell ()
9089 select {
9190 case <- ctxx .Done ():
9291 t .Fatalf ("mainthread.Go never schedules the function" )
9392 case <- done :
9493 }
9594}
96-
97- func BenchmarkCall (b * testing.B ) {
98- f := func () {}
99- mainthread .Init (func () {
100- b .ReportAllocs ()
101- b .ResetTimer ()
102- for i := 0 ; i < b .N ; i ++ {
103- mainthread .Call (f )
104- }
105- })
106- }
107-
108- func ExampleInit () {
109- mainthread .Init (func () {
110- mainthread .Call (func () {
111- fmt .Println ("from main thread" )
112- })
113- })
114- // Output: from main thread
115- }
You can’t perform that action at this time.
0 commit comments