X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning_background_processor.rs;fp=lightning-c-bindings%2Fsrc%2Flightning_background_processor.rs;h=fc44da94f86dc0bba314555130bed269ed72c3ca;hb=a82e075188fc15a103234832686915c196bfe240;hp=3900224e65c9989c775b902c8a7dc2712c6ce08f;hpb=3559b05697ad226c82994bfb6b3d09657d21bbe2;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning_background_processor.rs b/lightning-c-bindings/src/lightning_background_processor.rs index 3900224..fc44da9 100644 --- a/lightning-c-bindings/src/lightning_background_processor.rs +++ b/lightning-c-bindings/src/lightning_background_processor.rs @@ -33,6 +33,7 @@ type nativeBackgroundProcessor = nativeBackgroundProcessorImport; /// then there is a risk of channels force-closing on startup when the manager realizes it's /// outdated. However, as long as `ChannelMonitor` backups are sound, no funds besides those used /// for unilateral chain closure fees are at risk. +///BackgroundProcessor will immediately stop on drop. It should be stored until shutdown. #[must_use] #[repr(C)] pub struct BackgroundProcessor { @@ -93,6 +94,14 @@ pub struct ChannelManagerPersister { } unsafe impl Send for ChannelManagerPersister {} unsafe impl Sync for ChannelManagerPersister {} +#[no_mangle] +pub(crate) extern "C" fn ChannelManagerPersister_clone_fields(orig: &ChannelManagerPersister) -> ChannelManagerPersister { + ChannelManagerPersister { + this_arg: orig.this_arg, + persist_manager: Clone::clone(&orig.persist_manager), + free: Clone::clone(&orig.free), + } +} use lightning_background_processor::ChannelManagerPersister as rustChannelManagerPersister; impl rustChannelManagerPersister for ChannelManagerPersister { @@ -121,21 +130,25 @@ impl Drop for ChannelManagerPersister { } } } -/// Start a background thread that takes care of responsibilities enumerated in the top-level -/// documentation. +/// Start a background thread that takes care of responsibilities enumerated in the [top-level +/// documentation]. /// -/// If `persist_manager` returns an error, then this thread will return said error (and -/// `start()` will need to be called again to restart the `BackgroundProcessor`). Users should -/// wait on [`thread_handle`]'s `join()` method to be able to tell if and when an error is -/// returned, or implement `persist_manager` such that an error is never returned to the -/// `BackgroundProcessor` +/// The thread runs indefinitely unless the object is dropped, [`stop`] is called, or +/// `persist_manager` returns an error. In case of an error, the error is retrieved by calling +/// either [`join`] or [`stop`]. +/// +/// Typically, users should either implement [`ChannelManagerPersister`] to never return an +/// error or call [`join`] and handle any error that may arise. For the latter case, the +/// `BackgroundProcessor` must be restarted by calling `start` again after handling the error. /// /// `persist_manager` is responsible for writing out the [`ChannelManager`] to disk, and/or /// uploading to one or more backup services. See [`ChannelManager::write`] for writing out a /// [`ChannelManager`]. See [`FilesystemPersister::persist_manager`] for Rust-Lightning's /// provided implementation. /// -/// [`thread_handle`]: BackgroundProcessor::thread_handle +/// [top-level documentation]: Self +/// [`join`]: Self::join +/// [`stop`]: Self::stop /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager /// [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable /// [`FilesystemPersister::persist_manager`]: lightning_persister::FilesystemPersister::persist_manager @@ -146,7 +159,32 @@ pub extern "C" fn BackgroundProcessor_start(mut persister: crate::lightning_back BackgroundProcessor { inner: Box::into_raw(Box::new(ret)), is_owned: true } } -/// Stop `BackgroundProcessor`'s thread. +/// Join `BackgroundProcessor`'s thread, returning any error that occurred while persisting +/// [`ChannelManager`]. +/// +/// # Panics +/// +/// This function panics if the background thread has panicked such as while persisting or +/// handling events. +/// +/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager +#[must_use] +#[no_mangle] +pub extern "C" fn BackgroundProcessor_join(mut this_arg: BackgroundProcessor) -> crate::c_types::derived::CResult_NoneErrorZ { + let mut ret = (*unsafe { Box::from_raw(this_arg.take_inner()) }).join(); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::IOError::from_rust(e) }).into() }; + local_ret +} + +/// Stop `BackgroundProcessor`'s thread, returning any error that occurred while persisting +/// [`ChannelManager`]. +/// +/// # Panics +/// +/// This function panics if the background thread has panicked such as while persisting or +/// handling events. +/// +/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager #[must_use] #[no_mangle] pub extern "C" fn BackgroundProcessor_stop(mut this_arg: BackgroundProcessor) -> crate::c_types::derived::CResult_NoneErrorZ {