mirror of
https://github.com/logos-blockchain/logos-execution-zone.git
synced 2026-05-05 07:43:09 +00:00
Refactor runtime initialization
This commit is contained in:
parent
a97066c41d
commit
c44e8d6a89
@ -28,9 +28,7 @@ pub mod transfer;
|
|||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod wallet;
|
pub mod wallet;
|
||||||
|
|
||||||
use once_cell::sync::OnceCell;
|
use tokio::runtime::{Handle};
|
||||||
use std::sync::Arc;
|
|
||||||
use tokio::runtime::Runtime;
|
|
||||||
|
|
||||||
use crate::error::{print_error, WalletFfiError};
|
use crate::error::{print_error, WalletFfiError};
|
||||||
|
|
||||||
@ -38,15 +36,9 @@ use crate::error::{print_error, WalletFfiError};
|
|||||||
pub use error::WalletFfiError as FfiError;
|
pub use error::WalletFfiError as FfiError;
|
||||||
pub use types::*;
|
pub use types::*;
|
||||||
|
|
||||||
// Global Tokio runtime - initialized once
|
|
||||||
static RUNTIME: OnceCell<Arc<Runtime>> = OnceCell::new();
|
|
||||||
|
|
||||||
/// Get a reference to the global runtime.
|
/// Get a reference to the global runtime.
|
||||||
pub(crate) fn get_runtime() -> Result<&'static Arc<Runtime>, WalletFfiError> {
|
pub(crate) fn get_runtime() -> Result<Handle, WalletFfiError> {
|
||||||
RUNTIME.get().ok_or_else(|| {
|
Handle::try_current().map_err(|_| WalletFfiError::RuntimeError)
|
||||||
print_error("Runtime not initialized. Call wallet_ffi_init_runtime() first.");
|
|
||||||
WalletFfiError::RuntimeError
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run an async future on the global runtime, blocking until completion.
|
/// Run an async future on the global runtime, blocking until completion.
|
||||||
@ -65,12 +57,9 @@ pub(crate) fn block_on<F: std::future::Future>(future: F) -> Result<F::Output, W
|
|||||||
/// - `RuntimeError` if runtime creation failed
|
/// - `RuntimeError` if runtime creation failed
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn wallet_ffi_init_runtime() -> WalletFfiError {
|
pub extern "C" fn wallet_ffi_init_runtime() -> WalletFfiError {
|
||||||
let result = RUNTIME.get_or_try_init(|| {
|
let result = tokio::runtime::Builder::new_multi_thread()
|
||||||
tokio::runtime::Builder::new_multi_thread()
|
.enable_all()
|
||||||
.enable_all()
|
.build();
|
||||||
.build()
|
|
||||||
.map(Arc::new)
|
|
||||||
});
|
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => WalletFfiError::Success,
|
Ok(_) => WalletFfiError::Success,
|
||||||
@ -80,13 +69,3 @@ pub extern "C" fn wallet_ffi_init_runtime() -> WalletFfiError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if the runtime is initialized.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// - `true` if the runtime is ready
|
|
||||||
/// - `false` if `wallet_ffi_init_runtime()` hasn't been called yet
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern "C" fn wallet_ffi_runtime_initialized() -> bool {
|
|
||||||
RUNTIME.get().is_some()
|
|
||||||
}
|
|
||||||
|
|||||||
@ -218,15 +218,6 @@ typedef struct FfiTransferResult {
|
|||||||
*/
|
*/
|
||||||
enum WalletFfiError wallet_ffi_init_runtime(void);
|
enum WalletFfiError wallet_ffi_init_runtime(void);
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the runtime is initialized.
|
|
||||||
*
|
|
||||||
* # Returns
|
|
||||||
* - `true` if the runtime is ready
|
|
||||||
* - `false` if `wallet_ffi_init_runtime()` hasn't been called yet
|
|
||||||
*/
|
|
||||||
bool wallet_ffi_runtime_initialized(void);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new public account.
|
* Create a new public account.
|
||||||
*
|
*
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user