Commit a380c1c9 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Use ch for condition variable

Thanks @titanous I didnt know this worked.
parent 9b0c3b28
...@@ -34,8 +34,8 @@ type RemoteCmd struct { ...@@ -34,8 +34,8 @@ type RemoteCmd struct {
ExitStatus int ExitStatus int
// Internal locks and such used for safely setting some shared variables // Internal locks and such used for safely setting some shared variables
l sync.Mutex l sync.Mutex
exitCond *sync.Cond exitCh chan struct{}
} }
// A Communicator is the interface used to communicate with the machine // A Communicator is the interface used to communicate with the machine
...@@ -134,33 +134,25 @@ OutputLoop: ...@@ -134,33 +134,25 @@ OutputLoop:
// order to set that the command is done. // order to set that the command is done.
func (r *RemoteCmd) SetExited(status int) { func (r *RemoteCmd) SetExited(status int) {
r.l.Lock() r.l.Lock()
if r.exitCond == nil { defer r.l.Unlock()
r.exitCond = sync.NewCond(new(sync.Mutex))
}
r.l.Unlock()
r.exitCond.L.Lock() if r.exitCh == nil {
defer r.exitCond.L.Unlock() r.exitCh = make(chan struct{})
}
r.Exited = true r.Exited = true
r.ExitStatus = status r.ExitStatus = status
r.exitCond.Broadcast() close(r.exitCh)
} }
// Wait waits for the remote command to complete. // Wait waits for the remote command to complete.
func (r *RemoteCmd) Wait() { func (r *RemoteCmd) Wait() {
// Make sure our condition variable is initialized. // Make sure our condition variable is initialized.
r.l.Lock() r.l.Lock()
if r.exitCond == nil { if r.exitCh == nil {
r.exitCond = sync.NewCond(new(sync.Mutex)) r.exitCh = make(chan struct{})
} }
r.l.Unlock() r.l.Unlock()
// Wait on the condition variable to notify we exited <-r.exitCh
r.exitCond.L.Lock()
defer r.exitCond.L.Unlock()
for !r.Exited {
r.exitCond.Wait()
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment