1 | package main |
---|
2 | |
---|
3 | import ( |
---|
4 | "fmt" |
---|
5 | "sync" |
---|
6 | "time" |
---|
7 | "runtime" |
---|
8 | "os" |
---|
9 | "strconv" |
---|
10 | ) |
---|
11 | |
---|
12 | var Sets, Channels, ChannelSize int = 1, 2, 100 |
---|
13 | var cons_done, prod_done bool = false, false; |
---|
14 | var total_operations uint64 = 0 |
---|
15 | var m sync.Mutex |
---|
16 | |
---|
17 | var prodJoin chan int = make(chan int) |
---|
18 | var consJoin chan int = make(chan int) |
---|
19 | |
---|
20 | func selectconsumer( chans [] chan uint64 ) { |
---|
21 | var count uint64 = 0 |
---|
22 | for { |
---|
23 | if cons_done { break } |
---|
24 | select { |
---|
25 | case <- chans[0]: |
---|
26 | case <- chans[1]: |
---|
27 | } |
---|
28 | if ! prod_done { count++ } |
---|
29 | } |
---|
30 | m.Lock() |
---|
31 | total_operations += count |
---|
32 | m.Unlock() |
---|
33 | consJoin <- 0 |
---|
34 | } |
---|
35 | |
---|
36 | func consumer( channel chan uint64 ) { |
---|
37 | var count uint64 = 0 |
---|
38 | for { |
---|
39 | if cons_done { break } |
---|
40 | <-channel |
---|
41 | if ! prod_done { count++ } |
---|
42 | } |
---|
43 | m.Lock() |
---|
44 | total_operations += count |
---|
45 | m.Unlock() |
---|
46 | consJoin <- 0 |
---|
47 | } |
---|
48 | |
---|
49 | func selectproducer( chans [] chan uint64 ) { |
---|
50 | var count uint64 = 0 |
---|
51 | var checksum uint64 = 0 |
---|
52 | for { |
---|
53 | if prod_done { break } |
---|
54 | checksum = checksum ^ count |
---|
55 | select { |
---|
56 | case chans[0] <- count: |
---|
57 | case chans[1] <- count: |
---|
58 | } |
---|
59 | count++ |
---|
60 | } |
---|
61 | prodJoin <- 0 |
---|
62 | } |
---|
63 | |
---|
64 | func producer( channel chan uint64 ) { |
---|
65 | var count uint64 = 0 |
---|
66 | for { |
---|
67 | if prod_done { break } |
---|
68 | channel <- count |
---|
69 | count++ |
---|
70 | } |
---|
71 | prodJoin <- 0 |
---|
72 | } |
---|
73 | |
---|
74 | func usage() { |
---|
75 | fmt.Printf( "Usage: %v " + |
---|
76 | "[ sets (> 0) | 'd' (default %v) ] " + |
---|
77 | "[ ChannelSize (> 0) | 'd' (default %v) ]\n", |
---|
78 | os.Args[0], Sets, ChannelSize ); |
---|
79 | os.Exit( 1 ); |
---|
80 | } |
---|
81 | |
---|
82 | func main() { |
---|
83 | switch len( os.Args ) { |
---|
84 | case 3: |
---|
85 | if os.Args[2] != "d" { // default ? |
---|
86 | ChannelSize, _ = strconv.Atoi( os.Args[2] ) |
---|
87 | if ChannelSize < 0 { usage(); } |
---|
88 | } // if |
---|
89 | fallthrough |
---|
90 | case 2: |
---|
91 | if os.Args[1] != "d" { // default ? |
---|
92 | Sets, _ = strconv.Atoi( os.Args[1] ) |
---|
93 | if Sets < 1 { usage(); } |
---|
94 | } // if |
---|
95 | case 1: // use defaults |
---|
96 | default: |
---|
97 | usage(); |
---|
98 | } // switch |
---|
99 | runtime.GOMAXPROCS( Sets * 2 + Sets * Channels * 2 ); |
---|
100 | |
---|
101 | // fmt.Println("Processors: ",Processors," Channels: ",Channels," ProdsPerChan: ",ProdsPerChan," ConsPerChan: ",ConsPerChan," Channel Size: ",ChannelSize) |
---|
102 | |
---|
103 | chans := make( [] chan uint64, Channels ) |
---|
104 | for i := range chans { |
---|
105 | chans[i] = make(chan uint64, ChannelSize) |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | for j := 0; j < Sets; j++ { |
---|
110 | go selectproducer( chans ) |
---|
111 | go selectconsumer( chans ) |
---|
112 | for i := 0; i < Channels; i++ { |
---|
113 | go producer( chans[i] ) |
---|
114 | go consumer( chans[i] ) |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | // wait 10 seconds |
---|
120 | time.Sleep(time.Second * 10) |
---|
121 | // fmt.Println("prod done\n") |
---|
122 | prod_done = true |
---|
123 | for j := 0; j < Sets + Sets * Channels; j++ { |
---|
124 | <-prodJoin |
---|
125 | } |
---|
126 | // fmt.Println("cons done\n") |
---|
127 | cons_done = true |
---|
128 | for i := range chans { |
---|
129 | close(chans[i]) |
---|
130 | } |
---|
131 | |
---|
132 | for j := 0; j < Sets + Sets * Channels; j++{ |
---|
133 | <-consJoin |
---|
134 | } |
---|
135 | fmt.Println(total_operations) |
---|
136 | } |
---|