mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
stdatomic: add/sub return result of op (#15235)
This commit is contained in:
parent
e5e750d533
commit
afaab7ab4d
@ -9,27 +9,27 @@ module stdatomic
|
||||
// much more.
|
||||
|
||||
// add_u64 adds provided delta as an atomic operation
|
||||
pub fn add_u64(ptr &u64, delta int) bool {
|
||||
res := C.atomic_fetch_add_u64(voidptr(ptr), delta)
|
||||
return res == 0
|
||||
pub fn add_u64(ptr &u64, delta int) u64 {
|
||||
C.atomic_fetch_add_u64(voidptr(ptr), delta)
|
||||
return *ptr
|
||||
}
|
||||
|
||||
// sub_u64 subtracts provided delta as an atomic operation
|
||||
pub fn sub_u64(ptr &u64, delta int) bool {
|
||||
res := C.atomic_fetch_sub_u64(voidptr(ptr), delta)
|
||||
return res == 0
|
||||
pub fn sub_u64(ptr &u64, delta int) u64 {
|
||||
C.atomic_fetch_sub_u64(voidptr(ptr), delta)
|
||||
return *ptr
|
||||
}
|
||||
|
||||
// add_i64 adds provided delta as an atomic operation
|
||||
pub fn add_i64(ptr &i64, delta int) bool {
|
||||
res := C.atomic_fetch_add_u64(voidptr(ptr), delta)
|
||||
return res == 0
|
||||
pub fn add_i64(ptr &i64, delta int) i64 {
|
||||
C.atomic_fetch_add_u64(voidptr(ptr), delta)
|
||||
return *ptr
|
||||
}
|
||||
|
||||
// add_i64 subtracts provided delta as an atomic operation
|
||||
pub fn sub_i64(ptr &i64, delta int) bool {
|
||||
res := C.atomic_fetch_sub_u64(voidptr(ptr), delta)
|
||||
return res == 0
|
||||
pub fn sub_i64(ptr &i64, delta int) i64 {
|
||||
C.atomic_fetch_sub_u64(voidptr(ptr), delta)
|
||||
return *ptr
|
||||
}
|
||||
|
||||
// atomic store/load operations have to be used when there might be another concurrent access
|
||||
|
@ -38,52 +38,44 @@ fn test_count_10_times_1_cycle_should_not_be_10_cycles_without_sync() {
|
||||
|
||||
fn test_atomic_count_plus_one_u64() {
|
||||
mut c := u64(0)
|
||||
stdatomic.add_u64(&c, 1)
|
||||
assert stdatomic.load_u64(&c) == 1
|
||||
assert stdatomic.add_u64(&c, 1) == 1
|
||||
}
|
||||
|
||||
fn test_atomic_count_plus_one_i64() {
|
||||
mut c := i64(0)
|
||||
stdatomic.add_i64(&c, 1)
|
||||
assert stdatomic.load_i64(&c) == 1
|
||||
assert stdatomic.add_i64(&c, 1) == 1
|
||||
}
|
||||
|
||||
fn test_atomic_count_plus_greater_than_one_u64() {
|
||||
mut c := u64(0)
|
||||
stdatomic.add_u64(&c, 10)
|
||||
assert stdatomic.load_u64(&c) == 10
|
||||
assert stdatomic.add_u64(&c, 10) == 10
|
||||
}
|
||||
|
||||
fn test_atomic_count_plus_greater_than_one_i64() {
|
||||
mut c := i64(0)
|
||||
stdatomic.add_i64(&c, 10)
|
||||
assert stdatomic.load_i64(&c) == 10
|
||||
assert stdatomic.add_i64(&c, 10) == 10
|
||||
}
|
||||
|
||||
fn test_atomic_count_minus_one_u64() {
|
||||
mut c := u64(1)
|
||||
stdatomic.sub_u64(&c, 1)
|
||||
assert stdatomic.load_u64(&c) == 0
|
||||
assert stdatomic.sub_u64(&c, 1) == 0
|
||||
}
|
||||
|
||||
fn test_atomic_count_minus_one_i64() {
|
||||
mut c := i64(0)
|
||||
stdatomic.sub_i64(&c, 1)
|
||||
assert stdatomic.load_i64(&c) == -1
|
||||
assert stdatomic.sub_i64(&c, 1) == -1
|
||||
}
|
||||
|
||||
fn test_atomic_count_minus_greater_than_one_u64() {
|
||||
mut c := u64(0)
|
||||
stdatomic.store_u64(&c, 10)
|
||||
stdatomic.sub_u64(&c, 10)
|
||||
assert stdatomic.load_u64(&c) == 0
|
||||
assert stdatomic.sub_u64(&c, 10) == 0
|
||||
}
|
||||
|
||||
fn test_atomic_count_minus_greater_than_one_i64() {
|
||||
mut c := i64(0)
|
||||
stdatomic.store_i64(&c, 10)
|
||||
stdatomic.sub_i64(&c, 20)
|
||||
assert stdatomic.load_i64(&c) == -10
|
||||
assert stdatomic.sub_i64(&c, 20) == -10
|
||||
}
|
||||
|
||||
// count_one_cycle counts the common counter iterations_per_cycle times in thread-safe way
|
||||
|
Loading…
Reference in New Issue
Block a user