-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |