2016-08-15 03:44:20 +03: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"
)
// 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
2017-02-09 09:39:06 +03:00
pool map [ string ] struct { }
2016-08-15 03:44:20 +03:00
}
// NewStatusTable initializes and returns a new StatusTable object.
func NewStatusTable ( ) * StatusTable {
return & StatusTable {
2017-02-09 09:39:06 +03:00
pool : make ( map [ string ] struct { } ) ,
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 ( )
_ , ok := p . pool [ name ]
if ! ok {
p . pool [ name ] = struct { } { }
}
p . lock . Unlock ( )
return ! ok
}
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 ( )
2017-02-09 09:39:06 +03:00
p . pool [ name ] = struct { } { }
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 ( )
2017-02-09 09:39:06 +03:00
delete ( p . pool , name )
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 ( )
2017-02-09 09:39:06 +03:00
_ , ok := p . pool [ name ]
p . lock . RUnlock ( )
return ok
2016-08-15 03:44:20 +03:00
}