From a37bc49efcfc70589f3e9d0331b73545e9489843 Mon Sep 17 00:00:00 2001 From: Kevin Heavey Date: Thu, 4 Jul 2024 02:32:19 +0400 Subject: [PATCH] Extract atomic-u64 crate (#1824) * create solana-atomic-u64 crate * make reexport public in atomic-u64 * add "For internal use only" * make required items pub in atomic-u64 * update atomic-u64 usage in solana-program --- Cargo.lock | 8 ++++++++ Cargo.toml | 2 ++ programs/sbf/Cargo.lock | 8 ++++++++ sdk/atomic-u64/Cargo.toml | 16 ++++++++++++++++ .../src/atomic_u64.rs => atomic-u64/src/lib.rs} | 14 +++++++------- sdk/program/Cargo.toml | 1 + sdk/program/src/blake3.rs | 2 +- sdk/program/src/hash.rs | 2 +- sdk/program/src/keccak.rs | 2 +- sdk/program/src/lib.rs | 1 - sdk/program/src/pubkey.rs | 2 +- 11 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 sdk/atomic-u64/Cargo.toml rename sdk/{program/src/atomic_u64.rs => atomic-u64/src/lib.rs} (60%) diff --git a/Cargo.lock b/Cargo.lock index d1c8a926be46f0..e30f99ef39ec80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5566,6 +5566,13 @@ dependencies = [ "solana-sdk", ] +[[package]] +name = "solana-atomic-u64" +version = "2.1.0" +dependencies = [ + "parking_lot 0.12.3", +] + [[package]] name = "solana-banking-bench" version = "2.1.0" @@ -6842,6 +6849,7 @@ dependencies = [ "serial_test", "sha2 0.10.8", "sha3 0.10.8", + "solana-atomic-u64", "solana-define-syscall", "solana-frozen-abi", "solana-frozen-abi-macro", diff --git a/Cargo.toml b/Cargo.toml index 9e5d960051f9a6..ce34df96634f83 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -96,6 +96,7 @@ members = [ "runtime-transaction", "sanitize", "sdk", + "sdk/atomic-u64", "sdk/cargo-build-bpf", "sdk/cargo-build-sbf", "sdk/cargo-test-bpf", @@ -326,6 +327,7 @@ soketto = "0.7" solana-account-decoder = { path = "account-decoder", version = "=2.1.0" } solana-accounts-db = { path = "accounts-db", version = "=2.1.0" } solana-address-lookup-table-program = { path = "programs/address-lookup-table", version = "=2.1.0" } +solana-atomic-u64 = { path = "sdk/atomic-u64", version = "=2.1.0" } solana-banks-client = { path = "banks-client", version = "=2.1.0" } solana-banks-interface = { path = "banks-interface", version = "=2.1.0" } solana-banks-server = { path = "banks-server", version = "=2.1.0" } diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index fa9299579ce585..1f85af025424df 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -4600,6 +4600,13 @@ dependencies = [ "thiserror", ] +[[package]] +name = "solana-atomic-u64" +version = "2.1.0" +dependencies = [ + "parking_lot 0.12.2", +] + [[package]] name = "solana-banks-client" version = "2.1.0" @@ -5319,6 +5326,7 @@ dependencies = [ "serde_derive", "sha2 0.10.8", "sha3 0.10.8", + "solana-atomic-u64", "solana-define-syscall", "solana-sanitize", "solana-sdk-macro", diff --git a/sdk/atomic-u64/Cargo.toml b/sdk/atomic-u64/Cargo.toml new file mode 100644 index 00000000000000..6b6d9ec51eed73 --- /dev/null +++ b/sdk/atomic-u64/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "solana-atomic-u64" +description = "Solana atomic u64 implementation. For internal use only." +documentation = "https://docs.rs/solana-atomic-u64" +version = { workspace = true } +authors = { workspace = true } +repository = { workspace = true } +homepage = { workspace = true } +license = { workspace = true } +edition = { workspace = true } + +[target.'cfg(not(target_pointer_width = "64"))'.dependencies] +parking_lot = { workspace = true } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] diff --git a/sdk/program/src/atomic_u64.rs b/sdk/atomic-u64/src/lib.rs similarity index 60% rename from sdk/program/src/atomic_u64.rs rename to sdk/atomic-u64/src/lib.rs index ea8695d0b51263..fae0dfa33d1ee7 100644 --- a/sdk/program/src/atomic_u64.rs +++ b/sdk/atomic-u64/src/lib.rs @@ -1,17 +1,17 @@ -pub(crate) use implementation::AtomicU64; +pub use implementation::AtomicU64; #[cfg(target_pointer_width = "64")] mod implementation { use std::sync::atomic; - pub(crate) struct AtomicU64(atomic::AtomicU64); + pub struct AtomicU64(atomic::AtomicU64); impl AtomicU64 { - pub(crate) const fn new(initial: u64) -> Self { + pub const fn new(initial: u64) -> Self { Self(atomic::AtomicU64::new(initial)) } - pub(crate) fn fetch_add(&self, v: u64) -> u64 { + pub fn fetch_add(&self, v: u64) -> u64 { self.0.fetch_add(v, atomic::Ordering::Relaxed) } } @@ -21,14 +21,14 @@ mod implementation { mod implementation { use parking_lot::{const_mutex, Mutex}; - pub(crate) struct AtomicU64(Mutex); + pub struct AtomicU64(Mutex); impl AtomicU64 { - pub(crate) const fn new(initial: u64) -> Self { + pub const fn new(initial: u64) -> Self { Self(const_mutex(initial)) } - pub(crate) fn fetch_add(&self, v: u64) -> u64 { + pub fn fetch_add(&self, v: u64) -> u64 { let mut lock = self.0.lock(); let i = *lock; *lock = i + v; diff --git a/sdk/program/Cargo.toml b/sdk/program/Cargo.toml index 69a5c59790eaf5..430e7d7badfc05 100644 --- a/sdk/program/Cargo.toml +++ b/sdk/program/Cargo.toml @@ -31,6 +31,7 @@ serde_bytes = { workspace = true } serde_derive = { workspace = true } sha2 = { workspace = true } sha3 = { workspace = true } +solana-atomic-u64 = { workspace = true } solana-frozen-abi = { workspace = true, optional = true } solana-frozen-abi-macro = { workspace = true, optional = true } solana-sanitize = { workspace = true } diff --git a/sdk/program/src/blake3.rs b/sdk/program/src/blake3.rs index 90e19b19e74679..7bbb90768da050 100644 --- a/sdk/program/src/blake3.rs +++ b/sdk/program/src/blake3.rs @@ -102,7 +102,7 @@ impl Hash { /// unique Hash for tests and benchmarks. pub fn new_unique() -> Self { - use crate::atomic_u64::AtomicU64; + use solana_atomic_u64::AtomicU64; static I: AtomicU64 = AtomicU64::new(1); let mut b = [0u8; HASH_BYTES]; diff --git a/sdk/program/src/hash.rs b/sdk/program/src/hash.rs index db2d9ff840dab1..27967c850376bb 100644 --- a/sdk/program/src/hash.rs +++ b/sdk/program/src/hash.rs @@ -136,7 +136,7 @@ impl Hash { /// unique Hash for tests and benchmarks. pub fn new_unique() -> Self { - use crate::atomic_u64::AtomicU64; + use solana_atomic_u64::AtomicU64; static I: AtomicU64 = AtomicU64::new(1); let mut b = [0u8; HASH_BYTES]; diff --git a/sdk/program/src/keccak.rs b/sdk/program/src/keccak.rs index f246944b921a91..680d9bc63b2a16 100644 --- a/sdk/program/src/keccak.rs +++ b/sdk/program/src/keccak.rs @@ -100,7 +100,7 @@ impl Hash { /// unique Hash for tests and benchmarks. pub fn new_unique() -> Self { - use crate::atomic_u64::AtomicU64; + use solana_atomic_u64::AtomicU64; static I: AtomicU64 = AtomicU64::new(1); let mut b = [0u8; HASH_BYTES]; diff --git a/sdk/program/src/lib.rs b/sdk/program/src/lib.rs index 1b7e27810ce258..2862aff1854f74 100644 --- a/sdk/program/src/lib.rs +++ b/sdk/program/src/lib.rs @@ -472,7 +472,6 @@ extern crate self as solana_program; pub mod account_info; pub mod address_lookup_table; pub mod alt_bn128; -pub(crate) mod atomic_u64; pub mod big_mod_exp; pub mod blake3; #[cfg(feature = "borsh")] diff --git a/sdk/program/src/pubkey.rs b/sdk/program/src/pubkey.rs index bbf987341fbd8a..e9cabcc48c141e 100644 --- a/sdk/program/src/pubkey.rs +++ b/sdk/program/src/pubkey.rs @@ -186,7 +186,7 @@ impl Pubkey { /// unique Pubkey for tests and benchmarks. pub fn new_unique() -> Self { - use crate::atomic_u64::AtomicU64; + use solana_atomic_u64::AtomicU64; static I: AtomicU64 = AtomicU64::new(1); let mut b = [0u8; 32];