Skip to content

Commit

Permalink
The number of jobs in library detection now follows --jobs flag
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Jun 11, 2024
1 parent 8afa7d8 commit 55ee2f5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions internal/arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ func (b *Builder) preprocess() error {
b.librariesBuildPath,
b.buildProperties,
b.targetPlatform.Platform.Architecture,
b.jobs,
)
if err != nil {
return err
Expand Down
8 changes: 5 additions & 3 deletions internal/arduino/builder/internal/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ func (l *SketchLibrariesDetector) FindIncludes(
librariesBuildPath *paths.Path,
buildProperties *properties.Map,
platformArch string,
jobs int,
) error {
err := l.findIncludes(ctx, buildPath, buildCorePath, buildVariantPath, sketchBuildPath, sketch, librariesBuildPath, buildProperties, platformArch)
err := l.findIncludes(ctx, buildPath, buildCorePath, buildVariantPath, sketchBuildPath, sketch, librariesBuildPath, buildProperties, platformArch, jobs)
if err != nil && l.onlyUpdateCompilationDatabase {
l.logger.Info(
fmt.Sprintf(
Expand All @@ -216,6 +217,7 @@ func (l *SketchLibrariesDetector) findIncludes(
librariesBuildPath *paths.Path,
buildProperties *properties.Map,
platformArch string,
jobs int,
) error {
librariesResolutionCache := buildPath.Join("libraries.cache")
if l.useCachedLibrariesResolution && librariesResolutionCache.Exist() {
Expand All @@ -238,7 +240,7 @@ func (l *SketchLibrariesDetector) findIncludes(
}

// Pre-run cache entries
l.preRunner = runner.New(ctx)
l.preRunner = runner.New(ctx, jobs)
for _, entry := range l.cache.EntriesAhead() {
if entry.Compile != nil && entry.CompileTask != nil {
upToDate, _ := entry.Compile.ObjFileIsUpToDate()
Expand Down Expand Up @@ -279,7 +281,7 @@ func (l *SketchLibrariesDetector) findIncludes(

// Create a new pre-runner if the previous one was cancelled
if l.preRunner == nil {
l.preRunner = runner.New(ctx)
l.preRunner = runner.New(ctx, jobs)
// Push in the remainder of the queue
for _, sourceFile := range *sourceFileQueue {
l.preRunner.Enqueue(l.gccPreprocessTask(sourceFile, buildProperties))
Expand Down
9 changes: 7 additions & 2 deletions internal/arduino/builder/internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ func (cmd *enqueuedCommand) String() string {
return cmd.task.String()
}

func New(inCtx context.Context) *Runner {
// New creates a new Runner with the given number of workers.
// If workers is 0, the number of workers will be the number of available CPUs.
func New(inCtx context.Context, workers int) *Runner {
ctx, cancel := context.WithCancel(inCtx)
queue := make(chan *enqueuedCommand, 1000)
r := &Runner{
Expand All @@ -52,7 +54,10 @@ func New(inCtx context.Context) *Runner {
}

// Spawn workers
for i := 0; i < runtime.NumCPU(); i++ {
if workers == 0 {
workers = runtime.NumCPU()
}
for i := 0; i < workers; i++ {
r.wg.Add(1)
go func() {
worker(ctx, queue)
Expand Down
2 changes: 1 addition & 1 deletion internal/arduino/builder/internal/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
func TestRunMultipleTask(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
r := runner.New(ctx)
r := runner.New(ctx, 0)
r.Enqueue(runner.NewTask("bash", "-c", "sleep 1 ; echo -n 0"))
r.Enqueue(runner.NewTask("bash", "-c", "sleep 2 ; echo -n 1"))
r.Enqueue(runner.NewTask("bash", "-c", "sleep 3 ; echo -n 2"))
Expand Down

0 comments on commit 55ee2f5

Please sign in to comment.