github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/libcontainerd/queue/queue.go (about)

     1  package queue // import "github.com/demonoid81/moby/libcontainerd/queue"
     2  
     3  import "sync"
     4  
     5  // Queue is the structure used for holding functions in a queue.
     6  type Queue struct {
     7  	sync.Mutex
     8  	fns map[string]chan struct{}
     9  }
    10  
    11  // Append adds an item to a queue.
    12  func (q *Queue) Append(id string, f func()) {
    13  	q.Lock()
    14  	defer q.Unlock()
    15  
    16  	if q.fns == nil {
    17  		q.fns = make(map[string]chan struct{})
    18  	}
    19  
    20  	done := make(chan struct{})
    21  
    22  	fn, ok := q.fns[id]
    23  	q.fns[id] = done
    24  	go func() {
    25  		if ok {
    26  			<-fn
    27  		}
    28  		f()
    29  		close(done)
    30  
    31  		q.Lock()
    32  		if q.fns[id] == done {
    33  			delete(q.fns, id)
    34  		}
    35  		q.Unlock()
    36  	}()
    37  }