mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime: use func value for parfor body
Yet another leftover from C: parfor took a func value for the callback, casted it to an unsafe.Pointer for storage, and then casted it back to a func value to call it. This is unnecessary, so just store the body as a func value. Beyond general cleanup, this also eliminates the last use of unsafe in parfor. Change-Id: Ia904af7c6c443ba75e2699835aee8e9a39b26dd8 Reviewed-on: https://go-review.googlesource.com/3396 Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
This commit is contained in:
parent
ebbdf2a14c
commit
428afae027
2 changed files with 9 additions and 11 deletions
|
|
@ -36,7 +36,7 @@ func LFStackPop(head *uint64) *LFNode {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ParFor struct {
|
type ParFor struct {
|
||||||
body *byte
|
body func(*ParFor, uint32)
|
||||||
done uint32
|
done uint32
|
||||||
Nthr uint32
|
Nthr uint32
|
||||||
thrseq uint32
|
thrseq uint32
|
||||||
|
|
|
||||||
|
|
@ -6,16 +6,14 @@
|
||||||
|
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import "unsafe"
|
|
||||||
|
|
||||||
// A parfor holds state for the parallel for operation.
|
// A parfor holds state for the parallel for operation.
|
||||||
type parfor struct {
|
type parfor struct {
|
||||||
body unsafe.Pointer // go func(*parfor, uint32), executed for each element
|
body func(*parfor, uint32) // executed for each element
|
||||||
done uint32 // number of idle threads
|
done uint32 // number of idle threads
|
||||||
nthr uint32 // total number of threads
|
nthr uint32 // total number of threads
|
||||||
thrseq uint32 // thread id sequencer
|
thrseq uint32 // thread id sequencer
|
||||||
cnt uint32 // iteration space [0, cnt)
|
cnt uint32 // iteration space [0, cnt)
|
||||||
wait bool // if true, wait while all threads finish processing,
|
wait bool // if true, wait while all threads finish processing,
|
||||||
// otherwise parfor may return while other threads are still working
|
// otherwise parfor may return while other threads are still working
|
||||||
|
|
||||||
thr []parforthread // thread descriptors
|
thr []parforthread // thread descriptors
|
||||||
|
|
@ -63,7 +61,7 @@ func parforsetup(desc *parfor, nthr, n uint32, wait bool, body func(*parfor, uin
|
||||||
throw("parfor: invalid args")
|
throw("parfor: invalid args")
|
||||||
}
|
}
|
||||||
|
|
||||||
desc.body = *(*unsafe.Pointer)(unsafe.Pointer(&body))
|
desc.body = body
|
||||||
desc.done = 0
|
desc.done = 0
|
||||||
desc.nthr = nthr
|
desc.nthr = nthr
|
||||||
desc.thrseq = 0
|
desc.thrseq = 0
|
||||||
|
|
@ -91,7 +89,7 @@ func parfordo(desc *parfor) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// If single-threaded, just execute the for serially.
|
// If single-threaded, just execute the for serially.
|
||||||
body := *(*func(*parfor, uint32))(unsafe.Pointer(&desc.body))
|
body := desc.body
|
||||||
if desc.nthr == 1 {
|
if desc.nthr == 1 {
|
||||||
for i := uint32(0); i < desc.cnt; i++ {
|
for i := uint32(0); i < desc.cnt; i++ {
|
||||||
body(desc, i)
|
body(desc, i)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue