Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created July 13, 2010 04:47
Show Gist options
  • Save kylelemons/473463 to your computer and use it in GitHub Desktop.
Save kylelemons/473463 to your computer and use it in GitHub Desktop.
func CriticalDataManager(cmdchan chan CriticalCommand) {
queued := new(vector.Vector)
critical := 0
for {
var rawcmd CriticalCommand
if critical == 0 && queued.Len() > 0 {
// Process the queued-up commands when we aren't in critical section
rawcmd := queued.At(0)
queued.Delete(0)
} else {
// Get new commands when the queue is empty
rawcmd = <-cmdchan
if closed(rawcmd) {
break
}
}
// Queue up commands not from the critical sender when in critical section
if critical != 0 && rawcmd.Sender() != critical {
queued.Push(rawcmd)
continue
}
// Process commands
switch cmd := rawcmd.(type) {
case CriticalSectionBegin:
critical = cmd.sender // enter critical section
case CriticalSectionEnd:
critical = 0 // exit critical section
case ...
}
rawcmd.Result() <- true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment