Skip to content

Commit

Permalink
Use uint32 for cleanerRunning Instead of atomic.Bool to Maintain Comp…
Browse files Browse the repository at this point in the history
…atibility with Go 1.13

Signed-off-by: Shivam Kumar <kumar.shivam.jarvis@gmail.com>
  • Loading branch information
ShivamKumar2002 committed Nov 29, 2023
1 parent 1d8be58 commit 422970f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
13 changes: 7 additions & 6 deletions timedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type TimedMap struct {
cleanupTickTime time.Duration
cleanerTicker *time.Ticker
cleanerStopChan chan bool
cleanerRunning atomic.Bool
cleanerRunning *uint32
}

type keyWrap struct {
Expand Down Expand Up @@ -193,7 +193,7 @@ func (tm *TimedMap) Size() int {
// If the cleanup loop is already running, it will be
// stopped and restarted using the new specification.
func (tm *TimedMap) StartCleanerInternal(interval time.Duration) {
if tm.cleanerRunning.Load() {
if atomic.LoadUint32(tm.cleanerRunning) != 0 {
tm.StopCleaner()
}
tm.cleanerTicker = time.NewTicker(interval)
Expand All @@ -208,7 +208,7 @@ func (tm *TimedMap) StartCleanerInternal(interval time.Duration) {
// If the cleanup loop is already running, it will be
// stopped and restarted using the new specification.
func (tm *TimedMap) StartCleanerExternal(initiator <-chan time.Time) {
if tm.cleanerRunning.Load() {
if atomic.LoadUint32(tm.cleanerRunning) != 0 {
tm.StopCleaner()
}
go tm.cleanupLoop(initiator)
Expand All @@ -219,7 +219,7 @@ func (tm *TimedMap) StartCleanerExternal(initiator <-chan time.Time) {
// where TimedMap is used that the data can be cleaned
// up correctly.
func (tm *TimedMap) StopCleaner() {
if !tm.cleanerRunning.Load() {
if atomic.LoadUint32(tm.cleanerRunning) == 0 {
return
}
tm.cleanerStopChan <- true
Expand All @@ -237,9 +237,9 @@ func (tm *TimedMap) Snapshot() map[interface{}]interface{} {
// cleanupLoop holds the loop executing the cleanup
// when initiated by tc.
func (tm *TimedMap) cleanupLoop(tc <-chan time.Time) {
tm.cleanerRunning.Store(true)
atomic.StoreUint32(tm.cleanerRunning, 1)
defer func() {
tm.cleanerRunning.Store(false)
atomic.StoreUint32(tm.cleanerRunning, 0)
}()

for {
Expand Down Expand Up @@ -418,6 +418,7 @@ func newTimedMap(
) *TimedMap {
tm := &TimedMap{
container: container,
cleanerRunning: new(uint32),
cleanerStopChan: make(chan bool),
elementPool: &sync.Pool{
New: func() interface{} {
Expand Down
15 changes: 8 additions & 7 deletions timedmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package timedmap

import (
"sync"
"sync/atomic"
"testing"
"time"

Expand All @@ -19,7 +20,7 @@ func TestNew(t *testing.T) {
assert.NotNil(t, tm)
assert.EqualValues(t, 0, len(tm.container))
time.Sleep(10 * time.Millisecond)
assert.True(t, tm.cleanerRunning.Load())
assert.True(t, atomic.LoadUint32(tm.cleanerRunning) != 0)
}

func TestFromMap(t *testing.T) {
Expand Down Expand Up @@ -246,7 +247,7 @@ func TestStopCleaner(t *testing.T) {
time.Sleep(10 * time.Millisecond)
tm.StopCleaner()
time.Sleep(10 * time.Millisecond)
assert.False(t, tm.cleanerRunning.Load())
assert.False(t, atomic.LoadUint32(tm.cleanerRunning) != 0)

assert.NotPanics(t, func() {
tm.StopCleaner()
Expand All @@ -259,7 +260,7 @@ func TestStartCleanerInternal(t *testing.T) {
tm := New(0)
time.Sleep(10 * time.Millisecond)

assert.False(t, tm.cleanerRunning.Load())
assert.False(t, atomic.LoadUint32(tm.cleanerRunning) != 0)

// Ensure cleanup timer is not running
tm.set(1, 0, 1, 0)
Expand All @@ -268,7 +269,7 @@ func TestStartCleanerInternal(t *testing.T) {

tm.StartCleanerInternal(dCleanupTick)
time.Sleep(10 * time.Millisecond)
assert.True(t, tm.cleanerRunning.Load())
assert.True(t, atomic.LoadUint32(tm.cleanerRunning) != 0)

// Ensure cleanup timer is running
tm.set(1, 0, 1, 0)
Expand All @@ -294,7 +295,7 @@ func TestStartCleanerExternal(t *testing.T) {
tm := New(0)
time.Sleep(10 * time.Millisecond)

assert.False(t, tm.cleanerRunning.Load())
assert.False(t, atomic.LoadUint32(tm.cleanerRunning) != 0)

// Ensure cleanup timer is not running
tm.set(1, 0, 1, 0)
Expand All @@ -305,7 +306,7 @@ func TestStartCleanerExternal(t *testing.T) {

tm.StartCleanerExternal(c)
time.Sleep(10 * time.Millisecond)
assert.True(t, tm.cleanerRunning.Load())
assert.True(t, atomic.LoadUint32(tm.cleanerRunning) != 0)

// Ensure cleanup is controlled by c
tm.set(1, 0, 1, 0)
Expand All @@ -323,7 +324,7 @@ func TestStartCleanerExternal(t *testing.T) {
tm := New(dCleanupTick)
time.Sleep(10 * time.Millisecond)

assert.True(t, tm.cleanerRunning.Load())
assert.True(t, atomic.LoadUint32(tm.cleanerRunning) != 0)
assert.NotNil(t, tm.cleanerTicker)

c := make(chan time.Time)
Expand Down

0 comments on commit 422970f

Please sign in to comment.