Skip to content

Commit 19f27ae

Browse files
committed
all: update docs
1 parent f558428 commit 19f27ae

File tree

5 files changed

+115
-31
lines changed

5 files changed

+115
-31
lines changed

README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff 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
1721
package main
1822

1923
import "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
2528
func 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?

bench_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

example_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

mainthread.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
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() { ... }
735
package mainthread // import "golang.design/x/mainthread"
836

937
import (

mainthread_test.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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

1212
import (
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-
}

0 commit comments

Comments
 (0)