1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/state.go

62 lines
1.1 KiB
Go
Raw Normal View History

2013-01-18 19:13:39 -05:00
package docker
import (
2013-03-12 08:36:37 -04:00
"fmt"
"github.com/dotcloud/docker/future"
2013-01-28 17:30:05 -05:00
"sync"
"time"
2013-01-18 19:13:39 -05:00
)
type State struct {
2013-01-22 20:30:37 -05:00
Running bool
Pid int
ExitCode int
StartedAt time.Time
2013-01-18 19:13:39 -05:00
stateChangeLock *sync.Mutex
stateChangeCond *sync.Cond
}
func newState() *State {
lock := new(sync.Mutex)
return &State{
stateChangeLock: lock,
stateChangeCond: sync.NewCond(lock),
}
}
// String returns a human-readable description of the state
func (s *State) String() string {
if s.Running {
2013-01-29 06:18:07 -05:00
return fmt.Sprintf("Up %s", future.HumanDuration(time.Now().Sub(s.StartedAt)))
}
2013-01-29 06:18:07 -05:00
return fmt.Sprintf("Exit %d", s.ExitCode)
}
2013-01-18 19:13:39 -05:00
func (s *State) setRunning(pid int) {
s.Running = true
s.ExitCode = 0
s.Pid = pid
s.StartedAt = time.Now()
2013-01-18 19:13:39 -05:00
s.broadcast()
}
func (s *State) setStopped(exitCode int) {
s.Running = false
s.Pid = 0
s.ExitCode = exitCode
s.broadcast()
}
func (s *State) broadcast() {
s.stateChangeLock.Lock()
s.stateChangeCond.Broadcast()
s.stateChangeLock.Unlock()
}
func (s *State) wait() {
s.stateChangeLock.Lock()
s.stateChangeCond.Wait()
s.stateChangeLock.Unlock()
}