github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/blockchain/pseudohsm/watch.go (about)

     1  // +build darwin,!ios freebsd linux,!arm64 netbsd solaris windows
     2  
     3  package pseudohsm
     4  
     5  type watcher struct {
     6  	kc       *keyCache
     7  	starting bool
     8  	running  bool
     9  	quit     chan struct{}
    10  }
    11  
    12  func newWatcher(kc *keyCache) *watcher {
    13  	return &watcher{
    14  		kc:   kc,
    15  		quit: make(chan struct{}),
    16  	}
    17  }
    18  
    19  // starts the watcher loop in the background.
    20  // Start a watcher in the background if that's not already in progress.
    21  // The caller must hold w.kc.mu.
    22  func (w *watcher) start() {
    23  	if w.starting || w.running {
    24  		return
    25  	}
    26  	w.starting = true
    27  	go w.loop()
    28  }
    29  
    30  func (w *watcher) close() {
    31  	close(w.quit)
    32  }
    33  
    34  func (w *watcher) loop() {
    35  	defer func() {
    36  		w.kc.mu.Lock()
    37  		w.running = false
    38  		w.starting = false
    39  		w.kc.mu.Unlock()
    40  	}()
    41  }