本站源代码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

60 行
1.4KB

  1. // Copyright 2016 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 sync
  5. import (
  6. "sync"
  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 map[string]struct{}
  15. }
  16. // NewStatusTable initializes and returns a new StatusTable object.
  17. func NewStatusTable() *StatusTable {
  18. return &StatusTable{
  19. pool: make(map[string]struct{}),
  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. _, ok := p.pool[name]
  27. if !ok {
  28. p.pool[name] = struct{}{}
  29. }
  30. p.lock.Unlock()
  31. return !ok
  32. }
  33. // Start sets value of given name to true in the pool.
  34. func (p *StatusTable) Start(name string) {
  35. p.lock.Lock()
  36. p.pool[name] = struct{}{}
  37. p.lock.Unlock()
  38. }
  39. // Stop sets value of given name to false in the pool.
  40. func (p *StatusTable) Stop(name string) {
  41. p.lock.Lock()
  42. delete(p.pool, name)
  43. p.lock.Unlock()
  44. }
  45. // IsRunning checks if value of given name is set to true in the pool.
  46. func (p *StatusTable) IsRunning(name string) bool {
  47. p.lock.RLock()
  48. _, ok := p.pool[name]
  49. p.lock.RUnlock()
  50. return ok
  51. }
上海开阖软件有限公司 沪ICP备12045867号-1