github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zutil/daemon/signal.go (about)

     1  package daemon
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/sohaha/zlsgo/zutil"
     7  )
     8  
     9  var (
    10  	singleSignal sync.Once
    11  	single            = zutil.NewChan[bool]()
    12  	singleNum    uint = 0
    13  	singleLock   sync.Mutex
    14  )
    15  
    16  func singelDo() {
    17  	singleSignal.Do(func() {
    18  		go func() {
    19  			kill := KillSignal()
    20  			singleLock.Lock()
    21  			for {
    22  				if singleNum == 0 {
    23  					break
    24  				}
    25  
    26  				singleNum--
    27  				single.In() <- kill
    28  			}
    29  			single.Close()
    30  			singleLock.Unlock()
    31  		}()
    32  	})
    33  }
    34  
    35  func SingleKillSignal() <-chan bool {
    36  	singleLock.Lock()
    37  	defer singleLock.Unlock()
    38  
    39  	singleNum++
    40  	singelDo()
    41  
    42  	return single.Out()
    43  }
    44  
    45  func ReSingleKillSignal() {
    46  	singleLock.Lock()
    47  	defer singleLock.Unlock()
    48  
    49  	if singleNum > 0 {
    50  		return
    51  	}
    52  
    53  	single = zutil.NewChan[bool]()
    54  	singleSignal = sync.Once{}
    55  
    56  	singelDo()
    57  }