2020-06-26 21:04:17 +03:00
|
|
|
import sync
|
2021-12-28 11:12:40 +03:00
|
|
|
import sync.stdatomic
|
2020-06-26 21:04:17 +03:00
|
|
|
|
2021-12-28 11:12:40 +03:00
|
|
|
const iterations_per_cycle = 100_000
|
2020-07-20 11:14:55 +03:00
|
|
|
|
2020-06-26 21:04:17 +03:00
|
|
|
struct Counter {
|
|
|
|
mut:
|
2020-09-09 15:14:44 +03:00
|
|
|
counter u64
|
2020-06-26 21:04:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// without proper syncronization this would fail
|
2020-07-20 11:14:55 +03:00
|
|
|
fn test_count_10_times_1_cycle_should_result_10_cycles_with_sync() {
|
|
|
|
desired_iterations := 10 * iterations_per_cycle
|
2020-06-26 21:04:17 +03:00
|
|
|
mut wg := sync.new_waitgroup()
|
|
|
|
mut counter := &Counter{}
|
|
|
|
wg.add(10)
|
|
|
|
for i := 0; i < 10; i++ {
|
2020-07-20 11:14:55 +03:00
|
|
|
go count_one_cycle(mut counter, mut wg)
|
2020-06-26 21:04:17 +03:00
|
|
|
}
|
|
|
|
wg.wait()
|
2022-04-25 12:09:25 +03:00
|
|
|
assert counter.counter == u64(desired_iterations)
|
2020-07-20 11:14:55 +03:00
|
|
|
eprintln(' with synchronization the counter is: ${counter.counter:10} , expectedly == ${desired_iterations:10}')
|
2020-06-26 21:04:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This test just to make sure that we have an anti-test to prove it works
|
2020-07-20 11:14:55 +03:00
|
|
|
fn test_count_10_times_1_cycle_should_not_be_10_cycles_without_sync() {
|
|
|
|
desired_iterations := 10 * iterations_per_cycle
|
|
|
|
mut wg := sync.new_waitgroup()
|
|
|
|
mut counter := &Counter{}
|
|
|
|
wg.add(10)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
go count_one_cycle_without_sync(mut counter, mut wg)
|
|
|
|
}
|
|
|
|
wg.wait()
|
2022-03-06 20:01:22 +03:00
|
|
|
// Note: we do not assert here, just print, because sometimes by chance counter.counter may be == desired_iterations
|
2020-07-20 11:14:55 +03:00
|
|
|
eprintln('without synchronization the counter is: ${counter.counter:10} , expectedly != ${desired_iterations:10}')
|
|
|
|
}
|
2020-06-26 21:04:17 +03:00
|
|
|
|
2021-12-28 11:12:40 +03:00
|
|
|
fn test_atomic_count_plus_one_u64() {
|
2020-06-26 21:04:17 +03:00
|
|
|
mut c := u64(0)
|
2022-07-27 03:21:03 +03:00
|
|
|
assert stdatomic.add_u64(&c, 1) == 1
|
2020-06-26 21:04:17 +03:00
|
|
|
}
|
|
|
|
|
2021-12-28 11:12:40 +03:00
|
|
|
fn test_atomic_count_plus_one_i64() {
|
2020-06-26 21:04:17 +03:00
|
|
|
mut c := i64(0)
|
2022-07-27 03:21:03 +03:00
|
|
|
assert stdatomic.add_i64(&c, 1) == 1
|
2020-06-26 21:04:17 +03:00
|
|
|
}
|
|
|
|
|
2021-12-28 11:12:40 +03:00
|
|
|
fn test_atomic_count_plus_greater_than_one_u64() {
|
2020-06-26 21:04:17 +03:00
|
|
|
mut c := u64(0)
|
2022-07-27 03:21:03 +03:00
|
|
|
assert stdatomic.add_u64(&c, 10) == 10
|
2020-06-26 21:04:17 +03:00
|
|
|
}
|
|
|
|
|
2021-12-28 11:12:40 +03:00
|
|
|
fn test_atomic_count_plus_greater_than_one_i64() {
|
2020-06-26 21:04:17 +03:00
|
|
|
mut c := i64(0)
|
2022-07-27 03:21:03 +03:00
|
|
|
assert stdatomic.add_i64(&c, 10) == 10
|
2020-06-26 21:04:17 +03:00
|
|
|
}
|
|
|
|
|
2021-12-28 11:12:40 +03:00
|
|
|
fn test_atomic_count_minus_one_u64() {
|
2020-06-26 21:04:17 +03:00
|
|
|
mut c := u64(1)
|
2022-07-27 03:21:03 +03:00
|
|
|
assert stdatomic.sub_u64(&c, 1) == 0
|
2020-06-26 21:04:17 +03:00
|
|
|
}
|
|
|
|
|
2021-12-28 11:12:40 +03:00
|
|
|
fn test_atomic_count_minus_one_i64() {
|
2020-06-26 21:04:17 +03:00
|
|
|
mut c := i64(0)
|
2022-07-27 03:21:03 +03:00
|
|
|
assert stdatomic.sub_i64(&c, 1) == -1
|
2020-06-26 21:04:17 +03:00
|
|
|
}
|
|
|
|
|
2021-12-28 11:12:40 +03:00
|
|
|
fn test_atomic_count_minus_greater_than_one_u64() {
|
2020-09-05 15:44:16 +03:00
|
|
|
mut c := u64(0)
|
2021-12-28 11:12:40 +03:00
|
|
|
stdatomic.store_u64(&c, 10)
|
2022-07-27 03:21:03 +03:00
|
|
|
assert stdatomic.sub_u64(&c, 10) == 0
|
2020-06-26 21:04:17 +03:00
|
|
|
}
|
|
|
|
|
2021-12-28 11:12:40 +03:00
|
|
|
fn test_atomic_count_minus_greater_than_one_i64() {
|
2020-09-05 15:44:16 +03:00
|
|
|
mut c := i64(0)
|
2021-12-28 11:12:40 +03:00
|
|
|
stdatomic.store_i64(&c, 10)
|
2022-07-27 03:21:03 +03:00
|
|
|
assert stdatomic.sub_i64(&c, 20) == -10
|
2020-06-26 21:04:17 +03:00
|
|
|
}
|
|
|
|
|
2020-07-20 11:14:55 +03:00
|
|
|
// count_one_cycle counts the common counter iterations_per_cycle times in thread-safe way
|
|
|
|
fn count_one_cycle(mut counter Counter, mut group sync.WaitGroup) {
|
|
|
|
for i := 0; i < iterations_per_cycle; i++ {
|
2021-12-28 11:12:40 +03:00
|
|
|
stdatomic.add_u64(&counter.counter, 1)
|
2020-06-26 21:04:17 +03:00
|
|
|
}
|
|
|
|
group.done()
|
|
|
|
}
|
|
|
|
|
2020-07-20 11:14:55 +03:00
|
|
|
// count_one_cycle_without_sync counts the common counter iterations_per_cycle times in none thread-safe way
|
|
|
|
fn count_one_cycle_without_sync(mut counter Counter, mut group sync.WaitGroup) {
|
|
|
|
for i := 0; i < iterations_per_cycle; i++ {
|
|
|
|
counter.counter++
|
|
|
|
}
|
|
|
|
group.done()
|
|
|
|
}
|