queue_unix.go 473 B

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