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