diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2025-11-24 08:04:20 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2025-11-24 08:55:49 +0000 |
| commit | 9174555f6d933d77dace419771065710ef6df014 (patch) | |
| tree | e272115ab3fa416d5105163934b5faa1f0c5fc5c /pkg | |
| parent | a6deb8053825b4c7024c2b04f5d4f5a12ace1272 (diff) | |
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.
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/bisect/bisect.go | 4 | ||||
| -rw-r--r-- | pkg/instance/instance.go | 40 | ||||
| -rw-r--r-- | pkg/osutil/semaphore.go | 42 | ||||
| -rw-r--r-- | pkg/updater/updater.go | 2 |
4 files changed, 48 insertions, 40 deletions
diff --git a/pkg/bisect/bisect.go b/pkg/bisect/bisect.go index 55a5d44f7..a37436495 100644 --- a/pkg/bisect/bisect.go +++ b/pkg/bisect/bisect.go @@ -36,8 +36,8 @@ type Config struct { Syzkaller SyzkallerConfig Repro ReproConfig Manager *mgrconfig.Config - BuildSemaphore *instance.Semaphore - TestSemaphore *instance.Semaphore + BuildSemaphore *osutil.Semaphore + TestSemaphore *osutil.Semaphore BuildCPUs int // CrossTree specifies whether a cross tree bisection is to take place, i.e. // Kernel.Commit is not reachable from Kernel.Branch. diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go index b4460e3ab..df0d74b98 100644 --- a/pkg/instance/instance.go +++ b/pkg/instance/instance.go @@ -40,8 +40,8 @@ type Env interface { type env struct { cfg *mgrconfig.Config optionalFlags bool - buildSem *Semaphore - testSem *Semaphore + buildSem *osutil.Semaphore + testSem *osutil.Semaphore } type BuildKernelConfig struct { @@ -56,7 +56,7 @@ type BuildKernelConfig struct { BuildCPUs int } -func NewEnv(cfg *mgrconfig.Config, buildSem, testSem *Semaphore) (Env, error) { +func NewEnv(cfg *mgrconfig.Config, buildSem, testSem *osutil.Semaphore) (Env, error) { if !vm.AllowsOvercommit(cfg.Type) { return nil, fmt.Errorf("test instances are not supported for %v VMs", cfg.Type) } @@ -508,40 +508,6 @@ func RunnerCmd(prog, fwdAddr, os, arch string, poolIdx, vmIdx int, threaded, new "-threaded=%t -new-env=%t", prog, fwdAddr, os, arch, poolIdx, vmIdx, threaded, newEnv) } -type Semaphore struct { - ch chan struct{} -} - -func NewSemaphore(count int) *Semaphore { - s := &Semaphore{ - ch: make(chan struct{}, count), - } - for i := 0; i < count; i++ { - s.Signal() - } - return s -} - -func (s *Semaphore) Wait() { - <-s.ch -} - -func (s *Semaphore) WaitC() <-chan struct{} { - return s.ch -} - -func (s *Semaphore) Available() int { - return len(s.ch) -} - -func (s *Semaphore) Signal() { - if av := s.Available(); av == cap(s.ch) { - // Not super reliable, but let it be here just in case. - panic(fmt.Sprintf("semaphore capacity (%d) is exceeded (%d)", cap(s.ch), av)) - } - s.ch <- struct{}{} -} - // RunSmokeTest executes syz-manager in the smoke test mode and returns two values: // The crash report, if the testing failed. // An error if there was a problem not related to testing the kernel. diff --git a/pkg/osutil/semaphore.go b/pkg/osutil/semaphore.go new file mode 100644 index 000000000..83d251ce0 --- /dev/null +++ b/pkg/osutil/semaphore.go @@ -0,0 +1,42 @@ +// Copyright 2025 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package osutil + +import ( + "fmt" +) + +type Semaphore struct { + ch chan struct{} +} + +func NewSemaphore(count int) *Semaphore { + s := &Semaphore{ + ch: make(chan struct{}, count), + } + for i := 0; i < count; i++ { + s.Signal() + } + return s +} + +func (s *Semaphore) Wait() { + <-s.ch +} + +func (s *Semaphore) WaitC() <-chan struct{} { + return s.ch +} + +func (s *Semaphore) Available() int { + return len(s.ch) +} + +func (s *Semaphore) Signal() { + if av := s.Available(); av == cap(s.ch) { + // Not super reliable, but let it be here just in case. + panic(fmt.Sprintf("semaphore capacity (%v) is exceeded (%v)", cap(s.ch), av)) + } + s.ch <- struct{}{} +} diff --git a/pkg/updater/updater.go b/pkg/updater/updater.go index dd62a061a..00181f732 100644 --- a/pkg/updater/updater.go +++ b/pkg/updater/updater.go @@ -49,7 +49,7 @@ type Updater struct { type Config struct { // If set, exit on updates instead of restarting the current binary. ExitOnUpdate bool - BuildSem *instance.Semaphore + BuildSem *osutil.Semaphore ReportBuildError func(commit *vcs.Commit, compilerID string, buildErr error) SyzkallerRepo string SyzkallerBranch string |
