2016-08-14 19:44:20 -05:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2022-10-12 00:18:26 -05:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/container"
|
2016-08-14 19:44:20 -05: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 00:18:26 -05:00
|
|
|
pool container.Set[string]
|
2016-08-14 19:44:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewStatusTable initializes and returns a new StatusTable object.
|
|
|
|
func NewStatusTable() *StatusTable {
|
|
|
|
return &StatusTable{
|
2022-10-12 00:18:26 -05:00
|
|
|
pool: make(container.Set[string]),
|
2016-08-14 19:44:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 03:57:17 -05: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 00:18:26 -05:00
|
|
|
added := p.pool.Add(name)
|
2017-05-31 03:57:17 -05:00
|
|
|
p.lock.Unlock()
|
2022-10-12 00:18:26 -05:00
|
|
|
return added
|
2017-05-31 03:57:17 -05:00
|
|
|
}
|
|
|
|
|
2016-08-14 19:44:20 -05:00
|
|
|
// Start sets value of given name to true in the pool.
|
|
|
|
func (p *StatusTable) Start(name string) {
|
|
|
|
p.lock.Lock()
|
2022-10-12 00:18:26 -05:00
|
|
|
p.pool.Add(name)
|
2017-02-09 01:39:06 -05:00
|
|
|
p.lock.Unlock()
|
2016-08-14 19:44:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop sets value of given name to false in the pool.
|
|
|
|
func (p *StatusTable) Stop(name string) {
|
|
|
|
p.lock.Lock()
|
2022-10-12 00:18:26 -05:00
|
|
|
p.pool.Remove(name)
|
2017-02-09 01:39:06 -05:00
|
|
|
p.lock.Unlock()
|
2016-08-14 19:44:20 -05: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 00:18:26 -05:00
|
|
|
exists := p.pool.Contains(name)
|
2017-02-09 01:39:06 -05:00
|
|
|
p.lock.RUnlock()
|
2022-10-12 00:18:26 -05:00
|
|
|
return exists
|
2016-08-14 19:44:20 -05:00
|
|
|
}
|