Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,23 @@
|
||||
module main
|
||||
|
||||
import x.atomics
|
||||
|
||||
fn main() {
|
||||
// Basic atomic load and store operations
|
||||
mut value := i32(0)
|
||||
|
||||
// Atomically store a value
|
||||
atomics.store_i32(&value, 42)
|
||||
|
||||
// Atomically load the value
|
||||
loaded := atomics.load_i32(&value)
|
||||
println('Loaded value: ${loaded}') // Output: 42
|
||||
|
||||
// Atomic add: returns the new value after addition
|
||||
new_value := atomics.add_i32(&value, 10)
|
||||
println('After add: ${new_value}') // Output: 52
|
||||
|
||||
// Atomic swap: returns the old value
|
||||
old := atomics.swap_i32(&value, 100)
|
||||
println('Old value: ${old}, new value: ${atomics.load_i32(&value)}') // Output: 52, 100
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
module main
|
||||
|
||||
import x.atomics
|
||||
|
||||
fn increment(counter &i64) {
|
||||
atomics.add_i64(counter, 1)
|
||||
}
|
||||
|
||||
fn worker(counter &i64, iterations int) {
|
||||
for _ in 0 .. iterations {
|
||||
increment(counter)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut counter := i64(0)
|
||||
|
||||
num_threads := 4
|
||||
increments_per_thread := 10000
|
||||
|
||||
mut threads := []thread{}
|
||||
|
||||
for _ in 0 .. num_threads {
|
||||
threads << spawn worker(&counter, increments_per_thread)
|
||||
}
|
||||
|
||||
threads.wait()
|
||||
|
||||
expected := i64(num_threads * increments_per_thread)
|
||||
actual := atomics.load_i64(&counter)
|
||||
|
||||
println('Expected: ${expected}')
|
||||
println('Actual: ${actual}')
|
||||
|
||||
if actual == expected {
|
||||
println('Counter is correct')
|
||||
} else {
|
||||
println('Counter mismatch - race condition detected')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
module main
|
||||
|
||||
import x.atomics
|
||||
|
||||
struct SpinLock {
|
||||
mut:
|
||||
state u32 // 0 = unlocked, 1 = locked
|
||||
}
|
||||
|
||||
fn acquire(mut spinlock SpinLock) {
|
||||
for !atomics.cas_u32(&spinlock.state, 0, 1) {
|
||||
// Busy-wait
|
||||
}
|
||||
}
|
||||
|
||||
fn release(mut spinlock SpinLock) {
|
||||
atomics.store_u32(&spinlock.state, 0)
|
||||
}
|
||||
|
||||
struct SharedData {
|
||||
mut:
|
||||
spinlock SpinLock
|
||||
value int
|
||||
}
|
||||
|
||||
fn worker(mut data SharedData, iterations int) {
|
||||
for _ in 0 .. iterations {
|
||||
acquire(mut data.spinlock)
|
||||
data.value++
|
||||
release(mut data.spinlock)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut data := &SharedData{}
|
||||
|
||||
num_threads := 4
|
||||
iterations_per_thread := 10000
|
||||
|
||||
mut threads := []thread{}
|
||||
|
||||
for _ in 0 .. num_threads {
|
||||
threads << spawn worker(mut data, iterations_per_thread)
|
||||
}
|
||||
|
||||
threads.wait()
|
||||
|
||||
expected := num_threads * iterations_per_thread
|
||||
actual := data.value
|
||||
|
||||
println('Expected: ${expected}')
|
||||
println('Actual: ${actual}')
|
||||
|
||||
if actual == expected {
|
||||
println('Spinlock works correctly')
|
||||
} else {
|
||||
println('Race condition detected')
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user