X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-c-bindings;a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning_background_processor.rs;h=5279e3aa3dbac22411b363a46b322fbc33f13678;hp=3900224e65c9989c775b902c8a7dc2712c6ce08f;hb=ed6eb46f7b4e0d4c869ad08dffc600b4e93e2129;hpb=f0a481f3ab7db33c7cc9cf607f68727b3f89095d diff --git a/lightning-c-bindings/src/lightning_background_processor.rs b/lightning-c-bindings/src/lightning_background_processor.rs index 3900224..5279e3a 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 { @@ -51,7 +52,7 @@ pub struct BackgroundProcessor { impl Drop for BackgroundProcessor { fn drop(&mut self) { if self.is_owned && !<*mut nativeBackgroundProcessor>::is_null(self.inner) { - let _ = unsafe { Box::from_raw(self.inner) }; + let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) }; } } } @@ -64,11 +65,17 @@ extern "C" fn BackgroundProcessor_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeBackgroundProcessor); } } #[allow(unused)] -/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy impl BackgroundProcessor { + pub(crate) fn get_native_ref(&self) -> &'static nativeBackgroundProcessor { + unsafe { &*ObjOps::untweak_ptr(self.inner) } + } + pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBackgroundProcessor { + unsafe { &mut *ObjOps::untweak_ptr(self.inner) } + } + /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy pub(crate) fn take_inner(mut self) -> *mut nativeBackgroundProcessor { assert!(self.is_owned); - let ret = self.inner; + let ret = ObjOps::untweak_ptr(self.inner); self.inner = std::ptr::null_mut(); ret } @@ -93,11 +100,19 @@ 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 { fn persist_manager(&self, mut channel_manager: &lightning::ln::channelmanager::ChannelManager) -> Result<(), std::io::Error> { - let mut ret = (self.persist_manager)(self.this_arg, &crate::lightning::ln::channelmanager::ChannelManager { inner: unsafe { (channel_manager as *const _) as *mut _ }, is_owned: false }); + let mut ret = (self.persist_manager)(self.this_arg, &crate::lightning::ln::channelmanager::ChannelManager { inner: unsafe { ObjOps::nonnull_ptr_to_inner((channel_manager as *const _) as *mut _) }, is_owned: false }); let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).to_rust() })}; local_ret } @@ -121,32 +136,61 @@ 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 #[must_use] #[no_mangle] pub extern "C" fn BackgroundProcessor_start(mut persister: crate::lightning_background_processor::ChannelManagerPersister, mut event_handler: crate::lightning::util::events::EventHandler, chain_monitor: &crate::lightning::chain::chainmonitor::ChainMonitor, channel_manager: &crate::lightning::ln::channelmanager::ChannelManager, peer_manager: &crate::lightning::ln::peer_handler::PeerManager, mut logger: crate::lightning::util::logger::Logger) -> BackgroundProcessor { - let mut ret = lightning_background_processor::BackgroundProcessor::start(persister, event_handler, unsafe { &*chain_monitor.inner }, unsafe { &*channel_manager.inner }, unsafe { &*peer_manager.inner }, logger); - BackgroundProcessor { inner: Box::into_raw(Box::new(ret)), is_owned: true } + let mut ret = lightning_background_processor::BackgroundProcessor::start(persister, event_handler, chain_monitor.get_native_ref(), channel_manager.get_native_ref(), peer_manager.get_native_ref(), logger); + BackgroundProcessor { inner: ObjOps::heap_alloc(ret), is_owned: true } +} + +/// 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. +/// 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 {