Skip to content

Commit

Permalink
content: add mainthread-naive implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Jan 25, 2021
1 parent b0aa3eb commit 4c21d4a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
goos: darwin
goarch: arm64
pkg: x/mainthread
BenchmarkDirectCall-8 1000000000 0.9441 ns/op 0 B/op 0 allocs/op
BenchmarkDirectCall-8 1000000000 0.9443 ns/op 0 B/op 0 allocs/op
BenchmarkDirectCall-8 1000000000 0.9459 ns/op 0 B/op 0 allocs/op
BenchmarkDirectCall-8 1000000000 0.9563 ns/op 0 B/op 0 allocs/op
BenchmarkDirectCall-8 1000000000 0.9526 ns/op 0 B/op 0 allocs/op
BenchmarkDirectCall-8 1000000000 0.9475 ns/op 0 B/op 0 allocs/op
BenchmarkDirectCall-8 1000000000 0.9495 ns/op 0 B/op 0 allocs/op
BenchmarkDirectCall-8 1000000000 0.9544 ns/op 0 B/op 0 allocs/op
BenchmarkDirectCall-8 1000000000 0.9490 ns/op 0 B/op 0 allocs/op
BenchmarkDirectCall-8 1000000000 0.9644 ns/op 0 B/op 0 allocs/op
BenchmarkMainThreadCall-8 2673351 447.7 ns/op 120 B/op 2 allocs/op
BenchmarkMainThreadCall-8 2675954 446.5 ns/op 120 B/op 2 allocs/op
BenchmarkMainThreadCall-8 2675186 448.6 ns/op 120 B/op 2 allocs/op
BenchmarkMainThreadCall-8 2681828 447.4 ns/op 120 B/op 2 allocs/op
BenchmarkMainThreadCall-8 2674504 447.1 ns/op 120 B/op 2 allocs/op
BenchmarkMainThreadCall-8 2597884 457.2 ns/op 120 B/op 2 allocs/op
BenchmarkMainThreadCall-8 2670514 446.2 ns/op 120 B/op 2 allocs/op
BenchmarkMainThreadCall-8 2676504 449.6 ns/op 120 B/op 2 allocs/op
BenchmarkMainThreadCall-8 2672632 449.1 ns/op 120 B/op 2 allocs/op
BenchmarkMainThreadCall-8 2668776 448.3 ns/op 120 B/op 2 allocs/op
PASS
ok x/mainthread 27.356s
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2021 The golang.design Initiative Authors.
// All rights reserved.
//
// The code below is produced by Changkun Ou <[email protected]>.

package mainthread

import (
"runtime"
)

func init() {
runtime.LockOSThread()
}

var funcQ = make(chan func(), runtime.GOMAXPROCS(0))

// Init initializes the functionality of running arbitrary subsequent
// functions be called on the main system thread.
//
// Init must be called in the main.main function.
func Init(main func()) {
done := make(chan struct{})
go func() {
main()
done <- struct{}{}
}()

for {
select {
case f := <-funcQ:
f()
case <-done:
return
}
}
}

// Call calls f on the main thread and blocks until f finishes.
func Call(f func()) {
done := make(chan struct{})
funcQ <- func() {
f()
done <- struct{}{}
}
<-done
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2021 The golang.design Initiative Authors.
// All rights reserved.
//
// The code below is produced by Changkun Ou <[email protected]>.

package mainthread_test

import (
"testing"
mainthread "x/mainthread-naive"
)

var f = func() {}

func BenchmarkDirectCall(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
f()
}
}

func BenchmarkMainThreadCall(b *testing.B) {
mainthread.Init(func() {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
mainthread.Call(f)
}
})
}

0 comments on commit 4c21d4a

Please sign in to comment.