/
handlers.go
72 lines (62 loc) · 1.28 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package goat
import (
"runtime"
"time"
"runtime/trace"
"os"
"fmt"
"strconv"
"log"
)
func Sched_Handler(){
randBound_yield()
// other handlers can come down here
}
func StartRace() {
fmt.Println("GOAT start...")
maxprocs, err := strconv.Atoi(os.Getenv("GOATMAXPROCS"))
if err != nil || maxprocs < 1{
panic("Invalid GOATMAXPROCS")
}
runtime.GOMAXPROCS(maxprocs)
}
func Start() chan interface{}{
fmt.Println("GOAT start...")
maxprocs, err := strconv.Atoi(os.Getenv("GOATMAXPROCS"))
if err != nil || maxprocs < 1{
panic("Invalid GOATMAXPROCS")
}
fmt.Println("GOAT_MAXPROCS = ", maxprocs)
runtime.GOMAXPROCS(maxprocs)
trace.Start(os.Stderr)
ch := make(chan interface{})
return ch
}
func Watch(ch chan interface{}){
fmt.Println("GOAT stop...")
to, err := strconv.Atoi(os.Getenv("GOATTO"))
if err != nil{
panic("GOATTO not set")
}
select {
case <- ch:
fmt.Println("GOAT finished (normal)")
ch <- 0
case <- time.After(time.Second * time.Duration(to)):
trace.Stop()
fmt.Println("GOAT stopped (timeout)")
os.Exit(0)
}
}
func Stop(ch chan interface{}){
if err := recover() ; err != nil{
// an error occured
//time.Sleep(time.Millisecond)
trace.Stop()
log.Println(err)
}
ch <- true
<-ch
time.Sleep(time.Millisecond)
trace.Stop()
}