tools/gen-device-avr: process files in parallel

This significantly speeds up processing of the files.
Этот коммит содержится в:
Ayke van Laethem 2019-11-28 17:25:17 +01:00 коммит произвёл Ron Evans
родитель 24259cbb5f
коммит 06647aab24

Просмотреть файл

@ -8,8 +8,10 @@ import (
"math/bits" "math/bits"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strconv" "strconv"
"strings" "strings"
"sync"
) )
type AVRToolsDeviceFile struct { type AVRToolsDeviceFile struct {
@ -426,31 +428,67 @@ __num_isrs = {{.numInterrupts}};
return t.Execute(out, device.metadata) return t.Execute(out, device.metadata)
} }
func processFile(filepath, outdir string) error {
device, err := readATDF(filepath)
if err != nil {
return err
}
err = writeGo(outdir, device)
if err != nil {
return err
}
err = writeAsm(outdir, device)
if err != nil {
return err
}
return writeLD(outdir, device)
}
func generate(indir, outdir string) error { func generate(indir, outdir string) error {
// Read list of ATDF files to process.
matches, err := filepath.Glob(indir + "/*.atdf") matches, err := filepath.Glob(indir + "/*.atdf")
if err != nil { if err != nil {
return err return err
} }
// Start worker goroutines.
var wg sync.WaitGroup
workChan := make(chan string)
errChan := make(chan error, 1)
for i := 0; i < runtime.NumCPU(); i++ {
go func() {
for filepath := range workChan {
err := processFile(filepath, outdir)
wg.Done()
if err != nil {
// Store error to errChan if no error was stored before.
select {
case errChan <- err:
default:
}
}
}
}()
}
// Submit all jobs to the goroutines.
wg.Add(len(matches))
for _, filepath := range matches { for _, filepath := range matches {
fmt.Println(filepath) fmt.Println(filepath)
device, err := readATDF(filepath) workChan <- filepath
if err != nil { }
return err close(workChan)
}
err = writeGo(outdir, device) // Wait until all workers have finished.
if err != nil { wg.Wait()
return err
} // Check for an error.
err = writeAsm(outdir, device) select {
if err != nil { case err := <-errChan:
return err return err
} default:
err = writeLD(outdir, device) return nil
if err != nil {
return err
}
} }
return nil
} }
func main() { func main() {