status_pool.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package sync
  7. import (
  8. "sync"
  9. )
  10. // StatusTable is a table maintains true/false values.
  11. //
  12. // This table is particularly useful for un/marking and checking values
  13. // in different goroutines.
  14. type StatusTable struct {
  15. sync.RWMutex
  16. pool map[string]bool
  17. }
  18. // NewStatusTable initializes and returns a new StatusTable object.
  19. func NewStatusTable() *StatusTable {
  20. return &StatusTable{
  21. pool: make(map[string]bool),
  22. }
  23. }
  24. // Start sets value of given name to true in the pool.
  25. func (p *StatusTable) Start(name string) {
  26. p.Lock()
  27. defer p.Unlock()
  28. p.pool[name] = true
  29. }
  30. // Stop sets value of given name to false in the pool.
  31. func (p *StatusTable) Stop(name string) {
  32. p.Lock()
  33. defer p.Unlock()
  34. p.pool[name] = false
  35. }
  36. // IsRunning checks if value of given name is set to true in the pool.
  37. func (p *StatusTable) IsRunning(name string) bool {
  38. p.RLock()
  39. defer p.RUnlock()
  40. return p.pool[name]
  41. }