-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (82 loc) · 1.95 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
mutex "goodlock/GoodMutex"
tree "goodlock/LockTree"
)
type algo struct {
trees *[]*tree.LockTree
}
func New(trees *[]*tree.LockTree) *algo {
return &algo{trees}
}
func (a *algo) Analyze() {
for i, e := range *a.trees {
fmt.Printf("%d: %v\n", i, e)
}
}
func exampleThread1(id int, locks []*mutex.GoodMutex, stopchan chan string, treechan chan *tree.LockTree) {
lockTree := tree.New(id)
go func() {
for {
// Content here!
locks[0].Lock(lockTree)
locks[2].Lock(lockTree)
locks[1].Lock(lockTree)
locks[1].Unlock(lockTree)
locks[3].Lock(lockTree)
locks[3].Unlock(lockTree)
locks[0].Unlock(lockTree)
locks[3].Lock(lockTree)
locks[1].Lock(lockTree)
locks[2].Lock(lockTree)
locks[2].Unlock(lockTree)
locks[1].Unlock(lockTree)
locks[3].Unlock(lockTree)
}
}()
<-stopchan
treechan <- lockTree
}
func exampleThread2(id int, locks []*mutex.GoodMutex, stopchan chan string, treechan chan *tree.LockTree) {
lockTree := tree.New(id)
go func() {
for {
// Content here!
locks[0].Lock(lockTree)
locks[1].Lock(lockTree)
locks[2].Lock(lockTree)
locks[2].Unlock(lockTree)
locks[1].Unlock(lockTree)
locks[0].Unlock(lockTree)
locks[3].Lock(lockTree)
locks[2].Lock(lockTree)
locks[1].Lock(lockTree)
locks[1].Unlock(lockTree)
locks[2].Unlock(lockTree)
locks[3].Unlock(lockTree)
}
}()
<-stopchan
treechan <- lockTree
}
// TODO: Fix. Help.
func main() {
var stop string
availableLocks := []*mutex.GoodMutex{mutex.NewMutex(), mutex.NewMutex(), mutex.NewMutex(), mutex.NewMutex()}
var trees []*tree.LockTree
stopchan := make(chan string)
treechan := make(chan *tree.LockTree)
go exampleThread1(1, availableLocks, stopchan, treechan)
go exampleThread2(2, availableLocks, stopchan, treechan)
fmt.Scanf("%s", &stop)
go func() {
for {
stopchan <- stop
}
}()
algo := New(&trees)
trees = append(trees, <-treechan)
trees = append(trees, <-treechan)
algo.Analyze()
}