-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncio.go
55 lines (47 loc) · 1.12 KB
/
asyncio.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
package engine
import (
"github.com/mumax/3/cuda"
"github.com/mumax/3/timer"
)
// Asynchronous I/O queue flushes data to disk while simulation keeps running.
// See save.go, autosave.go
var (
saveQue chan func() // passes save requests from runDownloader to runSaver
done = make(chan bool) // marks output server is completely done after closing dlQue
nOutBuf int // number of output buffers actually in use (<= maxOutputQueLen)
)
const maxOutputQueLen = 16 // number of outputs that can be queued for asynchronous I/O.
func init() {
saveQue = make(chan func())
go runSaver()
}
func queOutput(f func()) {
if cuda.Synchronous {
timer.Start("io")
}
saveQue <- f
if cuda.Synchronous {
timer.Stop("io")
}
}
// Continuously executes tasks the from SaveQue channel.
func runSaver() {
for f := range saveQue {
f()
}
done <- true
}
// Finalizer function called upon program exit.
// Waits until all asynchronous output has been saved.
func drainOutput() {
if saveQue != nil {
if cuda.Synchronous {
timer.Start("io")
}
close(saveQue)
<-done
if cuda.Synchronous {
timer.Stop("io")
}
}
}