-
Notifications
You must be signed in to change notification settings - Fork 1
/
do-func.go
47 lines (39 loc) · 1.13 KB
/
do-func.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
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
// DoThread is invoked in a go statement and executes op.
// g0 receives errors and is the wait-for function.
func DoThread(op func() (err error), g0 Go) {
var err error
defer g0.Done(&err)
defer Recover(Annotation(), &err, NoOnError)
err = op()
}
func DoProcThread(op func(), g0 Go) {
var err error
defer g0.Done(&err)
defer Recover(Annotation(), &err, NoOnError)
op()
}
// DoThreadError is a goroutine that returns its error separately.
func DoThreadError(op func() (err error), errCh chan<- error, g0 Go) {
var err error
defer g0.Done(&err)
defer func() {
errCh <- err
err = nil
}()
defer Recover(Annotation(), &err, NoOnError)
err = op()
}
// DoGoGetError executes op in a thread.
// err contains any error, error are not submitted to Go object.
// DoGoGetError blocks until the goroutine completes.
func DoGoGetError(op func() (err error), g0 Go) (err error) {
errCh := make(chan error)
go DoThreadError(op, errCh, g0.Go())
err = <-errCh // block until goroutine completes
return
}