github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/libcontainerd/queue.go (about)

     1  package libcontainerd
     2  
     3  import "sync"
     4  
     5  type queue struct {
     6  	sync.Mutex
     7  	fns map[string]chan struct{}
     8  }
     9  
    10  func (q *queue) append(id string, f func()) {
    11  	q.Lock()
    12  	defer q.Unlock()
    13  
    14  	if q.fns == nil {
    15  		q.fns = make(map[string]chan struct{})
    16  	}
    17  
    18  	done := make(chan struct{})
    19  
    20  	fn, ok := q.fns[id]
    21  	q.fns[id] = done
    22  	go func() {
    23  		if ok {
    24  			<-fn
    25  		}
    26  		f()
    27  		close(done)
    28  
    29  		q.Lock()
    30  		if q.fns[id] == done {
    31  			delete(q.fns, id)
    32  		}
    33  		q.Unlock()
    34  	}()
    35  }