Created
July 13, 2010 04:47
-
-
Save kylelemons/473463 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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