2020-06-27 00:31:03 +03:00
|
|
|
module atomic2
|
2020-06-26 21:04:17 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
Implements the atomic operations. For now TCC does not support
|
|
|
|
the atomic versions on nix so it uses locks to simulate the same behavor.
|
|
|
|
On windows tcc can simulate with other atomic operations.
|
|
|
|
|
2021-04-19 19:01:47 +03:00
|
|
|
The @VEXEROOT/thirdparty/stdatomic contains compability header files
|
2020-06-26 21:04:17 +03:00
|
|
|
for stdatomic that supports both nix, windows and c++.
|
|
|
|
|
|
|
|
This implementations should be regarded as alpha stage and be
|
|
|
|
further tested.
|
|
|
|
*/
|
2021-04-19 19:01:47 +03:00
|
|
|
#flag windows -I @VEXEROOT/thirdparty/stdatomic/win
|
|
|
|
#flag linux -I @VEXEROOT/thirdparty/stdatomic/nix
|
|
|
|
#flag darwin -I @VEXEROOT/thirdparty/stdatomic/nix
|
|
|
|
#flag freebsd -I @VEXEROOT/thirdparty/stdatomic/nix
|
|
|
|
#flag solaris -I @VEXEROOT/thirdparty/stdatomic/nix
|
2020-08-06 16:28:19 +03:00
|
|
|
$if linux {
|
|
|
|
$if tinyc {
|
|
|
|
// most Linux distributions have /usr/lib/libatomic.so, but Ubuntu uses gcc version specific dir
|
2021-02-25 13:35:00 +03:00
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/6
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/7
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/8
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/9
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/10
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/11
|
|
|
|
#flag -L/usr/lib/gcc/x86_64-linux-gnu/12
|
|
|
|
#flag -latomic
|
2020-08-06 16:28:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#include <atomic.h>
|
2020-06-26 21:04:17 +03:00
|
|
|
// add_u64 adds provided delta as an atomic operation
|
|
|
|
pub fn add_u64(ptr &u64, delta int) bool {
|
2021-04-19 15:38:48 +03:00
|
|
|
res := C.atomic_fetch_add_u64(voidptr(ptr), delta)
|
2020-06-26 21:04:17 +03:00
|
|
|
return res == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// sub_u64 subtracts provided delta as an atomic operation
|
|
|
|
pub fn sub_u64(ptr &u64, delta int) bool {
|
2021-04-19 15:38:48 +03:00
|
|
|
res := C.atomic_fetch_sub_u64(voidptr(ptr), delta)
|
2020-06-26 21:04:17 +03:00
|
|
|
return res == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// add_i64 adds provided delta as an atomic operation
|
|
|
|
pub fn add_i64(ptr &i64, delta int) bool {
|
2021-04-19 15:38:48 +03:00
|
|
|
res := C.atomic_fetch_add_u64(voidptr(ptr), delta)
|
2020-06-26 21:04:17 +03:00
|
|
|
return res == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// add_i64 subtracts provided delta as an atomic operation
|
|
|
|
pub fn sub_i64(ptr &i64, delta int) bool {
|
2021-04-19 15:38:48 +03:00
|
|
|
res := C.atomic_fetch_sub_u64(voidptr(ptr), delta)
|
2020-06-26 21:04:17 +03:00
|
|
|
return res == 0
|
|
|
|
}
|
2020-09-05 15:44:16 +03:00
|
|
|
|
|
|
|
// atomic store/load operations have to be used when there might be another concurrent access
|
|
|
|
// atomicall set a value
|
|
|
|
pub fn store_u64(ptr &u64, val u64) {
|
2021-04-19 15:38:48 +03:00
|
|
|
C.atomic_store_u64(voidptr(ptr), val)
|
2020-09-05 15:44:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// atomicall get a value
|
|
|
|
pub fn load_u64(ptr &u64) u64 {
|
2021-04-19 15:38:48 +03:00
|
|
|
return C.atomic_load_u64(voidptr(ptr))
|
2020-09-05 15:44:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// atomicall set a value
|
|
|
|
pub fn store_i64(ptr &i64, val i64) {
|
2021-04-19 15:38:48 +03:00
|
|
|
C.atomic_store_u64(voidptr(ptr), val)
|
2020-09-05 15:44:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// atomicall get a value
|
|
|
|
pub fn load_i64(ptr &i64) i64 {
|
2021-04-19 15:38:48 +03:00
|
|
|
return i64(C.atomic_load_u64(voidptr(ptr)))
|
2020-09-05 15:44:16 +03:00
|
|
|
}
|