Skip to content

Commit

Permalink
content: draft zero-alloc-call-sched
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Jan 21, 2021
1 parent bfca30e commit d6a2d0a
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 17 deletions.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions content/assets/zero-alloc-call-sched/thread/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package thread_test

import (
"testing"
"x/thread"
)

func BenchmarkThreadCall(b *testing.B) {
th := thread.New()
f := func() {}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
th.Call(f)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ type funcData struct {
done chan struct{}
}

// Thread offers facilities to schedule function calls to run
// on a same thread.
// Thread offers facilities to schedule function calls to run on a
// specific thread.
type Thread struct {
f chan funcData
terminate chan struct{}
}

// Call calls f on the given thread.
// Call calls f on the given thread and returns true if the call is
// complete or false if failed.
func (t *Thread) Call(f func()) bool {
if f == nil {
return false
Expand All @@ -43,9 +44,7 @@ func (t *Thread) Call(f func()) bool {
default:
done := donePool.Get().(chan struct{})
defer donePool.Put(done)
defer func() {
<-done
}()
defer func() { <-done }()

t.f <- funcData{fn: f, done: done}
}
Expand All @@ -62,7 +61,7 @@ func (t *Thread) Terminate() {
}
}

// New creates
// New creates a thread.
func New() *Thread {
t := Thread{
f: make(chan funcData),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//
// The code below is produced by Changkun Ou <[email protected]>.

// +build linux

package thread_test

import (
Expand Down Expand Up @@ -63,13 +65,3 @@ func TestThread(t *testing.T) {
t.Fatalf("failed to schedule function on the same thread.")
}
}

func BenchmarkThreadCall(b *testing.B) {
th := thread.New()
f := func() {}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
th.Call(f)
}
}
48 changes: 48 additions & 0 deletions content/posts/zero-alloc-call-sched.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
date: 2021-01-21T10:57:59+01:00
toc: true
slug: /zero-alloc-call-sched
tags:
- Channel
- EscapeAnalysis
- GUI
- MainThread
- Thread
title: Scheduling Calls with Zero Allocation
draft: true
---

Author(s): [Changkun Ou](https://changkun.de)

GUI programming in Go is a little bit tricky. The infamous issue regarding interacting with the legacy GUI frameworks is that most of the graphics related APIs must be called from the main thread. This basically violates the concurrent nature of Go: A goroutine may be arbitrarily and randomly scheduled or rescheduled on different running threads, i.e., the same pice of code will be called from different threads over time, even without evolving the `go` keyword.

<!--more-->


## Background

TODO:

## The Main Thread

TODO:

## Cost Analysis and Optimization

TODO:

## Optimal Threading Control

TODO:

## Verification and Discussion

TODO:

## Conclusion

TODO:

## References

TODO:

0 comments on commit d6a2d0a

Please sign in to comment.