2016-08-15 02:44:20 +02:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2016-08-15 02:44:20 +02:00
|
|
|
|
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2022-10-12 07:18:26 +02:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/container"
|
2016-08-15 02:44:20 +02: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 07:18:26 +02:00
|
|
|
pool container.Set[string]
|
2016-08-15 02:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewStatusTable initializes and returns a new StatusTable object.
|
|
|
|
func NewStatusTable() *StatusTable {
|
|
|
|
return &StatusTable{
|
2022-10-12 07:18:26 +02:00
|
|
|
pool: make(container.Set[string]),
|
2016-08-15 02:44:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 10:57:17 +02: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 07:18:26 +02:00
|
|
|
added := p.pool.Add(name)
|
2017-05-31 10:57:17 +02:00
|
|
|
p.lock.Unlock()
|
2022-10-12 07:18:26 +02:00
|
|
|
return added
|
2017-05-31 10:57:17 +02:00
|
|
|
}
|
|
|
|
|
2016-08-15 02:44:20 +02:00
|
|
|
// Start sets value of given name to true in the pool.
|
|
|
|
func (p *StatusTable) Start(name string) {
|
|
|
|
p.lock.Lock()
|
2022-10-12 07:18:26 +02:00
|
|
|
p.pool.Add(name)
|
2017-02-09 07:39:06 +01:00
|
|
|
p.lock.Unlock()
|
2016-08-15 02:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop sets value of given name to false in the pool.
|
|
|
|
func (p *StatusTable) Stop(name string) {
|
|
|
|
p.lock.Lock()
|
2022-10-12 07:18:26 +02:00
|
|
|
p.pool.Remove(name)
|
2017-02-09 07:39:06 +01:00
|
|
|
p.lock.Unlock()
|
2016-08-15 02:44:20 +02: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 07:18:26 +02:00
|
|
|
exists := p.pool.Contains(name)
|
2017-02-09 07:39:06 +01:00
|
|
|
p.lock.RUnlock()
|
2022-10-12 07:18:26 +02:00
|
|
|
return exists
|
2016-08-15 02:44:20 +02:00
|
|
|
}
|