refactor next_event

This commit is contained in:
dignifiedquire
2020-05-13 18:42:56 +02:00
parent 8a7923c974
commit b25bec53d8
6 changed files with 10 additions and 38 deletions

2
Cargo.lock generated
View File

@@ -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)",

View File

@@ -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"

View File

@@ -190,12 +190,6 @@ typedef struct _dc_event dc_event_t;
*/
/**
* TODO: document
*/
int dc_has_next_event(dc_context_t* context);
/**
* TODO: document
*/

View File

@@ -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,

View File

@@ -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);
}
}
});

View File

@@ -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<HashMap<usize, String>>,
pub(crate) logs: SegQueue<Event>,
pub(crate) logs: (SyncSender<Event>, SyncReceiver<Event>),
pub(crate) scheduler: RwLock<Scheduler>,
@@ -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<Event> {
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<Receiver<()>> {
if self.has_ongoing().await {