2024-01-03 11:59:45 -06:00
|
|
|
// 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 debugger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2024-01-18 22:36:35 -06:00
|
|
|
"go.wit.com/gui"
|
2024-01-18 01:34:32 -06:00
|
|
|
"go.wit.com/lib/gadgets"
|
|
|
|
"go.wit.com/log"
|
2024-01-03 11:59:45 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var debugWG *sync.WaitGroup
|
|
|
|
var debugNumberChan chan int
|
|
|
|
|
2024-01-20 20:24:58 -06:00
|
|
|
func DebugGoChannels() *gadgets.BasicWindow {
|
2024-01-04 23:28:55 -06:00
|
|
|
var w *gadgets.BasicWindow
|
|
|
|
var g *gui.Node
|
2024-01-03 11:59:45 -06:00
|
|
|
|
2024-01-20 20:24:58 -06:00
|
|
|
p := gui.TreeRoot()
|
|
|
|
|
2024-01-04 23:28:55 -06:00
|
|
|
w = gadgets.NewBasicWindow(p, "Debug GO Channels")
|
2024-01-05 16:56:03 -06:00
|
|
|
w.Draw()
|
2024-01-04 23:28:55 -06:00
|
|
|
g = w.Box().NewGroup("Channel stuff").Pad()
|
2024-01-03 11:59:45 -06:00
|
|
|
|
|
|
|
// var debugWG sync.WaitGroup
|
2024-01-18 01:34:32 -06:00
|
|
|
g.NewButton("init()", func() {
|
|
|
|
if debugNumberChan == nil {
|
2024-01-08 22:33:18 -06:00
|
|
|
log.Log(CHAN, "making debugNumberChan channel")
|
2024-01-03 11:59:45 -06:00
|
|
|
debugNumberChan = make(chan int)
|
|
|
|
} else {
|
2024-01-08 22:33:18 -06:00
|
|
|
log.Log(CHAN, "debugNumberChan already made")
|
2024-01-03 11:59:45 -06:00
|
|
|
}
|
|
|
|
debugWG = new(sync.WaitGroup)
|
|
|
|
})
|
2024-01-18 01:34:32 -06:00
|
|
|
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")
|
2024-01-03 11:59:45 -06:00
|
|
|
})
|
2024-01-18 01:34:32 -06:00
|
|
|
g.NewButton("sendNumber(2) (chan <- 2, 4)", func() {
|
2024-01-03 11:59:45 -06:00
|
|
|
debugWG.Add(1)
|
|
|
|
debugWG.Add(1)
|
|
|
|
go sendNumber(2)
|
|
|
|
go sendNumber(4)
|
|
|
|
})
|
2024-01-18 01:34:32 -06:00
|
|
|
g.NewButton("sendNumber(1) (chan <- 7)", func() {
|
2024-01-03 11:59:45 -06:00
|
|
|
debugWG.Add(1)
|
|
|
|
go sendNumber(7)
|
|
|
|
})
|
2024-01-18 01:34:32 -06:00
|
|
|
g.NewButton("send 4 numbers (chan <- int)", func() {
|
2024-01-08 22:33:18 -06:00
|
|
|
log.Log(CHAN, "generateNumbers(4)")
|
2024-01-03 11:59:45 -06:00
|
|
|
go generateNumbers(4)
|
|
|
|
})
|
2024-01-18 01:34:32 -06:00
|
|
|
g.NewButton("debugWG.Done()", func() {
|
2024-01-08 22:33:18 -06:00
|
|
|
log.Log(CHAN, "ran debugWG.Done()")
|
2024-01-03 11:59:45 -06:00
|
|
|
debugWG.Done()
|
|
|
|
})
|
2024-01-18 01:34:32 -06:00
|
|
|
g.NewButton("close chan", func() {
|
2024-01-08 22:33:18 -06:00
|
|
|
log.Log(CHAN, "close() on", debugNumberChan)
|
2024-01-03 11:59:45 -06:00
|
|
|
close(debugNumberChan)
|
|
|
|
})
|
2024-01-18 01:34:32 -06:00
|
|
|
g.NewButton("print", func() {
|
2024-01-08 22:33:18 -06:00
|
|
|
log.Log(CHAN, "waitgroup counter is ?")
|
2024-01-03 11:59:45 -06:00
|
|
|
})
|
2024-01-04 23:28:55 -06:00
|
|
|
return w
|
2024-01-03 11:59:45 -06:00
|
|
|
}
|
2024-01-04 23:28:55 -06:00
|
|
|
|
2024-01-03 11:59:45 -06:00
|
|
|
func sendNumber(i int) {
|
2024-01-08 22:33:18 -06:00
|
|
|
log.Log(CHAN, "START debugNumberChan <-", i, " (sending", i, "to channel)")
|
2024-01-03 11:59:45 -06:00
|
|
|
debugNumberChan <- i
|
|
|
|
debugWG.Wait()
|
2024-01-08 22:33:18 -06:00
|
|
|
log.Log(CHAN, "END debugNumberChan sendNumber() done", i)
|
2024-01-03 11:59:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateNumbers(total int) {
|
|
|
|
fmt.Printf("START generateNumbers()\n")
|
|
|
|
for idx := 1; idx <= total; idx++ {
|
2024-01-08 22:33:18 -06:00
|
|
|
log.Log(CHAN, "ran debugNumberChan <= idx where idx =", idx)
|
2024-01-03 11:59:45 -06:00
|
|
|
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
|
2024-01-08 22:33:18 -06:00
|
|
|
log.Log(CHAN, "START printInt", name, "read debugNumberChan()")
|
2024-01-03 11:59:45 -06:00
|
|
|
for num := range debugNumberChan {
|
2024-01-18 01:34:32 -06:00
|
|
|
log.Log(CHAN, "printInt()", name, "read", num, "from channel")
|
2024-01-03 11:59:45 -06:00
|
|
|
debugWG.Done()
|
2024-01-18 01:34:32 -06:00
|
|
|
if tmp == i {
|
2024-01-03 11:59:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
tmp += 1
|
|
|
|
}
|
|
|
|
fmt.Printf("END printInt()", name, "read debugNumberChan\n")
|
|
|
|
}
|