Skip to content

Commit

Permalink
parallelize
Browse files Browse the repository at this point in the history
  • Loading branch information
archie2x committed Aug 30, 2024
1 parent b283492 commit be833e2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 20 deletions.
20 changes: 10 additions & 10 deletions tools/tinygoize/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"strings"
)

type BuildResult int
type BuildCode int

const (
BuildResultSuccess BuildResult = iota
BuildResultFailed
BuildResultExclude
BuildResultFatal
BuildCodeSuccess BuildCode = iota
BuildCodeFailed
BuildCodeExclude
BuildCodeFatal
)

// Additional tags required for specific commands. Assume command names unique
Expand Down Expand Up @@ -65,7 +65,7 @@ func isExcluded(dir string) bool {
}

// "tinygo build" in directory 'dir'
func build(tinygo *string, dir string) (BuildResult, error) {
func build(tinygo *string, dir string) (BuildCode, error) {
log.Printf("%s Building...\n", dir)

tags := []string{"tinygo.enable"}
Expand All @@ -80,15 +80,15 @@ func build(tinygo *string, dir string) (BuildResult, error) {
if err != nil {
berr, ok := err.(*exec.ExitError)
if !ok {
return BuildResultFatal, err
return BuildCodeFatal, err
}
if isExcluded(dir) {
log.Printf("%v EXCLUDED\n", dir)
return BuildResultExclude, nil
return BuildCodeExclude, nil
}
log.Printf("%v FAILED %v\n", dir, berr)
return BuildResultFailed, nil
return BuildCodeFailed, nil
}
log.Printf("%v PASS\n", dir)
return BuildResultSuccess, nil
return BuildCodeSuccess, nil
}
65 changes: 55 additions & 10 deletions tools/tinygoize/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"path/filepath"
"sort"
"strings"
"sync"
"runtime"
)

// Track set of passing, failing, and excluded commands
Expand All @@ -36,20 +38,63 @@ func tinygoVersion(tinygo *string) (string, error) {
return strings.TrimSpace(string(out)), err
}

type BuildResult struct {
dir string
code BuildCode
err error
}

func worker(tinygo *string, tasks <-chan string, results chan<- BuildResult, wg *sync.WaitGroup) {
defer wg.Done()
for dir := range tasks {
code, err := build(tinygo, dir)
// Send the result back to the main routine
results <- BuildResult {
dir: dir,
code: code,
err: err,
}
}
}

// "tinygo build" in each of directories 'dirs'
func buildDirs(tinygo *string, dirs []string) (status BuildStatus, err error) {
for _, dir := range dirs {
result, err := build(tinygo, dir)
if err != nil {
nproc := runtime.NumCPU()
tasks := make(chan string)
results := make(chan BuildResult)
var wg sync.WaitGroup

// Start workers
for i := 0; i < nproc; i++ {
wg.Add(1)
go worker(tinygo, tasks, results, &wg)
}

// Assign tasks
go func() {
for _, dir := range dirs {
tasks <- dir
}
close(tasks) // close channel signals workers to exit when done
}()

// Collect results
go func() {
wg.Wait()
close(results) // close results channel after all workers done
}()

for result := range results {
if result.err != nil {
break
}
switch result {
case BuildResultExclude:
status.excluded = append(status.excluded, dir)
case BuildResultFailed:
status.failing = append(status.failing, dir)
case BuildResultSuccess:
status.passing = append(status.passing, dir)
switch result.code {
case BuildCodeExclude:
status.excluded = append(status.excluded, result.dir)
case BuildCodeFailed:
status.failing = append(status.failing, result.dir)
case BuildCodeSuccess:
status.passing = append(status.passing, result.dir)
}
}
return
Expand Down

0 comments on commit be833e2

Please sign in to comment.