github.com/ethersphere/bee/v2@v2.2.0/pkg/util/syncutil/signaler.go (about)

     1  // Copyright 2021 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package syncutil
     6  
     7  import "sync"
     8  
     9  // Signaler allows for multiple writers to safely signal an event
    10  // so that reader on the channel C would get unblocked
    11  type Signaler struct {
    12  	C    chan struct{}
    13  	once sync.Once
    14  }
    15  
    16  // NewSignaler initializes a new obj
    17  func NewSignaler() *Signaler {
    18  	return &Signaler{C: make(chan struct{})}
    19  }
    20  
    21  // Signal safely closes the blocking channel
    22  func (s *Signaler) Signal() {
    23  	s.once.Do(func() {
    24  		close(s.C)
    25  	})
    26  }