Skip to content

Instantly share code, notes, and snippets.

@axw
Created May 1, 2012 07:26
Show Gist options
  • Save axw/2565914 to your computer and use it in GitHub Desktop.
Save axw/2565914 to your computer and use it in GitHub Desktop.
YieldUntilAllOtherBlock
package main
import (
"fmt"
"os/exec"
"runtime"
"strings"
"syscall"
)
func YieldUntilAllOtherBlock(tids []int) {
do := func (c chan bool) {
runtime.LockOSThread()
anyRunning := true
for anyRunning {
anyRunning = false
for _, tid := range tids {
stat := fmt.Sprintf("/proc/%v/stat", tid)
o, e := exec.Command("/bin/cat", stat).CombinedOutput()
if e != nil {
panic(e)
}
state := strings.Split(string(o), " ")[2]
if state == "R" {
anyRunning = true
break
}
}
if anyRunning {
fmt.Println("try again")
}
}
runtime.UnlockOSThread()
c<-true
}
c := make(chan bool)
go do(c)
<-c
}
func forever(c chan int) {
fmt.Println(syscall.Gettid())
c<-syscall.Gettid()
for {
for x := 0; x < 1000000000; {
x++
}
fmt.Println("done")
//os.Stdin.Read(make([]byte, 1))
fmt.Println(<-c)
}
}
func main() {
c := make(chan int)
go forever(c)
tid1 := <-c
go forever(c)
tid2 := <-c
YieldUntilAllOtherBlock([]int{tid1, tid2})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment