github.com/DARA-Project/GoDist-Scheduler@v0.0.0-20201030134746-668de4acea0d/structured/dphil_local/dining_philosophers.go (about) 1 package main 2 3 import "runtime" 4 5 import ( 6 "fmt" 7 "math/rand" 8 "time" 9 ) 10 11 type Philosopher struct { 12 name string 13 chopstick chan bool 14 neighbor *Philosopher 15 } 16 17 func makePhilosopher(name string, neighbor *Philosopher) *Philosopher { 18 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:15:19") 19 phil := &Philosopher{name, make(chan bool, 1), neighbor} 20 phil.chopstick <- true 21 return phil 22 } 23 24 func (phil *Philosopher) think() { 25 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:21:24") 26 fmt.Printf("%v is thinking.\n", phil.name) 27 time.Sleep(time.Duration(rand.Int63n(1e9))) 28 } 29 30 func (phil *Philosopher) eat() { 31 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:26:29") 32 fmt.Printf("%v is eating.\n", phil.name) 33 time.Sleep(time.Duration(rand.Int63n(1e9))) 34 } 35 36 func (phil *Philosopher) getChopsticks() { 37 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:31:33") 38 timeout := make(chan bool) 39 go func() { 40 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:33:36") 41 time.Sleep(1e9) 42 timeout <- true 43 }() 44 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:37:39") 45 <-phil.chopstick 46 fmt.Printf("%v got his chopstick.\n", phil.name) 47 select { 48 case <-phil.neighbor.chopstick: 49 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:40:43") 50 fmt.Printf("%v got %v's chopstick.\n", phil.name, phil.neighbor.name) 51 fmt.Printf("%v has two chopsticks.\n", phil.name) 52 return 53 case <-timeout: 54 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:44:47") 55 phil.chopstick <- true 56 phil.think() 57 phil.getChopsticks() 58 } 59 } 60 61 func (phil *Philosopher) returnChopsticks() { 62 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:51:54") 63 phil.chopstick <- true 64 phil.neighbor.chopstick <- true 65 } 66 67 func (phil *Philosopher) dine(announce chan *Philosopher) { 68 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:56:62") 69 phil.think() 70 phil.getChopsticks() 71 phil.eat() 72 phil.returnChopsticks() 73 announce <- phil 74 } 75 76 func main() { 77 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:64:69") 78 79 names := []string{"A", "B", "C"} 80 philosophers := make([]*Philosopher, len(names)) 81 var phil *Philosopher 82 for i, name := range names { 83 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:69:72") 84 phil = makePhilosopher(name, phil) 85 philosophers[i] = phil 86 } 87 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:73:77") 88 philosophers[0].neighbor = phil 89 fmt.Printf("There are %v philosophers sitting at a table.\n", len(philosophers)) 90 fmt.Println("They each have one chopstick, and must borrow from their neighbor to eat.") 91 announce := make(chan *Philosopher) 92 for _, phil := range philosophers { 93 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:77:79") 94 go phil.dine(announce) 95 } 96 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:80:80") 97 for i := 0; i < len(names); i++ { 98 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:80:82") 99 phil := <-announce 100 go func() { 101 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:82:87") 102 103 time.Sleep(time.Second) 104 close(phil.chopstick) 105 }() 106 runtime.ReportBlockCoverage("../structured/dphil_local/dining_philosophers.go:88:88") 107 fmt.Printf("%v is done dining.\n", phil.name) 108 } 109 }