-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updated definition of atomic operations.
- Loading branch information
Showing
10 changed files
with
522 additions
and
357 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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> | ||
* (C) 2020 Vladimir Sadovnikov <[email protected]> | ||
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/> | ||
* (C) 2024 Vladimir Sadovnikov <[email protected]> | ||
* | ||
* This file is part of lsp-common-lib | ||
* Created on: 31 мар. 2020 г. | ||
|
@@ -26,9 +26,65 @@ | |
#error "This file should not be included directly" | ||
#endif /* LSP_PLUG_IN_COMMON_ATOMIC_IMPL */ | ||
|
||
#define ATOMIC_LOAD_DEF(type, qsz, mod) \ | ||
inline type atomic_load(type *ptr) \ | ||
{ \ | ||
type tmp; \ | ||
\ | ||
ARCH_AARCH64_ASM \ | ||
( \ | ||
__ASM_EMIT("ldar" qsz " %" mod "[tmp], [%[ptr]]") \ | ||
: [tmp] "=&r" (tmp) \ | ||
: [ptr] "r" (ptr) \ | ||
: "memory" \ | ||
); \ | ||
return tmp; \ | ||
} | ||
|
||
#define ATOMIC_CAS_DEF(type, qsz, mod, extra) \ | ||
inline type atomic_cas(extra type *ptr, type exp, type rep) \ | ||
namespace lsp | ||
{ | ||
ATOMIC_LOAD_DEF(int8_t, "sb", "w") | ||
ATOMIC_LOAD_DEF(uint8_t, "b", "w") | ||
ATOMIC_LOAD_DEF(int16_t, "sh", "w") | ||
ATOMIC_LOAD_DEF(uint16_t, "h", "w") | ||
ATOMIC_LOAD_DEF(int32_t, "", "w") | ||
ATOMIC_LOAD_DEF(uint32_t, "", "w") | ||
ATOMIC_LOAD_DEF(int64_t, "", "x") | ||
ATOMIC_LOAD_DEF(uint64_t, "", "x") | ||
ATOMIC_LOAD_DEF(void *, "", "x") | ||
} /* namespace lsp */ | ||
|
||
#undef ATOMIC_LOAD_DEF | ||
|
||
#define ATOMIC_STORE_DEF(type, qsz, mod) \ | ||
inline void atomic_store(type *ptr, type value) \ | ||
{ \ | ||
ARCH_AARCH64_ASM \ | ||
( \ | ||
__ASM_EMIT("stlr" qsz " %" mod "[value], [%[ptr]]") \ | ||
: \ | ||
: [ptr] "r" (ptr), [value] "r" (value) \ | ||
: "memory" \ | ||
); \ | ||
} | ||
|
||
namespace lsp | ||
{ | ||
ATOMIC_STORE_DEF(int8_t, "sb", "w") | ||
ATOMIC_STORE_DEF(uint8_t, "b", "w") | ||
ATOMIC_STORE_DEF(int16_t, "sh", "w") | ||
ATOMIC_STORE_DEF(uint16_t, "h", "w") | ||
ATOMIC_STORE_DEF(int32_t, "", "w") | ||
ATOMIC_STORE_DEF(uint32_t, "", "w") | ||
ATOMIC_STORE_DEF(int64_t, "", "x") | ||
ATOMIC_STORE_DEF(uint64_t, "", "x") | ||
ATOMIC_STORE_DEF(void *, "", "x") | ||
} /* namespace lsp */ | ||
|
||
#undef ATOMIC_LOAD_DEF | ||
|
||
#define ATOMIC_CAS_DEF(type, qsz, mod) \ | ||
inline type atomic_cas(type *ptr, type exp, type rep) \ | ||
{ \ | ||
type tmp; \ | ||
\ | ||
|
@@ -53,30 +109,21 @@ | |
|
||
namespace lsp | ||
{ | ||
ATOMIC_CAS_DEF(int8_t, "b", "w", ) | ||
ATOMIC_CAS_DEF(int8_t, "b", "w", volatile) | ||
ATOMIC_CAS_DEF(uint8_t, "b", "w", ) | ||
ATOMIC_CAS_DEF(uint8_t, "b", "w", volatile) | ||
ATOMIC_CAS_DEF(int16_t, "h", "w", ) | ||
ATOMIC_CAS_DEF(int16_t, "h", "w", volatile) | ||
ATOMIC_CAS_DEF(uint16_t, "h", "w", ) | ||
ATOMIC_CAS_DEF(uint16_t, "h", "w", volatile) | ||
ATOMIC_CAS_DEF(int32_t, "", "w", ) | ||
ATOMIC_CAS_DEF(int32_t, "", "w", volatile) | ||
ATOMIC_CAS_DEF(uint32_t, "", "w", ) | ||
ATOMIC_CAS_DEF(uint32_t, "", "w", volatile) | ||
ATOMIC_CAS_DEF(void *, "", "x", ) | ||
ATOMIC_CAS_DEF(void *, "", "x", volatile) | ||
ATOMIC_CAS_DEF(int64_t, "", "x", ) | ||
ATOMIC_CAS_DEF(int64_t, "", "x", volatile) | ||
ATOMIC_CAS_DEF(uint64_t, "", "x", ) | ||
ATOMIC_CAS_DEF(uint64_t, "", "x", volatile) | ||
} | ||
ATOMIC_CAS_DEF(int8_t, "b", "w") | ||
ATOMIC_CAS_DEF(uint8_t, "b", "w") | ||
ATOMIC_CAS_DEF(int16_t, "h", "w") | ||
ATOMIC_CAS_DEF(uint16_t, "h", "w") | ||
ATOMIC_CAS_DEF(int32_t, "", "w") | ||
ATOMIC_CAS_DEF(uint32_t, "", "w") | ||
ATOMIC_CAS_DEF(void *, "", "x") | ||
ATOMIC_CAS_DEF(int64_t, "", "x") | ||
ATOMIC_CAS_DEF(uint64_t, "", "x") | ||
} /* namespace lsp */ | ||
|
||
#undef ATOMIC_CAS_DEF | ||
|
||
#define ATOMIC_ADD_DEF(type, qsz, mod, extra) \ | ||
inline type atomic_add(extra type *ptr, type value) \ | ||
#define ATOMIC_ADD_DEF(type, qsz, mod) \ | ||
inline type atomic_add(type *ptr, type value) \ | ||
{ \ | ||
uint32_t tmp; \ | ||
type sum, retval; \ | ||
|
@@ -101,28 +148,20 @@ namespace lsp | |
|
||
namespace lsp | ||
{ | ||
ATOMIC_ADD_DEF(int8_t, "b", "w", ) | ||
ATOMIC_ADD_DEF(int8_t, "b", "w", volatile) | ||
ATOMIC_ADD_DEF(uint8_t, "b", "w", ) | ||
ATOMIC_ADD_DEF(uint8_t, "b", "w", volatile) | ||
ATOMIC_ADD_DEF(int16_t, "h", "w", ) | ||
ATOMIC_ADD_DEF(int16_t, "h", "w", volatile) | ||
ATOMIC_ADD_DEF(uint16_t, "h", "w", ) | ||
ATOMIC_ADD_DEF(uint16_t, "h", "w", volatile) | ||
ATOMIC_ADD_DEF(int32_t, "", "w", ) | ||
ATOMIC_ADD_DEF(int32_t, "", "w", volatile) | ||
ATOMIC_ADD_DEF(uint32_t, "", "w", ) | ||
ATOMIC_ADD_DEF(uint32_t, "", "w", volatile) | ||
ATOMIC_ADD_DEF(int64_t, "", "x", ) | ||
ATOMIC_ADD_DEF(int64_t, "", "x", volatile) | ||
ATOMIC_ADD_DEF(uint64_t, "", "x", ) | ||
ATOMIC_ADD_DEF(uint64_t, "", "x", volatile) | ||
} | ||
ATOMIC_ADD_DEF(int8_t, "b", "w") | ||
ATOMIC_ADD_DEF(uint8_t, "b", "w") | ||
ATOMIC_ADD_DEF(int16_t, "h", "w") | ||
ATOMIC_ADD_DEF(uint16_t, "h", "w") | ||
ATOMIC_ADD_DEF(int32_t, "", "w") | ||
ATOMIC_ADD_DEF(uint32_t, "", "w") | ||
ATOMIC_ADD_DEF(int64_t, "", "x") | ||
ATOMIC_ADD_DEF(uint64_t, "", "x") | ||
} /* namespace lsp */ | ||
|
||
#undef ATOMIC_ADD_DEF | ||
|
||
#define ATOMIC_SWAP_DEF(type, qsz, mod, extra) \ | ||
inline type atomic_swap(extra type *ptr, type value) \ | ||
#define ATOMIC_SWAP_DEF(type, qsz, mod) \ | ||
inline type atomic_swap(type *ptr, type value) \ | ||
{ \ | ||
uint32_t tmp; \ | ||
type retval; \ | ||
|
@@ -145,39 +184,35 @@ namespace lsp | |
|
||
namespace lsp | ||
{ | ||
ATOMIC_SWAP_DEF(int8_t, "b", "w", ) | ||
ATOMIC_SWAP_DEF(int8_t, "b", "w", volatile) | ||
ATOMIC_SWAP_DEF(uint8_t, "b", "w", ) | ||
ATOMIC_SWAP_DEF(uint8_t, "b", "w", volatile) | ||
ATOMIC_SWAP_DEF(int16_t, "h", "w", ) | ||
ATOMIC_SWAP_DEF(int16_t, "h", "w", volatile) | ||
ATOMIC_SWAP_DEF(uint16_t, "h", "w", ) | ||
ATOMIC_SWAP_DEF(uint16_t, "h", "w", volatile) | ||
ATOMIC_SWAP_DEF(int32_t, "", "w", ) | ||
ATOMIC_SWAP_DEF(int32_t, "", "w", volatile) | ||
ATOMIC_SWAP_DEF(uint32_t, "", "w", ) | ||
ATOMIC_SWAP_DEF(uint32_t, "", "w", volatile) | ||
ATOMIC_SWAP_DEF(int64_t, "", "x", ) | ||
ATOMIC_SWAP_DEF(int64_t, "", "x", volatile) | ||
ATOMIC_SWAP_DEF(uint64_t, "", "x", ) | ||
ATOMIC_SWAP_DEF(uint64_t, "", "x", volatile) | ||
} | ||
ATOMIC_SWAP_DEF(int8_t, "b", "w") | ||
ATOMIC_SWAP_DEF(uint8_t, "b", "w") | ||
ATOMIC_SWAP_DEF(int16_t, "h", "w") | ||
ATOMIC_SWAP_DEF(uint16_t, "h", "w") | ||
ATOMIC_SWAP_DEF(int32_t, "", "w") | ||
ATOMIC_SWAP_DEF(uint32_t, "", "w") | ||
ATOMIC_SWAP_DEF(int64_t, "", "x") | ||
ATOMIC_SWAP_DEF(uint64_t, "", "x") | ||
} /* namespace lsp */ | ||
|
||
#undef ATOMIC_SWAP_DEF | ||
|
||
//----------------------------------------------------------------------------- | ||
// Atomic operations | ||
|
||
#define LSP_ATOMIC_UNLOCKED 1 | ||
#define LSP_ATOMIC_LOCKED 0 | ||
|
||
namespace lsp | ||
{ | ||
template <class type_t> | ||
inline void atomic_init(type_t &lk) { lk = 1; } | ||
inline void atomic_init(type_t &lk) { lk = LSP_ATOMIC_UNLOCKED; } | ||
|
||
template <class type_t> | ||
inline type_t atomic_trylock(type_t &lk) { return atomic_cas(&lk, 1, 0); } | ||
inline type_t atomic_trylock(type_t &lk) { return atomic_cas(&lk, LSP_ATOMIC_UNLOCKED, LSP_ATOMIC_LOCKED); } | ||
|
||
template <class type_t> | ||
inline type_t atomic_unlock(type_t &lk) { return atomic_swap(&lk, 1); } | ||
} | ||
inline type_t atomic_unlock(type_t &lk) { return atomic_swap(&lk, LSP_ATOMIC_UNLOCKED); } | ||
} /* namespace lsp */ | ||
|
||
|
||
#endif /* LSP_PLUG_IN_COMMON_ARCH_AARCH64_ATOMIC_H_ */ |
Oops, something went wrong.