Skip to content

Commit a90dd07

Browse files
committed
pkg/osutil: move Semaphore from pkg/instance
Semaphore is a very low-level primitive type, while pkg/instance is a very high-level package with lots of deps. Semaphore does not belong there, and may lead to cyclic deps if we use it more. Move it to pkg/osutil. It's not really OS-specific, but we don't have a better package.
1 parent bdb0720 commit a90dd07

File tree

5 files changed

+50
-42
lines changed

5 files changed

+50
-42
lines changed

pkg/bisect/bisect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ type Config struct {
3636
Syzkaller SyzkallerConfig
3737
Repro ReproConfig
3838
Manager *mgrconfig.Config
39-
BuildSemaphore *instance.Semaphore
40-
TestSemaphore *instance.Semaphore
39+
BuildSemaphore *osutil.Semaphore
40+
TestSemaphore *osutil.Semaphore
4141
BuildCPUs int
4242
// CrossTree specifies whether a cross tree bisection is to take place, i.e.
4343
// Kernel.Commit is not reachable from Kernel.Branch.

pkg/instance/instance.go

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ type Env interface {
4040
type env struct {
4141
cfg *mgrconfig.Config
4242
optionalFlags bool
43-
buildSem *Semaphore
44-
testSem *Semaphore
43+
buildSem *osutil.Semaphore
44+
testSem *osutil.Semaphore
4545
}
4646

4747
type BuildKernelConfig struct {
@@ -56,7 +56,7 @@ type BuildKernelConfig struct {
5656
BuildCPUs int
5757
}
5858

59-
func NewEnv(cfg *mgrconfig.Config, buildSem, testSem *Semaphore) (Env, error) {
59+
func NewEnv(cfg *mgrconfig.Config, buildSem, testSem *osutil.Semaphore) (Env, error) {
6060
if !vm.AllowsOvercommit(cfg.Type) {
6161
return nil, fmt.Errorf("test instances are not supported for %v VMs", cfg.Type)
6262
}
@@ -508,40 +508,6 @@ func RunnerCmd(prog, fwdAddr, os, arch string, poolIdx, vmIdx int, threaded, new
508508
"-threaded=%t -new-env=%t", prog, fwdAddr, os, arch, poolIdx, vmIdx, threaded, newEnv)
509509
}
510510

511-
type Semaphore struct {
512-
ch chan struct{}
513-
}
514-
515-
func NewSemaphore(count int) *Semaphore {
516-
s := &Semaphore{
517-
ch: make(chan struct{}, count),
518-
}
519-
for i := 0; i < count; i++ {
520-
s.Signal()
521-
}
522-
return s
523-
}
524-
525-
func (s *Semaphore) Wait() {
526-
<-s.ch
527-
}
528-
529-
func (s *Semaphore) WaitC() <-chan struct{} {
530-
return s.ch
531-
}
532-
533-
func (s *Semaphore) Available() int {
534-
return len(s.ch)
535-
}
536-
537-
func (s *Semaphore) Signal() {
538-
if av := s.Available(); av == cap(s.ch) {
539-
// Not super reliable, but let it be here just in case.
540-
panic(fmt.Sprintf("semaphore capacity (%d) is exceeded (%d)", cap(s.ch), av))
541-
}
542-
s.ch <- struct{}{}
543-
}
544-
545511
// RunSmokeTest executes syz-manager in the smoke test mode and returns two values:
546512
// The crash report, if the testing failed.
547513
// An error if there was a problem not related to testing the kernel.

pkg/osutil/semaphore.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2025 syzkaller project authors. All rights reserved.
2+
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3+
4+
package osutil
5+
6+
import (
7+
"fmt"
8+
)
9+
10+
type Semaphore struct {
11+
ch chan struct{}
12+
}
13+
14+
func NewSemaphore(count int) *Semaphore {
15+
s := &Semaphore{
16+
ch: make(chan struct{}, count),
17+
}
18+
for i := 0; i < count; i++ {
19+
s.Signal()
20+
}
21+
return s
22+
}
23+
24+
func (s *Semaphore) Wait() {
25+
<-s.ch
26+
}
27+
28+
func (s *Semaphore) WaitC() <-chan struct{} {
29+
return s.ch
30+
}
31+
32+
func (s *Semaphore) Available() int {
33+
return len(s.ch)
34+
}
35+
36+
func (s *Semaphore) Signal() {
37+
if av := s.Available(); av == cap(s.ch) {
38+
// Not super reliable, but let it be here just in case.
39+
panic(fmt.Sprintf("semaphore capacity (%v) is exceeded (%v)", cap(s.ch), av))
40+
}
41+
s.ch <- struct{}{}
42+
}

pkg/updater/updater.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type Updater struct {
4949
type Config struct {
5050
// If set, exit on updates instead of restarting the current binary.
5151
ExitOnUpdate bool
52-
BuildSem *instance.Semaphore
52+
BuildSem *osutil.Semaphore
5353
ReportBuildError func(commit *vcs.Commit, compilerID string, buildErr error)
5454
SyzkallerRepo string
5555
SyzkallerBranch string

syz-ci/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ func createManager(cfg *Config, mgrcfg *ManagerConfig, debug bool) (*Manager, er
170170
// Gates kernel builds, syzkaller builds and coverage report generation.
171171
// Kernel builds take whole machine, so we don't run more than one at a time.
172172
// Also current image build script uses some global resources (/dev/nbd0) and can't run in parallel.
173-
var buildSem = instance.NewSemaphore(1)
173+
var buildSem = osutil.NewSemaphore(1)
174174

175175
// Gates tests that require extra VMs.
176176
// Currently we overcommit instances in such cases, so we'd like to minimize the number of
177177
// simultaneous env.Test calls.
178-
var testSem = instance.NewSemaphore(1)
178+
var testSem = osutil.NewSemaphore(1)
179179

180180
const fuzzingMinutesBeforeCover = 360
181181
const benchUploadPeriod = 30 * time.Minute

0 commit comments

Comments
 (0)