github.com/moontrade/wavm-go@v0.3.2-0.20220316110326-d229dd66ad65/worker/reactor.go (about)

     1  package worker
     2  
     3  import (
     4  	"runtime"
     5  	"sync"
     6  	"time"
     7  )
     8  
     9  // Reactor schedules workers to run
    10  type Reactor struct {
    11  	workers map[int64]*Worker
    12  	started int64
    13  	wg      sync.WaitGroup
    14  	mu      sync.Mutex
    15  }
    16  
    17  func NewReactor() *Reactor {
    18  	return &Reactor{}
    19  }
    20  
    21  func (r *Reactor) Start() {
    22  	r.mu.Lock()
    23  	defer r.mu.Unlock()
    24  	if r.started > 0 {
    25  		return
    26  	}
    27  
    28  	r.wg.Add(1)
    29  	go r.run()
    30  }
    31  
    32  func (r *Reactor) run() {
    33  	defer r.wg.Done()
    34  	// Important for C __thread thread-local storage to work.
    35  	runtime.LockOSThread()
    36  	defer runtime.UnlockOSThread()
    37  
    38  	for {
    39  		time.Sleep(time.Second)
    40  	}
    41  }