summaryrefslogtreecommitdiff
path: root/debugGochan.go
blob: 3183bdb725e06d1dcdff29df52dc2dda317e68c2 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// https://www.digitalocean.com/community/tutorials/how-to-run-multiple-functions-concurrently-in-go
// who came up with the idea of making community tutorials. that was a good idea!

package gui

import (
	"fmt"
	"sync"
)

var debugWG *sync.WaitGroup
var debugNumberChan chan int

func (n *Node) debugGoChannels(makeWindow bool) {
	var w, g *Node

	// Either:
	// make a new window
	// make a new tab in the existing window
	if (makeWindow) {
		Config.Title = "Debug GO Channels"
		Config.Width = 300
		Config.Height = 400
		w = NewWindow()
		w.Custom = w.StandardClose
	} else {
		w = n.NewTab("Chan")
	}
	w.Dump()

	g = w.NewGroup("Channel stuff")

	// var debugWG sync.WaitGroup
	g.NewButton("init()", func () {
		if (debugNumberChan == nil) {
			log("making debugNumberChan channel")
			debugNumberChan = make(chan int)
		} else {
			log("debugNumberChan already made")
		}
		debugWG = new(sync.WaitGroup)
	})
	g.NewButton("go printInt(x) (read x values off the channel)", func () {
			debugWG.Add(1)
			go printInt(2, "routine1")
			debugWG.Add(1)
			go printInt(2, "routine2")
	})
	g.NewButton("sendNumber(2) (chan <- 2, 4)", func () {
		debugWG.Add(1)
		debugWG.Add(1)
		go sendNumber(2)
		go sendNumber(4)
	})
	g.NewButton("sendNumber(1) (chan <- 7)", func () {
		debugWG.Add(1)
		go sendNumber(7)
	})
	g.NewButton("send 4 numbers (chan <- int)", func () {
		log("generateNumbers(4)")
		go generateNumbers(4)
	})
	g.NewButton("debugWG.Done()", func () {
		log("ran debugWG.Done()")
		debugWG.Done()
	})
	g.NewButton("close chan", func () {
		close(debugNumberChan)
	})
	g.NewButton("print", func () {
		log("waitgroup counter is ?")
	})
}
func sendNumber(i int) {
	log("START debugNumberChan <-", i, "  (sending", i, "to channel)")
	debugNumberChan <- i
	debugWG.Wait()
	log("END   debugNumberChan sendNumber() done", i)
}

func generateNumbers(total int) {
	fmt.Printf("START generateNumbers()\n")
	for idx := 1; idx <= total; idx++ {
		log("ran debugNumberChan <= idx where idx =", idx)
		fmt.Printf("S generateNumbers() sending %d to channel\n", idx)
		debugNumberChan <- idx
		// res, err := (<-r)()
		fmt.Printf("E generateNumbers() sending %d to channel\n", idx)
	}
	debugWG.Wait()
	fmt.Printf("END   generateNumbers()\n")
}

// i equals the number of times to read values from the channel
func printInt(i int, name string) {
	tmp := 1
	log("START printInt", name, "read debugNumberChan()")
	for num := range debugNumberChan {
		log("printInt()",name, "read", num, "from channel")
		debugWG.Done()
		if (tmp == i) {
			return
		}
		tmp += 1
	}
	fmt.Printf("END printInt()", name, "read debugNumberChan\n")
}