-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (50 loc) · 1.03 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime/pprof"
"time"
)
var profile = flag.Bool("profile", false,
"whether to run a cpuprofile and output a .pprof file")
var locktype = flag.String("locktype", "c",
"values are either 'c' or 'go', to determine which ABQL implementation "+
"we'll use")
type ABQL interface {
Lock() (ticket int)
Unlock(ticket int)
}
func main() {
flag.Parse()
if *profile {
f, err := os.Create(fmt.Sprintf("%s.pprof", locktype))
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
var l ABQL
switch *locktype {
case "c":
l = NewC_ABQL()
case "go":
l = NewGo_ABQL()
}
fmt.Printf("Testing locktype '%s'\n", *locktype)
go func() {
ticket := l.Lock()
fmt.Println("got Lock in goroutine")
time.Sleep(3 * time.Second)
fmt.Println("unlocking in goroutine...")
l.Unlock(ticket)
}()
time.Sleep(time.Second)
ticket := l.Lock()
fmt.Println("got Lock in main")
fmt.Println("unlocking in main...")
l.Unlock(ticket)
fmt.Println("Done.")
}