Skip to content

Commit

Permalink
refactor: a rusty job
Browse files Browse the repository at this point in the history
* refactor(jobthread): safe and rusty
* refactor(job): rusty and safe
  • Loading branch information
dignifiedquire authored Aug 19, 2019
1 parent 1a8e08e commit a906fae
Show file tree
Hide file tree
Showing 21 changed files with 1,491 additions and 1,457 deletions.
30 changes: 15 additions & 15 deletions deltachat-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,111 +189,111 @@ pub unsafe extern "C" fn dc_perform_imap_jobs(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_perform_imap_jobs(context)
job::perform_imap_jobs(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_perform_imap_fetch(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_perform_imap_fetch(context)
job::perform_imap_fetch(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_perform_imap_idle(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_perform_imap_idle(context)
job::perform_imap_idle(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_interrupt_imap_idle(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_interrupt_imap_idle(context)
job::interrupt_imap_idle(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_perform_mvbox_fetch(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_perform_mvbox_fetch(context)
job::perform_mvbox_fetch(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_perform_mvbox_idle(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_perform_mvbox_idle(context)
job::perform_mvbox_idle(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_interrupt_mvbox_idle(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_interrupt_mvbox_idle(context)
job::interrupt_mvbox_idle(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_perform_sentbox_fetch(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_perform_sentbox_fetch(context)
job::perform_sentbox_fetch(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_perform_sentbox_idle(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_perform_sentbox_idle(context)
job::perform_sentbox_idle(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_interrupt_sentbox_idle(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_interrupt_sentbox_idle(context)
job::interrupt_sentbox_idle(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_perform_smtp_jobs(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_perform_smtp_jobs(context)
job::perform_smtp_jobs(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_perform_smtp_idle(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_perform_smtp_idle(context)
job::perform_smtp_idle(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_interrupt_smtp_idle(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_interrupt_smtp_idle(context)
job::interrupt_smtp_idle(context)
}

#[no_mangle]
pub unsafe extern "C" fn dc_maybe_network(context: *mut dc_context_t) {
assert!(!context.is_null());
let context = &*context;

dc_job::dc_maybe_network(context)
job::maybe_network(context)
}

#[no_mangle]
Expand Down Expand Up @@ -1034,7 +1034,7 @@ pub unsafe extern "C" fn dc_send_locations_to_chat(
assert!(!context.is_null());
let context = &*context;

dc_location::dc_send_locations_to_chat(context, chat_id, seconds)
dc_location::dc_send_locations_to_chat(context, chat_id, seconds as i64)
}

#[no_mangle]
Expand Down
4 changes: 2 additions & 2 deletions examples/repl/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use deltachat::contact::*;
use deltachat::context::*;
use deltachat::dc_configure::*;
use deltachat::dc_imex::*;
use deltachat::dc_job::*;
use deltachat::dc_location::*;
use deltachat::dc_msg::*;
use deltachat::dc_receive_imf::*;
use deltachat::dc_tools::*;
use deltachat::job::*;
use deltachat::lot::LotState;
use deltachat::peerstate::*;
use deltachat::qr::*;
Expand Down Expand Up @@ -581,7 +581,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
println!("{}", to_string(dc_get_info(context)));
}
"maybenetwork" => {
dc_maybe_network(context);
maybe_network(context);
}
"housekeeping" => {
sql::housekeeping(context);
Expand Down
36 changes: 16 additions & 20 deletions examples/repl/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use deltachat::config;
use deltachat::constants::*;
use deltachat::context::*;
use deltachat::dc_configure::*;
use deltachat::dc_job::*;
use deltachat::dc_securejoin::*;
use deltachat::dc_tools::*;
use deltachat::job::*;
use deltachat::oauth2::*;
use deltachat::types::*;
use deltachat::x::*;
Expand Down Expand Up @@ -172,43 +172,41 @@ fn start_threads(c: Arc<RwLock<Context>>) {
let ctx = c.clone();
let handle_imap = std::thread::spawn(move || loop {
while_running!({
unsafe {
dc_perform_imap_jobs(&ctx.read().unwrap());
dc_perform_imap_fetch(&ctx.read().unwrap());
}
perform_imap_jobs(&ctx.read().unwrap());
perform_imap_fetch(&ctx.read().unwrap());
while_running!({
let context = ctx.read().unwrap();
dc_perform_imap_idle(&context);
perform_imap_idle(&context);
});
});
});

let ctx = c.clone();
let handle_mvbox = std::thread::spawn(move || loop {
while_running!({
unsafe { dc_perform_mvbox_fetch(&ctx.read().unwrap()) };
perform_mvbox_fetch(&ctx.read().unwrap());
while_running!({
unsafe { dc_perform_mvbox_idle(&ctx.read().unwrap()) };
perform_mvbox_idle(&ctx.read().unwrap());
});
});
});

let ctx = c.clone();
let handle_sentbox = std::thread::spawn(move || loop {
while_running!({
unsafe { dc_perform_sentbox_fetch(&ctx.read().unwrap()) };
perform_sentbox_fetch(&ctx.read().unwrap());
while_running!({
unsafe { dc_perform_sentbox_idle(&ctx.read().unwrap()) };
perform_sentbox_idle(&ctx.read().unwrap());
});
});
});

let ctx = c;
let handle_smtp = std::thread::spawn(move || loop {
while_running!({
unsafe { dc_perform_smtp_jobs(&ctx.read().unwrap()) };
perform_smtp_jobs(&ctx.read().unwrap());
while_running!({
unsafe { dc_perform_smtp_idle(&ctx.read().unwrap()) };
perform_smtp_idle(&ctx.read().unwrap());
});
});
});
Expand All @@ -226,12 +224,10 @@ fn stop_threads(context: &Context) {
println!("Stopping threads");
IS_RUNNING.store(false, Ordering::Relaxed);

unsafe {
dc_interrupt_imap_idle(context);
dc_interrupt_mvbox_idle(context);
dc_interrupt_sentbox_idle(context);
dc_interrupt_smtp_idle(context);
}
interrupt_imap_idle(context);
interrupt_mvbox_idle(context);
interrupt_sentbox_idle(context);
interrupt_smtp_idle(context);

handle.handle_imap.take().unwrap().join().unwrap();
handle.handle_mvbox.take().unwrap().join().unwrap();
Expand Down Expand Up @@ -487,14 +483,14 @@ unsafe fn handle_cmd(line: &str, ctx: Arc<RwLock<Context>>) -> Result<ExitResult
if HANDLE.clone().lock().unwrap().is_some() {
println!("smtp-jobs are already running in a thread.",);
} else {
dc_perform_smtp_jobs(&ctx.read().unwrap());
perform_smtp_jobs(&ctx.read().unwrap());
}
}
"imap-jobs" => {
if HANDLE.clone().lock().unwrap().is_some() {
println!("imap-jobs are already running in a thread.");
} else {
dc_perform_imap_jobs(&ctx.read().unwrap());
perform_imap_jobs(&ctx.read().unwrap());
}
}
"configure" => {
Expand Down
19 changes: 9 additions & 10 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use deltachat::constants::Event;
use deltachat::contact::*;
use deltachat::context::*;
use deltachat::dc_configure::*;
use deltachat::dc_job::{
dc_perform_imap_fetch, dc_perform_imap_idle, dc_perform_imap_jobs, dc_perform_smtp_idle,
dc_perform_smtp_jobs,
use deltachat::job::{
perform_imap_fetch, perform_imap_idle, perform_imap_jobs, perform_smtp_idle, perform_smtp_jobs,
};

extern "C" fn cb(_ctx: &Context, event: Event, data1: usize, data2: usize) -> usize {
Expand Down Expand Up @@ -52,12 +51,12 @@ fn main() {
let r1 = running.clone();
let t1 = thread::spawn(move || {
while *r1.read().unwrap() {
dc_perform_imap_jobs(&ctx1);
perform_imap_jobs(&ctx1);
if *r1.read().unwrap() {
dc_perform_imap_fetch(&ctx1);
perform_imap_fetch(&ctx1);

if *r1.read().unwrap() {
dc_perform_imap_idle(&ctx1);
perform_imap_idle(&ctx1);
}
}
}
Expand All @@ -67,9 +66,9 @@ fn main() {
let r1 = running.clone();
let t2 = thread::spawn(move || {
while *r1.read().unwrap() {
dc_perform_smtp_jobs(&ctx1);
perform_smtp_jobs(&ctx1);
if *r1.read().unwrap() {
dc_perform_smtp_idle(&ctx1);
perform_smtp_idle(&ctx1);
}
}
});
Expand Down Expand Up @@ -123,8 +122,8 @@ fn main() {
println!("stopping threads");

*running.clone().write().unwrap() = false;
deltachat::dc_job::dc_interrupt_imap_idle(&ctx);
deltachat::dc_job::dc_interrupt_smtp_idle(&ctx);
deltachat::job::interrupt_imap_idle(&ctx);
deltachat::job::interrupt_smtp_idle(&ctx);

println!("joining");
t1.join().unwrap();
Expand Down
10 changes: 5 additions & 5 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::chatlist::*;
use crate::constants::*;
use crate::contact::*;
use crate::context::Context;
use crate::dc_job::*;
use crate::dc_msg::*;
use crate::dc_tools::*;
use crate::error::Error;
use crate::job::*;
use crate::param::*;
use crate::sql::{self, Sql};
use crate::stock::StockMessage;
Expand Down Expand Up @@ -898,7 +898,7 @@ pub unsafe fn send_msg<'a>(
}

ensure!(
dc_job_send_msg(context, (*msg).id) != 0,
job_send_msg(context, (*msg).id) != 0,
"Failed to initiate send job"
);

Expand Down Expand Up @@ -1339,8 +1339,8 @@ pub fn delete(context: &Context, chat_id: u32) -> Result<(), Error> {

context.call_cb(Event::MSGS_CHANGED, 0 as uintptr_t, 0 as uintptr_t);

dc_job_kill_action(context, 105);
unsafe { dc_job_add(context, 105, 0, Params::new(), 10) };
job_kill_action(context, Action::Housekeeping);
job_add(context, Action::Housekeeping, 0, Params::new(), 10);

Ok(())
}
Expand Down Expand Up @@ -1927,7 +1927,7 @@ pub unsafe fn forward_msgs(
new_msg_id = chat
.prepare_msg_raw(context, msg, fresh10)
.unwrap_or_default();
dc_job_send_msg(context, new_msg_id);
job_send_msg(context, new_msg_id);
}
created_db_entries.push(chat_id);
created_db_entries.push(new_msg_id);
Expand Down
8 changes: 4 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use strum_macros::{AsRefStr, Display, EnumIter, EnumProperty, EnumString};

use crate::constants::DC_VERSION_STR;
use crate::context::Context;
use crate::dc_job::*;
use crate::dc_tools::*;
use crate::error::Error;
use crate::job::*;
use crate::stock::StockMessage;

/// The available configuration keys.
Expand Down Expand Up @@ -102,17 +102,17 @@ impl Context {
}
Config::InboxWatch => {
let ret = self.sql.set_config(self, key, value);
unsafe { dc_interrupt_imap_idle(self) };
interrupt_imap_idle(self);
ret
}
Config::SentboxWatch => {
let ret = self.sql.set_config(self, key, value);
unsafe { dc_interrupt_sentbox_idle(self) };
interrupt_sentbox_idle(self);
ret
}
Config::MvboxWatch => {
let ret = self.sql.set_config(self, key, value);
unsafe { dc_interrupt_mvbox_idle(self) };
interrupt_mvbox_idle(self);
ret
}
Config::Selfstatus => {
Expand Down
1 change: 0 additions & 1 deletion src/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,6 @@ fn get_first_name<'a>(full_name: &'a str) -> &'a str {
/// Returns false if addr is an invalid address, otherwise true.
pub fn may_be_valid_addr(addr: &str) -> bool {
let res = addr.parse::<EmailAddress>();
println!("{:?}", res);
res.is_ok()
}

Expand Down
Loading

0 comments on commit a906fae

Please sign in to comment.