github.com/kaydxh/golang@v0.0.131/pkg/pool/instance/thread.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package instance 23 24 import ( 25 "context" 26 "runtime" 27 "sync" 28 29 runtime_ "github.com/kaydxh/golang/go/runtime" 30 ) 31 32 type Thread struct { 33 enableOsThread bool 34 35 ctx context.Context 36 cancel context.CancelFunc 37 handlerCh chan func() 38 once sync.Once 39 mu sync.Mutex 40 } 41 42 func NewThread(enableOsThread bool) *Thread { 43 t := &Thread{ 44 enableOsThread: enableOsThread, 45 } 46 t.initOnce() 47 return t 48 } 49 50 func (t *Thread) initOnce() { 51 t.once.Do(func() { 52 t.mu.Lock() 53 defer t.mu.Unlock() 54 t.ctx, t.cancel = context.WithCancel(context.Background()) 55 56 t.handlerCh = make(chan func()) 57 go t.DoInOSThread() 58 }) 59 } 60 61 func (t *Thread) Do(ctx context.Context, f func()) error { 62 t.initOnce() 63 64 // wait group make Do func and f func return in sync 65 var wg sync.WaitGroup 66 defer wg.Wait() 67 wg.Add(1) 68 69 handler := func() { 70 defer wg.Done() 71 defer runtime_.Recover() 72 f() 73 } 74 75 select { 76 case t.handlerCh <- handler: 77 return nil 78 79 case <-ctx.Done(): 80 wg.Done() 81 return ctx.Err() 82 83 case <-t.ctx.Done(): 84 wg.Done() 85 return t.ctx.Err() 86 } 87 } 88 89 func (t *Thread) DoInOSThread() { 90 91 if t.enableOsThread { 92 runtime.LockOSThread() 93 defer runtime.UnlockOSThread() 94 } 95 96 for { 97 select { 98 case handler, ok := <-t.handlerCh: 99 if !ok { 100 return 101 } 102 103 if handler == nil { 104 continue 105 } 106 handler() 107 108 case <-t.ctx.Done(): 109 return 110 } 111 112 } 113 }