-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpmt1.go
82 lines (66 loc) · 2.03 KB
/
mpmt1.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
// mpmt1.go: A stupid simple example of Go language goroutine.
//
// Currently onlly goroutine mode is supported.
//
// License:
// Apache License, Version 2.0
// History:
// * 2021/12/24 v0.1 Initial version.
// Author:
// Masanori Itoh <[email protected]>
// TODO:
// * blushup
package main
import (
"fmt"
"syscall"
"os"
"flag"
)
func worker(id int, ch_in <-chan uint64, ch_out chan<- uint64) {
var tv syscall.Timeval
var ts, ts_save, ts_diff uint64
duration := <-ch_in
fmt.Printf("worker: started. id: %d duration: %d (us)\n", id, duration)
syscall.Gettimeofday(&tv)
ts_save = uint64(tv.Sec) * 1000 * 1000 + uint64(tv.Usec)
for {
syscall.Gettimeofday(&tv)
ts = uint64(tv.Sec) * 1000 * 1000 + uint64(tv.Usec)
ts_diff = ts - ts_save
if ts_diff >= duration {
fmt.Printf("Expired! %d / %d(us)\n", ts_diff, duration)
break
}
}
ch_out <- uint64(id)
fmt.Println("worker: exiting...")
}
func main() {
var duration = flag.Int("d", 10, "duration")
var num_context = flag.Int("n", 4, "number of execution contexts")
var mode = flag.String("m", "g", "mode: [gG]: goroutine,...")
flag.Parse()
fmt.Printf("num_context: %d, duration: %d, mode: %s\n",
*num_context, *duration, *mode)
if *mode != "g" && *mode != "G" {
fmt.Printf("main: mode: %d not supported\n", *mode)
os.Exit(1)
}
//create channels
ch_send := make(chan uint64, *num_context)
ch_recv := make(chan uint64, *num_context)
//create goroutines
for i := 0; i < *num_context; i++ {
go worker(i, ch_send, ch_recv)
}
//start goroutine (telling duration by sending a message)
for i := 0; i < *num_context; i++ {
ch_send <- uint64(*duration * 1000 * 1000)
}
//wait for goroutine completions
for i := 0; i < *num_context; i++ {
fmt.Printf("i: %d id: %d\n", i, <-ch_recv)
}
fmt.Println("done")
}