A little bit of Go!

I changed a goroutine to make use a service which gets awaken whenever a signal happens. This is used because the signal is sent only in very complicated cases and I wanted to remove the tests from the bottom goroutine (many of the parameters are from other structures not available here.)

Here I wanted to get the basics to show how it's done.

var (
  someMutex      sync.Mutex
  someCond       *sync.Cond
  someState      bool
)

func init() {
  someCond = sync.NewCond(&someMutex)
  go someLoop()
}

func sendSignal() {
  someMutex.Lock()
  defer someMutex.Unlock()

  someState = true
  someCond.Signal()
}

func waitForSignal() {
  someMutex.Lock()
  defer someMutex.Unlock()

  while !someState {
    someCond.Wait()
  }

  someState = false
}

func someLoop() {
  for {
    waitForSignal()
    ...do my work here...
  }
}

Note that if you want to do some work before the wait on the signal, you can easily place it before the for (run it once) or just at the start before the waitForSignal() call.