-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.go
220 lines (194 loc) · 4.18 KB
/
thread.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2020 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.
// Package thread provides threading facilities, such as scheduling
// calls on a specific thread, local storage, etc.
package thread // import "golang.design/x/thread"
import (
"runtime"
"sync"
"sync/atomic"
)
// Thread represents a thread instance.
type Thread interface {
// ID returns the ID of the thread.
ID() uint64
// Call calls fn from the given thread. It blocks until fn returns.
Call(fn func())
// CallNonBlock call fn from the given thread without waiting
// fn to complete.
CallNonBlock(fn func())
// CallV call fn from the given thread and returns the returned
// value from fn.
//
// The purpose of this function is to avoid value escaping.
// In particular:
//
// th := thread.New()
// var ret interface{}
// th.Call(func() {
// ret = 1
// })
//
// will cause variable ret be allocated on the heap, whereas
//
// th := thread.New()
// ret := th.CallV(func() interface{} {
// return 1
// }).(int)
//
// will offer zero allocation benefits.
CallV(fn func() interface{}) interface{}
// SetTLS stores a given value to the local storage of the given
// thread. This method must be accessed in Call, or CallV, or
// CallNonBlock. For instance:
//
// th := thread.New()
// th.Call(func() {
// th.SetTLS("store in thread local storage")
// })
SetTLS(x interface{})
// GetTLS returns the locally stored value from local storage of
// the given thread. This method must be access in Call, or CallV,
// or CallNonBlock. For instance:
//
// th := thread.New()
// th.Call(func() {
// tls := th.GetTLS()
// // ... do what ever you want to do with tls value ...
// })
//
GetTLS() interface{}
// Terminate terminates the given thread gracefully.
// Scheduled but unexecuted calls will be discarded.
Terminate()
}
// New creates a new thread instance.
func New() Thread {
th := thread{
id: atomic.AddUint64(&globalID, 1),
fdCh: make(chan funcData, runtime.GOMAXPROCS(0)),
doneCh: make(chan struct{}),
}
runtime.SetFinalizer(&th, func(th interface{}) {
th.(*thread).Terminate()
})
go func() {
runtime.LockOSThread()
for {
select {
case fd := <-th.fdCh:
func() {
if fd.fn != nil {
defer func() {
if fd.done != nil {
fd.done <- struct{}{}
}
}()
fd.fn()
} else if fd.fnv != nil {
var ret interface{}
defer func() {
if fd.ret != nil {
fd.ret <- ret
}
}()
ret = fd.fnv()
}
}()
case <-th.doneCh:
close(th.doneCh)
return
}
}
}()
return &th
}
var (
donePool = sync.Pool{
New: func() interface{} {
return make(chan struct{})
},
}
varPool = sync.Pool{
New: func() interface{} {
return make(chan interface{})
},
}
globalID uint64 // atomic
_ Thread = &thread{}
)
type funcData struct {
fn func()
done chan struct{}
fnv func() interface{}
ret chan interface{}
}
type thread struct {
id uint64
tls interface{}
fdCh chan funcData
doneCh chan struct{}
}
func (th thread) ID() uint64 {
return th.id
}
func (th *thread) Call(fn func()) {
if fn == nil {
return
}
select {
case <-th.doneCh:
return
default:
done := donePool.Get().(chan struct{})
defer donePool.Put(done)
defer func() { <-done }()
th.fdCh <- funcData{fn: fn, done: done}
}
return
}
func (th *thread) CallNonBlock(fn func()) {
if fn == nil {
return
}
select {
case <-th.doneCh:
return
default:
th.fdCh <- funcData{fn: fn}
}
}
func (th *thread) CallV(fn func() interface{}) (ret interface{}) {
if fn == nil {
return nil
}
select {
case <-th.doneCh:
return nil
default:
done := varPool.Get().(chan interface{})
defer varPool.Put(done)
defer func() { ret = <-done }()
th.fdCh <- funcData{fnv: fn, ret: done}
return
}
}
func (th *thread) GetTLS() interface{} {
return th.tls
}
func (th *thread) SetTLS(x interface{}) {
th.tls = x
}
func (th *thread) Terminate() {
select {
case <-th.doneCh:
return
default:
th.doneCh <- struct{}{}
select {
case <-th.doneCh:
return
}
}
}