You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

working_pool.go 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "sync"
  7. )
  8. // workingPool represents a pool of working status which makes sure
  9. // that only one instance of same task is performing at a time.
  10. // However, different type of tasks can performing at the same time.
  11. type workingPool struct {
  12. lock sync.Mutex
  13. pool map[string]*sync.Mutex
  14. count map[string]int
  15. }
  16. // CheckIn checks in a task and waits if others are running.
  17. func (p *workingPool) CheckIn(name string) {
  18. p.lock.Lock()
  19. lock, has := p.pool[name]
  20. if !has {
  21. lock = &sync.Mutex{}
  22. p.pool[name] = lock
  23. }
  24. p.count[name]++
  25. p.lock.Unlock()
  26. lock.Lock()
  27. }
  28. // CheckOut checks out a task to let other tasks run.
  29. func (p *workingPool) CheckOut(name string) {
  30. p.lock.Lock()
  31. defer p.lock.Unlock()
  32. p.pool[name].Unlock()
  33. if p.count[name] == 1 {
  34. delete(p.pool, name)
  35. delete(p.count, name)
  36. } else {
  37. p.count[name]--
  38. }
  39. }