From a11e47b48c19fbe05555a2822fd2d698c4f2edb3 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Mon, 27 Mar 2023 12:08:06 -0700 Subject: [PATCH] Use built in atomic.Bool Signed-off-by: Maksym Pavlenko --- pkg/atomic/atomic_boolean.go | 54 ------------------------------- pkg/atomic/atomic_boolean_test.go | 32 ------------------ pkg/cri/sbserver/service.go | 7 ++-- pkg/cri/server/service.go | 7 ++-- 4 files changed, 6 insertions(+), 94 deletions(-) delete mode 100644 pkg/atomic/atomic_boolean.go delete mode 100644 pkg/atomic/atomic_boolean_test.go diff --git a/pkg/atomic/atomic_boolean.go b/pkg/atomic/atomic_boolean.go deleted file mode 100644 index 507e063dc..000000000 --- a/pkg/atomic/atomic_boolean.go +++ /dev/null @@ -1,54 +0,0 @@ -/* - Copyright The containerd Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package atomic - -import "sync/atomic" - -// Bool is an atomic Boolean, -// Its methods are all atomic, thus safe to be called by -// multiple goroutines simultaneously. -type Bool interface { - Set() - Unset() - IsSet() bool -} - -// NewBool creates an Bool with given default value -func NewBool(ok bool) Bool { - ab := new(atomicBool) - if ok { - ab.Set() - } - return ab -} - -type atomicBool int32 - -// Set sets the Boolean to true -func (ab *atomicBool) Set() { - atomic.StoreInt32((*int32)(ab), 1) -} - -// Unset sets the Boolean to false -func (ab *atomicBool) Unset() { - atomic.StoreInt32((*int32)(ab), 0) -} - -// IsSet returns whether the Boolean is true -func (ab *atomicBool) IsSet() bool { - return atomic.LoadInt32((*int32)(ab)) == 1 -} diff --git a/pkg/atomic/atomic_boolean_test.go b/pkg/atomic/atomic_boolean_test.go deleted file mode 100644 index 97e5a4b55..000000000 --- a/pkg/atomic/atomic_boolean_test.go +++ /dev/null @@ -1,32 +0,0 @@ -/* - Copyright The containerd Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package atomic - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestBoolean(t *testing.T) { - ab := NewBool(true) - assert.True(t, ab.IsSet()) - ab.Unset() - assert.False(t, ab.IsSet()) - ab.Set() - assert.True(t, ab.IsSet()) -} diff --git a/pkg/cri/sbserver/service.go b/pkg/cri/sbserver/service.go index 0c32f6f1f..d4d80c9ec 100644 --- a/pkg/cri/sbserver/service.go +++ b/pkg/cri/sbserver/service.go @@ -25,6 +25,7 @@ import ( "os" "path/filepath" "sync" + "sync/atomic" "time" "github.com/containerd/containerd" @@ -43,7 +44,6 @@ import ( "github.com/containerd/containerd/pkg/cri/store/label" - "github.com/containerd/containerd/pkg/atomic" criconfig "github.com/containerd/containerd/pkg/cri/config" containerstore "github.com/containerd/containerd/pkg/cri/store/container" imagestore "github.com/containerd/containerd/pkg/cri/store/image" @@ -138,7 +138,6 @@ func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri. snapshotStore: snapshotstore.NewStore(), sandboxNameIndex: registrar.NewRegistrar(), containerNameIndex: registrar.NewRegistrar(), - initialized: atomic.NewBool(false), netPlugin: make(map[string]cni.CNI), unpackDuplicationSuppressor: kmutex.New(), sandboxControllers: make(map[criconfig.SandboxControllerMode]sandbox.Controller), @@ -288,7 +287,7 @@ func (c *criService) Run() error { } // Set the server as initialized. GRPC services could start serving traffic. - c.initialized.Set() + c.initialized.Store(true) var eventMonitorErr, streamServerErr, cniNetConfMonitorErr error // Stop the whole CRI service if any of the critical service exits. @@ -340,7 +339,7 @@ func (c *criService) Close() error { // IsInitialized indicates whether CRI service has finished initialization. func (c *criService) IsInitialized() bool { - return c.initialized.IsSet() + return c.initialized.Load() } func (c *criService) register(s *grpc.Server) error { diff --git a/pkg/cri/server/service.go b/pkg/cri/server/service.go index 792342d7d..c39bb5054 100644 --- a/pkg/cri/server/service.go +++ b/pkg/cri/server/service.go @@ -24,6 +24,7 @@ import ( "os" "path/filepath" "sync" + "sync/atomic" "time" "github.com/containerd/containerd" @@ -40,7 +41,6 @@ import ( "github.com/containerd/containerd/pkg/cri/store/label" - "github.com/containerd/containerd/pkg/atomic" criconfig "github.com/containerd/containerd/pkg/cri/config" containerstore "github.com/containerd/containerd/pkg/cri/store/container" imagestore "github.com/containerd/containerd/pkg/cri/store/image" @@ -132,7 +132,6 @@ func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri. snapshotStore: snapshotstore.NewStore(), sandboxNameIndex: registrar.NewRegistrar(), containerNameIndex: registrar.NewRegistrar(), - initialized: atomic.NewBool(false), netPlugin: make(map[string]cni.CNI), unpackDuplicationSuppressor: kmutex.New(), } @@ -265,7 +264,7 @@ func (c *criService) Run() error { } // Set the server as initialized. GRPC services could start serving traffic. - c.initialized.Set() + c.initialized.Store(true) var eventMonitorErr, streamServerErr, cniNetConfMonitorErr error // Stop the whole CRI service if any of the critical service exits. @@ -318,7 +317,7 @@ func (c *criService) Close() error { // IsInitialized indicates whether CRI service has finished initialization. func (c *criService) IsInitialized() bool { - return c.initialized.IsSet() + return c.initialized.Load() } func (c *criService) register(s *grpc.Server) error {