status_pool.go 900 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package sync
  2. import (
  3. "sync"
  4. )
  5. // StatusTable is a table maintains true/false values.
  6. //
  7. // This table is particularly useful for un/marking and checking values
  8. // in different goroutines.
  9. type StatusTable struct {
  10. sync.RWMutex
  11. pool map[string]bool
  12. }
  13. // NewStatusTable initializes and returns a new StatusTable object.
  14. func NewStatusTable() *StatusTable {
  15. return &StatusTable{
  16. pool: make(map[string]bool),
  17. }
  18. }
  19. // Start sets value of given name to true in the pool.
  20. func (p *StatusTable) Start(name string) {
  21. p.Lock()
  22. defer p.Unlock()
  23. p.pool[name] = true
  24. }
  25. // Stop sets value of given name to false in the pool.
  26. func (p *StatusTable) Stop(name string) {
  27. p.Lock()
  28. defer p.Unlock()
  29. p.pool[name] = false
  30. }
  31. // IsRunning checks if value of given name is set to true in the pool.
  32. func (p *StatusTable) IsRunning(name string) bool {
  33. p.RLock()
  34. defer p.RUnlock()
  35. return p.pool[name]
  36. }