2016-08-15 08:44:20 +08:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2016-08-15 08:44:20 +08:00
|
|
|
|
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2022-10-12 13:18:26 +08:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/container"
|
2016-08-15 08:44:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// StatusTable is a table maintains true/false values.
|
|
|
|
//
|
|
|
|
// This table is particularly useful for un/marking and checking values
|
|
|
|
// in different goroutines.
|
|
|
|
type StatusTable struct {
|
|
|
|
lock sync.RWMutex
|
2022-10-12 13:18:26 +08:00
|
|
|
pool container.Set[string]
|
2016-08-15 08:44:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewStatusTable initializes and returns a new StatusTable object.
|
|
|
|
func NewStatusTable() *StatusTable {
|
|
|
|
return &StatusTable{
|
2022-10-12 13:18:26 +08:00
|
|
|
pool: make(container.Set[string]),
|
2016-08-15 08:44:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:57:17 +08:00
|
|
|
// StartIfNotRunning sets value of given name to true if not already in pool.
|
|
|
|
// Returns whether set value was set to true
|
|
|
|
func (p *StatusTable) StartIfNotRunning(name string) bool {
|
|
|
|
p.lock.Lock()
|
2022-10-12 13:18:26 +08:00
|
|
|
added := p.pool.Add(name)
|
2017-05-31 16:57:17 +08:00
|
|
|
p.lock.Unlock()
|
2022-10-12 13:18:26 +08:00
|
|
|
return added
|
2017-05-31 16:57:17 +08:00
|
|
|
}
|
|
|
|
|
2016-08-15 08:44:20 +08:00
|
|
|
// Start sets value of given name to true in the pool.
|
|
|
|
func (p *StatusTable) Start(name string) {
|
|
|
|
p.lock.Lock()
|
2022-10-12 13:18:26 +08:00
|
|
|
p.pool.Add(name)
|
2017-02-09 14:39:06 +08:00
|
|
|
p.lock.Unlock()
|
2016-08-15 08:44:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop sets value of given name to false in the pool.
|
|
|
|
func (p *StatusTable) Stop(name string) {
|
|
|
|
p.lock.Lock()
|
2022-10-12 13:18:26 +08:00
|
|
|
p.pool.Remove(name)
|
2017-02-09 14:39:06 +08:00
|
|
|
p.lock.Unlock()
|
2016-08-15 08:44:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsRunning checks if value of given name is set to true in the pool.
|
|
|
|
func (p *StatusTable) IsRunning(name string) bool {
|
|
|
|
p.lock.RLock()
|
2022-10-12 13:18:26 +08:00
|
|
|
exists := p.pool.Contains(name)
|
2017-02-09 14:39:06 +08:00
|
|
|
p.lock.RUnlock()
|
2022-10-12 13:18:26 +08:00
|
|
|
return exists
|
2016-08-15 08:44:20 +08:00
|
|
|
}
|