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