github.com/aergoio/aergo@v1.3.1/contract/lstate_factory.go (about) 1 package contract 2 3 /* 4 #include <lualib.h> 5 #include "lgmp.h" 6 */ 7 import "C" 8 import "sync" 9 10 var getCh chan *LState 11 var freeCh chan *LState 12 var once sync.Once 13 14 const MAX_LSTATE_SIZE = 150 15 16 func StartLStateFactory() { 17 once.Do(func() { 18 C.init_bignum() 19 getCh = make(chan *LState, MAX_LSTATE_SIZE) 20 freeCh = make(chan *LState, MAX_LSTATE_SIZE) 21 22 for i := 0; i < MAX_LSTATE_SIZE; i++ { 23 getCh <- NewLState() 24 } 25 go statePool() 26 }) 27 } 28 29 func statePool() { 30 for { 31 state := <-freeCh 32 state.Close() 33 getCh <- NewLState() 34 } 35 } 36 37 func GetLState() *LState { 38 state := <-getCh 39 return state 40 } 41 42 func FreeLState(state *LState) { 43 freeCh <- state 44 }