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.

status_pool.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package sync
  4. import (
  5. "sync"
  6. "code.gitea.io/gitea/modules/container"
  7. )
  8. // StatusTable is a table maintains true/false values.
  9. //
  10. // This table is particularly useful for un/marking and checking values
  11. // in different goroutines.
  12. type StatusTable struct {
  13. lock sync.RWMutex
  14. pool container.Set[string]
  15. }
  16. // NewStatusTable initializes and returns a new StatusTable object.
  17. func NewStatusTable() *StatusTable {
  18. return &StatusTable{
  19. pool: make(container.Set[string]),
  20. }
  21. }
  22. // StartIfNotRunning sets value of given name to true if not already in pool.
  23. // Returns whether set value was set to true
  24. func (p *StatusTable) StartIfNotRunning(name string) bool {
  25. p.lock.Lock()
  26. added := p.pool.Add(name)
  27. p.lock.Unlock()
  28. return added
  29. }
  30. // Start sets value of given name to true in the pool.
  31. func (p *StatusTable) Start(name string) {
  32. p.lock.Lock()
  33. p.pool.Add(name)
  34. p.lock.Unlock()
  35. }
  36. // Stop sets value of given name to false in the pool.
  37. func (p *StatusTable) Stop(name string) {
  38. p.lock.Lock()
  39. p.pool.Remove(name)
  40. p.lock.Unlock()
  41. }
  42. // IsRunning checks if value of given name is set to true in the pool.
  43. func (p *StatusTable) IsRunning(name string) bool {
  44. p.lock.RLock()
  45. exists := p.pool.Contains(name)
  46. p.lock.RUnlock()
  47. return exists
  48. }