diff --git a/Cargo.lock b/Cargo.lock index ff9ad0b17..5e2509042 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -670,7 +670,7 @@ dependencies = [ "byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "charset 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", - "crossbeam-queue 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-channel 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "debug_stub_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "deltachat_derive 2.0.0", "email 0.0.21 (git+https://github.com/deltachat/rust-email)", diff --git a/Cargo.toml b/Cargo.toml index 9d9e5577c..df3e5f387 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,6 @@ encoded-words = { git = "https://github.com/async-email/encoded-words", branch=" native-tls = "0.2.3" image = { version = "0.22.4", default-features=false, features = ["gif_codec", "jpeg", "ico", "png_codec", "pnm", "webp", "bmp"] } futures = "0.3.4" -crossbeam-queue = "0.2.1" thiserror = "1.0.14" anyhow = "1.0.28" @@ -65,6 +64,7 @@ log = {version = "0.4.8", optional = true } rustyline = { version = "4.1.0", optional = true } ansi_term = { version = "0.12.1", optional = true } async-trait = "0.1.31" +crossbeam-channel = "0.4.2" [dev-dependencies] tempfile = "3.0" diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index e8a231386..7a497d369 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -190,12 +190,6 @@ typedef struct _dc_event dc_event_t; */ - -/** - * TODO: document - */ -int dc_has_next_event(dc_context_t* context); - /** * TODO: document */ diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index c5171c764..0c4949d30 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -501,18 +501,6 @@ pub unsafe extern "C" fn dc_is_running(context: *mut dc_context_t) -> libc::c_in with_inner_async!(ffi_context, ctx, { ctx.is_running() }).unwrap_or_default() as libc::c_int } -#[no_mangle] -pub unsafe extern "C" fn dc_has_next_event(context: *mut dc_context_t) -> libc::c_int { - if context.is_null() { - return 0; - } - let ffi_context = &*context; - - ffi_context - .with_inner(|ctx| ctx.has_next_event()) - .unwrap_or_default() as libc::c_int -} - pub struct EventWrapper { pub event_id: libc::c_int, pub data1: uintptr_t, diff --git a/examples/simple.rs b/examples/simple.rs index 354a9b7d3..2542c89cd 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -41,12 +41,8 @@ async fn main() { let ctx1 = ctx.clone(); async_std::task::spawn(async move { loop { - if ctx1.has_next_event() { - if let Ok(event) = ctx1.get_next_event() { - cb(event); - } - } else { - async_std::task::sleep(time::Duration::from_millis(50)).await; + if let Ok(event) = ctx1.get_next_event() { + cb(event); } } }); diff --git a/src/context.rs b/src/context.rs index 58b5d534b..5c554ba5f 100644 --- a/src/context.rs +++ b/src/context.rs @@ -7,7 +7,7 @@ use std::ops::Deref; use anyhow::anyhow; use async_std::path::{Path, PathBuf}; use async_std::sync::{channel, Arc, Mutex, Receiver, RwLock, Sender}; -use crossbeam_queue::SegQueue; +use crossbeam_channel::{unbounded, Receiver as SyncReceiver, Sender as SyncSender}; use crate::chat::*; use crate::config::Config; @@ -53,7 +53,7 @@ pub struct InnerContext { /// Mutex to avoid generating the key for the user more than once. pub(crate) generating_key_mutex: Mutex<()>, pub(crate) translated_stockstrings: RwLock>, - pub(crate) logs: SegQueue, + pub(crate) logs: (SyncSender, SyncReceiver), pub(crate) scheduler: RwLock, @@ -118,7 +118,7 @@ impl Context { last_smeared_timestamp: RwLock::new(0), generating_key_mutex: Mutex::new(()), translated_stockstrings: RwLock::new(HashMap::new()), - logs: SegQueue::new(), + logs: unbounded(), scheduler: RwLock::new(Scheduler::Stopped), creation_time: std::time::SystemTime::now(), }; @@ -170,22 +170,16 @@ impl Context { } pub fn call_cb(&self, event: Event) { - self.logs.push(event); + self.logs.0.send(event).expect("failed to send event"); } pub fn get_next_event(&self) -> Result { - let event = self.logs.pop().map_err(|err| anyhow!("{}", err))?; + let event = self.logs.1.recv().map_err(|err| anyhow!("{}", err))?; Ok(event) } - pub fn has_next_event(&self) -> bool { - !self.logs.is_empty() - } - - /******************************************************************************* - * Ongoing process allocation/free/check - ******************************************************************************/ + // Ongoing process allocation/free/check pub async fn alloc_ongoing(&self) -> Result> { if self.has_ongoing().await {