Skip to content

Commit

Permalink
Mfw 4563 (#389)
Browse files Browse the repository at this point in the history
version: bug
  • Loading branch information
abriles1216 authored Apr 16, 2024
1 parent 0825034 commit 876a74b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
36 changes: 36 additions & 0 deletions util/atomicbool/atomic_bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package atomicbool

import (
"sync/atomic"
)

// AtomicBool is a boolean type that supports atomic operations.
// golang 1.19 has an atomic bool in the stdlib. This should
// swapped out for the stdlib type when openWRT moves to using
// 1.19
type AtomicBool struct {
flag int32
}

// NewAtomicBool initializes a new AtomicBool with the specified initial value.
func NewAtomicBool(initialValue bool) *AtomicBool {
var flag int32
if initialValue {
flag = 1
}
return &AtomicBool{flag: flag}
}

// Set atomically sets the value of the boolean flag.
func (b *AtomicBool) Set(value bool) {
var newValue int32
if value {
newValue = 1
}
atomic.StoreInt32(&b.flag, newValue)
}

// Get atomically retrieves the current value of the boolean flag.
func (b *AtomicBool) Get() bool {
return atomic.LoadInt32(&b.flag) != 0
}
51 changes: 51 additions & 0 deletions util/atomicbool/atomic_bool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package atomicbool

import (
"sync"
"testing"
)

func TestAtomicBoolConcurrent(t *testing.T) {
ab := NewAtomicBool(false)

numGoroutines := 10000

var wg sync.WaitGroup
wg.Add(numGoroutines)

for i := 0; i < numGoroutines; i++ {
go func() {
for j := 0; j < 1000; j++ {
ab.Set(!ab.Get())
}
wg.Done()
}()
}

wg.Wait()

finalValue := ab.Get()
if finalValue != true && finalValue != false {
t.Errorf("Final value should be true or false, got %v", finalValue)
}
}

func TestAtomicBool(t *testing.T) {
ab := NewAtomicBool(false)

if ab.Get() != false {
t.Errorf("Initial value should be false, got %v", ab.Get())
}

ab.Set(true)

if ab.Get() != true {
t.Errorf("Value should be true after setting, got %v", ab.Get())
}

ab.Set(false)

if ab.Get() != false {
t.Errorf("Value should be false after setting, got %v", ab.Get())
}
}
22 changes: 22 additions & 0 deletions util/environments/hybrid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package environments

import (
"fmt"
"os"
)

const (
HybridConfigPath = "/mnt/flash/mfw-settings/hybrid"
)

// Checks if running in the hybrid environment(process is running in EOS with other daemons in an openWRT BST).
func IsHybrid() (bool, error) {
_, err := os.Stat(HybridConfigPath)
if err == nil {
return true, nil
} else if os.IsNotExist(err) {
return false, nil
}

return false, fmt.Errorf("could not determine if file denoting a hybrid exists: %w", err)
}

0 comments on commit 876a74b

Please sign in to comment.