Merge pull request #8313 from mxpv/atomic

Use atomic.Bool from stdlib (and remove pkg/atomic)
This commit is contained in:
Phil Estes 2023-03-28 13:41:49 -04:00 committed by GitHub
commit 662ff50b73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 6 additions and 94 deletions

View File

@ -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
}

View File

@ -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())
}

View File

@ -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 {

View File

@ -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 {