1
0
mirror of https://github.com/docker/cli.git synced 2026-01-22 03:22:01 +03:00
Files
cli/components/engine/state.go
Guillaume J. Charmes fb9222b734 Add inconditionnal lock in Start/Stop/Kill to avoid races
Upstream-commit: 7c2b085d1a1394dc88b0cdf5c9df02ee1c1b2229
Component: engine
2013-04-09 09:09:54 -07:00

49 lines
746 B
Go

package docker
import (
"fmt"
"sync"
"time"
)
type State struct {
Running bool
Pid int
ExitCode int
StartedAt time.Time
l *sync.Mutex
}
// String returns a human-readable description of the state
func (s *State) String() string {
if s.Running {
return fmt.Sprintf("Up %s", HumanDuration(time.Now().Sub(s.StartedAt)))
}
return fmt.Sprintf("Exit %d", s.ExitCode)
}
func (s *State) setRunning(pid int) {
s.Running = true
s.ExitCode = 0
s.Pid = pid
s.StartedAt = time.Now()
}
func (s *State) setStopped(exitCode int) {
s.Running = false
s.Pid = 0
s.ExitCode = exitCode
}
func (s *State) initLock() {
s.l = &sync.Mutex{}
}
func (s *State) lock() {
s.l.Lock()
}
func (s *State) unlock() {
s.l.Unlock()
}