github.com/Rookout/GoSDK@v0.1.48/pkg/services/suspender/suspender.go (about)

     1  //go:build go1.15 && !go1.22
     2  // +build go1.15,!go1.22
     3  
     4  package suspender
     5  
     6  import (
     7  	"sync"
     8  	_ "unsafe"
     9  )
    10  
    11  //go:linkname stopWorld runtime.stopTheWorldGC
    12  func stopWorld(reason string)
    13  
    14  //go:linkname startWorld runtime.startTheWorldGC
    15  func startWorld()
    16  
    17  type suspender struct {
    18  	isSuspended bool
    19  }
    20  
    21  
    22  func (s *suspender) StopAll() {
    23  	if s.isSuspended {
    24  		
    25  		return
    26  	}
    27  	
    28  	stopWorld("")
    29  	s.isSuspended = true
    30  }
    31  
    32  func (s *suspender) ResumeAll() {
    33  	if !s.isSuspended {
    34  		return
    35  	}
    36  	
    37  	s.isSuspended = false
    38  	startWorld()
    39  }
    40  
    41  func (s *suspender) Stopped() bool {
    42  	
    43  	return s.isSuspended
    44  }
    45  
    46  var creationLock sync.Mutex
    47  var theOnlySuspender *suspender
    48  
    49  func GetSuspender() Suspender {
    50  	if theOnlySuspender == nil {
    51  		
    52  		creationLock.Lock()
    53  		defer creationLock.Unlock()
    54  		if theOnlySuspender == nil {
    55  			
    56  			theOnlySuspender = &suspender{isSuspended: false}
    57  		}
    58  	}
    59  
    60  	return theOnlySuspender
    61  }