queue.go 597 B

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