From a380c1c91ea4d77e65150c2ece1be1c786e22651 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 29 Jul 2013 13:20:57 -0700 Subject: [PATCH] packer: Use ch for condition variable Thanks @titanous I didnt know this worked. --- packer/communicator.go | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/packer/communicator.go b/packer/communicator.go index 63c052125..577c6ab80 100644 --- a/packer/communicator.go +++ b/packer/communicator.go @@ -34,8 +34,8 @@ type RemoteCmd struct { ExitStatus int // Internal locks and such used for safely setting some shared variables - l sync.Mutex - exitCond *sync.Cond + l sync.Mutex + exitCh chan struct{} } // A Communicator is the interface used to communicate with the machine @@ -134,33 +134,25 @@ OutputLoop: // order to set that the command is done. func (r *RemoteCmd) SetExited(status int) { r.l.Lock() - if r.exitCond == nil { - r.exitCond = sync.NewCond(new(sync.Mutex)) - } - r.l.Unlock() + defer r.l.Unlock() - r.exitCond.L.Lock() - defer r.exitCond.L.Unlock() + if r.exitCh == nil { + r.exitCh = make(chan struct{}) + } r.Exited = true r.ExitStatus = status - r.exitCond.Broadcast() + close(r.exitCh) } // Wait waits for the remote command to complete. func (r *RemoteCmd) Wait() { // Make sure our condition variable is initialized. r.l.Lock() - if r.exitCond == nil { - r.exitCond = sync.NewCond(new(sync.Mutex)) + if r.exitCh == nil { + r.exitCh = make(chan struct{}) } r.l.Unlock() - // Wait on the condition variable to notify we exited - r.exitCond.L.Lock() - defer r.exitCond.L.Unlock() - - for !r.Exited { - r.exitCond.Wait() - } + <-r.exitCh }