github.com/EagleQL/Xray-core@v1.4.3/common/signal/semaphore/semaphore.go (about)

     1  package semaphore
     2  
     3  // Instance is an implementation of semaphore.
     4  type Instance struct {
     5  	token chan struct{}
     6  }
     7  
     8  // New create a new Semaphore with n permits.
     9  func New(n int) *Instance {
    10  	s := &Instance{
    11  		token: make(chan struct{}, n),
    12  	}
    13  	for i := 0; i < n; i++ {
    14  		s.token <- struct{}{}
    15  	}
    16  	return s
    17  }
    18  
    19  // Wait returns a channel for acquiring a permit.
    20  func (s *Instance) Wait() <-chan struct{} {
    21  	return s.token
    22  }
    23  
    24  // Signal releases a permit into the semaphore.
    25  func (s *Instance) Signal() {
    26  	s.token <- struct{}{}
    27  }