code.gitea.io/gitea@v1.19.3/modules/sync/status_pool.go (about)

     1  // Copyright 2016 The Gogs Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package sync
     5  
     6  import (
     7  	"sync"
     8  
     9  	"code.gitea.io/gitea/modules/container"
    10  )
    11  
    12  // StatusTable is a table maintains true/false values.
    13  //
    14  // This table is particularly useful for un/marking and checking values
    15  // in different goroutines.
    16  type StatusTable struct {
    17  	lock sync.RWMutex
    18  	pool container.Set[string]
    19  }
    20  
    21  // NewStatusTable initializes and returns a new StatusTable object.
    22  func NewStatusTable() *StatusTable {
    23  	return &StatusTable{
    24  		pool: make(container.Set[string]),
    25  	}
    26  }
    27  
    28  // StartIfNotRunning sets value of given name to true if not already in pool.
    29  // Returns whether set value was set to true
    30  func (p *StatusTable) StartIfNotRunning(name string) bool {
    31  	p.lock.Lock()
    32  	added := p.pool.Add(name)
    33  	p.lock.Unlock()
    34  	return added
    35  }
    36  
    37  // Start sets value of given name to true in the pool.
    38  func (p *StatusTable) Start(name string) {
    39  	p.lock.Lock()
    40  	p.pool.Add(name)
    41  	p.lock.Unlock()
    42  }
    43  
    44  // Stop sets value of given name to false in the pool.
    45  func (p *StatusTable) Stop(name string) {
    46  	p.lock.Lock()
    47  	p.pool.Remove(name)
    48  	p.lock.Unlock()
    49  }
    50  
    51  // IsRunning checks if value of given name is set to true in the pool.
    52  func (p *StatusTable) IsRunning(name string) bool {
    53  	p.lock.RLock()
    54  	exists := p.pool.Contains(name)
    55  	p.lock.RUnlock()
    56  	return exists
    57  }