diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 17d1aed7f..203b70ac4 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -487,9 +487,8 @@ pub unsafe extern "C" fn dc_start_io(context: *mut dc_context_t) { } let ffi_context = &*context; - with_inner_async!(ffi_context, ctx, { ctx.run() }).unwrap_or(()) + with_inner_async!(ffi_context, ctx, { ctx.start_io() }).unwrap_or(()) } - #[no_mangle] pub unsafe extern "C" fn dc_is_io_running(context: *mut dc_context_t) -> libc::c_int { if context.is_null() { @@ -497,7 +496,7 @@ pub unsafe extern "C" fn dc_is_io_running(context: *mut dc_context_t) -> libc::c } let ffi_context = &*context; - with_inner_async!(ffi_context, ctx, { ctx.is_running() }).unwrap_or_default() as libc::c_int + with_inner_async!(ffi_context, ctx, { ctx.is_io_running() }).unwrap_or_default() as libc::c_int } #[no_mangle] @@ -698,7 +697,7 @@ pub unsafe extern "C" fn dc_stop_io(context: *mut dc_context_t) { let ffi_context = &*context; with_inner_async!(ffi_context, ctx, async move { - ctx.stop().await; + ctx.stop_io().await; }) .unwrap_or(()) } diff --git a/examples/repl/main.rs b/examples/repl/main.rs index 86a2088f6..e49844463 100644 --- a/examples/repl/main.rs +++ b/examples/repl/main.rs @@ -277,10 +277,8 @@ async fn start(args: Vec) -> Result<(), Error> { let events = context.get_event_emitter(); async_std::task::spawn(async move { - loop { - if let Some(event) = events.recv().await { - receive_event(event); - } + while let Some(event) = events.recv().await { + receive_event(event); } }); @@ -322,7 +320,7 @@ async fn start(args: Vec) -> Result<(), Error> { } Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => { println!("Exiting..."); - context.stop().await; + context.stop_io().await; break; } Err(err) => { @@ -355,10 +353,10 @@ async fn handle_cmd( match arg0 { "connect" => { - ctx.run().await; + ctx.start_io().await; } "disconnect" => { - ctx.stop().await; + ctx.stop_io().await; } "configure" => { ctx.configure().await?; @@ -381,7 +379,7 @@ async fn handle_cmd( print!("\x1b[1;1H\x1b[2J"); } "getqr" | "getbadqr" => { - ctx.run().await; + ctx.start_io().await; if let Some(mut qr) = dc_get_securejoin_qr(&ctx, ChatId::new(arg1.parse().unwrap_or_default())).await { @@ -400,7 +398,7 @@ async fn handle_cmd( } } "joinqr" => { - ctx.run().await; + ctx.start_io().await; if !arg0.is_empty() { dc_join_securejoin(&ctx, arg1).await; } diff --git a/examples/simple.rs b/examples/simple.rs index 694d901f0..3d8dc6cba 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -39,11 +39,9 @@ async fn main() { println!("info: {:#?}", info); let events = ctx.get_event_emitter(); - async_std::task::spawn(async move { - loop { - if let Some(event) = events.recv().await { - cb(event); - } + let events_spawn = async_std::task::spawn(async move { + while let Some(event) = events.recv().await { + cb(event); } }); @@ -62,7 +60,7 @@ async fn main() { ctx.configure().await.unwrap(); println!("------ RUN ------"); - ctx.clone().run().await; + ctx.clone().start_io().await; println!("--- SENDING A MESSAGE ---"); let contact_id = Contact::create(&ctx, "dignifiedquire", "dignifiedquire@gmail.com") @@ -89,6 +87,7 @@ async fn main() { async_std::task::sleep(duration).await; println!("stopping"); - ctx.stop().await; + ctx.stop_io().await; println!("closing"); + events_spawn.await; } diff --git a/src/context.rs b/src/context.rs index 57067df8a..1fa3d6c8f 100644 --- a/src/context.rs +++ b/src/context.rs @@ -135,21 +135,24 @@ impl Context { Ok(ctx) } - pub async fn run(&self) { - assert!(!self.is_running().await, "context is already running"); + /// Starts the IO scheduler. + pub async fn start_io(&self) { + info!(self, "starting IO"); + assert!(!self.is_io_running().await, "context is already running"); let l = &mut *self.inner.scheduler.write().await; - l.run(self.clone()).await; + l.start(self.clone()).await; } - pub async fn is_running(&self) -> bool { - self.inner.is_running().await + /// Returns if the IO scheduler is running. + pub async fn is_io_running(&self) -> bool { + self.inner.is_io_running().await } - pub async fn stop(&self) { - info!(self, "stopping context"); - self.inner.stop().await; - info!(self, "stopped context"); + /// Stops the IO scheduler. + pub async fn stop_io(&self) { + info!(self, "stopping IO"); + self.inner.stop_io().await; } /// Returns a reference to the underlying SQL instance. @@ -491,12 +494,12 @@ impl Context { } impl InnerContext { - async fn is_running(&self) -> bool { + async fn is_io_running(&self) -> bool { self.scheduler.read().await.is_running() } - async fn stop(&self) { - assert!(self.is_running().await, "context is already stopped"); + async fn stop_io(&self) { + assert!(self.is_io_running().await, "context is already stopped"); let token = { let lock = &*self.scheduler.read().await; lock.pre_stop().await diff --git a/src/scheduler.rs b/src/scheduler.rs index b9f9b5220..c1c84f193 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -276,7 +276,7 @@ async fn smtp_loop(ctx: Context, started: Sender<()>, smtp_handlers: SmtpConnect impl Scheduler { /// Start the scheduler, panics if it is already running. - pub async fn run(&mut self, ctx: Context) { + pub async fn start(&mut self, ctx: Context) { let (mvbox, mvbox_handlers) = ImapConnectionState::new(); let (sentbox, sentbox_handlers) = ImapConnectionState::new(); let (smtp, smtp_handlers) = SmtpConnectionState::new();