diff --git a/src/dc_configure.rs b/src/dc_configure.rs index a7815cb19..351b93a35 100644 --- a/src/dc_configure.rs +++ b/src/dc_configure.rs @@ -72,13 +72,14 @@ pub unsafe fn dc_configure(mut context: &dc_context_t) { dc_job_add(context, 900i32, 0i32, 0 as *const libc::c_char, 0i32); } pub unsafe fn dc_has_ongoing(mut context: &dc_context_t) -> libc::c_int { - return if 0 != context.ongoing_running - || *context.shall_stop_ongoing.clone().read().unwrap() == 0 - { - 1i32 + let s_a = context.running_state.clone(); + let s = s_a.read().unwrap(); + + if s.ongoing_running || !s.shall_stop_ongoing { + 1 } else { - 0i32 - }; + 0 + } } pub unsafe fn dc_is_configured(mut context: &dc_context_t) -> libc::c_int { return if 0 @@ -94,13 +95,16 @@ pub unsafe fn dc_is_configured(mut context: &dc_context_t) -> libc::c_int { }; } pub unsafe fn dc_stop_ongoing_process(context: &dc_context_t) { - if 0 != context.ongoing_running && *context.shall_stop_ongoing.clone().read().unwrap() == 0 { + let s_a = context.running_state.clone(); + let mut s = s_a.write().unwrap(); + + if s.ongoing_running && !s.shall_stop_ongoing { dc_log_info( context, 0i32, b"Signaling the ongoing process to stop ASAP.\x00" as *const u8 as *const libc::c_char, ); - *context.shall_stop_ongoing.clone().write().unwrap() = 1; + s.shall_stop_ongoing = true; } else { dc_log_info( context, @@ -186,7 +190,10 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut 0i32, b"Configure ...\x00" as *const u8 as *const libc::c_char, ); - if !(0 != *context.shall_stop_ongoing.clone().read().unwrap()) { + let s_a = context.running_state.clone(); + let s = s_a.read().unwrap(); + + if s.shall_stop_ongoing { (context.cb)( context, Event::CONFIGURE_PROGRESS, @@ -218,7 +225,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut // the used oauth2 addr may differ, check this. // if dc_get_oauth2_addr() is not available in the oauth2 implementation, // just use the given one. - if 0 != *context.shall_stop_ongoing.clone().read().unwrap() { + if s.shall_stop_ongoing { current_block = 2927484062889439186; } else { (context.cb)( @@ -245,7 +252,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut (*param).addr, ); } - if 0 != *context.shall_stop_ongoing.clone().read().unwrap() { + if s.shall_stop_ongoing { current_block = 2927484062889439186; } else { (context.cb)( @@ -284,7 +291,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut if (*param).mail_pw.is_null() { (*param).mail_pw = dc_strdup(0 as *const libc::c_char) } - if !(0 != *context.shall_stop_ongoing.clone().read().unwrap()) { + if !s.shall_stop_ongoing { (context.cb)( context, Event::CONFIGURE_PROGRESS, @@ -325,12 +332,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut param_autoconfig = moz_autoconfigure(context, url, param); free(url as *mut libc::c_void); - if 0 != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap() - { + if s.shall_stop_ongoing { current_block = 2927484062889439186; } else { (context.cb)( @@ -368,12 +370,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut param_autoconfig = moz_autoconfigure(context, url_0, param); free(url_0 as *mut libc::c_void); - if 0 != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap() - { + if s.shall_stop_ongoing { current_block = 2927484062889439186; } else { (context.cb)( @@ -435,12 +432,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut context, url_1, param, ); free(url_1 as *mut libc::c_void); - if 0 != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap() - { + if s.shall_stop_ongoing { current_block = 2927484062889439186; break; @@ -483,12 +475,8 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut free( url_2 as *mut libc::c_void, ); - if 0 != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap() - { + + if s.shall_stop_ongoing { current_block = 2927484062889439186; } else { @@ -544,11 +532,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut free(url_3 as *mut libc::c_void); - if 0 != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap() + if s.shall_stop_ongoing { current_block = @@ -607,9 +591,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut free(url_4 as *mut libc::c_void); - if 0 - != - *context.shall_stop_ongoing.clone().read().unwrap() + if s.shall_stop_ongoing { current_block = @@ -834,13 +816,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut b"Account settings incomplete.\x00" as *const u8 as *const libc::c_char, ); - } else if !(0 - != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap()) - { + } else if !s.shall_stop_ongoing { (context.cb)( context, Event::CONFIGURE_PROGRESS, @@ -885,12 +861,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut break; } // probe STARTTLS/993 - if 0 != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap() - { + if s.shall_stop_ongoing { current_block = 2927484062889439186; break; } @@ -934,12 +905,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut break; } // probe STARTTLS/143 - if 0 != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap() - { + if s.shall_stop_ongoing { current_block = 2927484062889439186; break; } @@ -985,12 +951,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut break; } // next probe round with only the localpart of the email-address as the loginname - if 0 != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap() - { + if s.shall_stop_ongoing { current_block = 2927484062889439186; break; } @@ -1031,13 +992,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut 2927484062889439186 => {} _ => { imap_connected_here = 1i32; - if !(0 - != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap()) - { + if !s.shall_stop_ongoing { (context.cb)( context, Event::CONFIGURE_PROGRESS, @@ -1064,13 +1019,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut if !param_autoconfig.is_null() { current_block = 2927484062889439186; - } else if 0 - != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap() - { + } else if s.shall_stop_ongoing { current_block = 2927484062889439186; } else { @@ -1116,12 +1065,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut .unwrap(), param, ) { - if 0 != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap() - { + if s.shall_stop_ongoing { current_block = 2927484062889439186; } else { @@ -1199,13 +1143,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut 2927484062889439186 => {} _ => { smtp_connected_here = 1i32; - if !(0 - != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap()) - { + if !s.shall_stop_ongoing { (context.cb)(context, Event::CONFIGURE_PROGRESS, (if 900i32 @@ -1261,13 +1199,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut .unwrap(), flags, ); - if !(0 - != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap()) - { + if !s.shall_stop_ongoing { (context.cb)(context, Event::CONFIGURE_PROGRESS, (if 910i32 @@ -1302,11 +1234,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut as *const libc::c_char, 1i32); - if !(0 != *context - .shall_stop_ongoing - .clone() - .read() - .unwrap()) + if !s.shall_stop_ongoing { (context.cb)(context, Event::CONFIGURE_PROGRESS, @@ -1337,9 +1265,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut *const u8 as *const libc::c_char); - if !(0 - != - *context.shall_stop_ongoing.clone().read().unwrap()) + if !s.shall_stop_ongoing { (context.cb)(context, Event::CONFIGURE_PROGRESS, @@ -1403,10 +1329,12 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &dc_context_t, _job: *mut ); } -pub unsafe fn dc_free_ongoing(mut context: &dc_context_t) { - // FIXME - // context.ongoing_running = 0i32; - *context.shall_stop_ongoing.clone().write().unwrap() = 1; +pub unsafe fn dc_free_ongoing(context: &dc_context_t) { + let s_a = context.running_state.clone(); + let mut s = s_a.write().unwrap(); + + s.ongoing_running = false; + s.shall_stop_ongoing = true; } pub unsafe fn dc_configure_folders( @@ -2163,10 +2091,13 @@ pub unsafe fn dc_alloc_ongoing(context: &dc_context_t) -> libc::c_int { ); return 0i32; } - // FIXME - // context.ongoing_running = 1i32; - *context.shall_stop_ongoing.clone().write().unwrap() = 0; - return 1i32; + let s_a = context.running_state.clone(); + let mut s = s_a.write().unwrap(); + + s.ongoing_running = true; + s.shall_stop_ongoing = false; + + 1 } pub unsafe fn dc_connect_to_configured_imap( diff --git a/src/dc_context.rs b/src/dc_context.rs index 6bb91b5f4..a0d30b132 100644 --- a/src/dc_context.rs +++ b/src/dc_context.rs @@ -42,13 +42,27 @@ pub struct dc_context_t { pub cmdline_sel_chat_id: u32, pub bob: Arc>, pub last_smeared_timestamp: Arc>, - pub ongoing_running: i32, - pub shall_stop_ongoing: Arc>, + pub running_state: Arc>, } unsafe impl std::marker::Send for dc_context_t {} unsafe impl std::marker::Sync for dc_context_t {} +#[derive(Debug)] +pub struct RunningState { + pub ongoing_running: bool, + pub shall_stop_ongoing: bool, +} + +impl Default for RunningState { + fn default() -> Self { + RunningState { + ongoing_running: false, + shall_stop_ongoing: true, + } + } +} + #[derive(Debug)] pub struct BobStatus { pub expects: i32, @@ -111,8 +125,7 @@ pub fn dc_context_new( userdata, cb, os_name: unsafe { dc_strdup_keep_null(os_name) }, - ongoing_running: 0, - shall_stop_ongoing: Arc::new(RwLock::new(1)), + running_state: Arc::new(RwLock::new(Default::default())), sql: Arc::new(Mutex::new(dc_sqlite3_new())), smtp: Arc::new(Mutex::new(dc_smtp_new())), smtp_state: Arc::new((Mutex::new(Default::default()), Condvar::new())), diff --git a/src/dc_imex.rs b/src/dc_imex.rs index 4e84871af..cb3f2b191 100644 --- a/src/dc_imex.rs +++ b/src/dc_imex.rs @@ -165,11 +165,23 @@ pub unsafe fn dc_initiate_key_transfer(mut context: &dc_context_t) -> *mut libc: setup_code = dc_create_setup_code(context); if !setup_code.is_null() { /* this may require a keypair to be created. this may take a second ... */ - if !(0 != *context.shall_stop_ongoing.clone().read().unwrap()) { + if !context + .running_state + .clone() + .read() + .unwrap() + .shall_stop_ongoing + { setup_file_content = dc_render_setup_file(context, setup_code); if !setup_file_content.is_null() { /* encrypting may also take a while ... */ - if !(0 != *context.shall_stop_ongoing.clone().read().unwrap()) { + if !context + .running_state + .clone() + .read() + .unwrap() + .shall_stop_ongoing + { setup_file_name = dc_get_fine_pathNfilename( context, b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, @@ -196,7 +208,13 @@ pub unsafe fn dc_initiate_key_transfer(mut context: &dc_context_t) -> *mut libc: ); dc_param_set_int((*msg).param, 'S' as i32, 6i32); dc_param_set_int((*msg).param, 'u' as i32, 2i32); - if !(0 != *context.shall_stop_ongoing.clone().read().unwrap()) { + if !context + .running_state + .clone() + .read() + .unwrap() + .shall_stop_ongoing + { msg_id = dc_send_msg(context, chat_id, msg); if !(msg_id == 0i32 as libc::c_uint) { dc_msg_unref(msg); @@ -208,7 +226,12 @@ pub unsafe fn dc_initiate_key_transfer(mut context: &dc_context_t) -> *mut libc: as *const libc::c_char, ); loop { - if 0 != *context.shall_stop_ongoing.clone().read().unwrap() + if context + .running_state + .clone() + .read() + .unwrap() + .shall_stop_ongoing { current_block = 6116957410927263949; break; @@ -1005,7 +1028,13 @@ unsafe fn import_backup( current_block = 10891380440665537214; break; } - if 0 != *context.shall_stop_ongoing.clone().read().unwrap() { + if context + .running_state + .clone() + .read() + .unwrap() + .shall_stop_ongoing + { current_block = 8648553629232744886; break; } @@ -1223,7 +1252,12 @@ unsafe fn export_backup(mut context: &dc_context_t, mut dir: *const libc::c_char current_block = 2631791190359682872; break; } - if 0 != *context.shall_stop_ongoing.clone().read().unwrap() + if context + .running_state + .clone() + .read() + .unwrap() + .shall_stop_ongoing { delete_dest_file = 1i32; current_block = 11487273724841241105; diff --git a/src/dc_job.rs b/src/dc_job.rs index 523e5b5dd..008920d44 100644 --- a/src/dc_job.rs +++ b/src/dc_job.rs @@ -1,6 +1,6 @@ use libc; use rand::{thread_rng, Rng}; -use std::time::Duration; +use std::time::{Duration, SystemTime}; use crate::constants::Event; use crate::dc_chat::*; @@ -1249,9 +1249,7 @@ pub unsafe fn dc_perform_smtp_idle(mut context: &dc_context_t) { as *const libc::c_char, ); } else { - // FIXME: correct time based on - // get_next_wakeup_time(context, 5000) + 1; - let dur = Duration::from_millis(5000); + let dur = get_next_wakeup_time(context, 5000); loop { let res = cvar.wait_timeout(state, dur).unwrap(); @@ -1273,24 +1271,29 @@ pub unsafe fn dc_perform_smtp_idle(mut context: &dc_context_t) { ); } -unsafe fn get_next_wakeup_time(context: &dc_context_t, thread: libc::c_int) -> time_t { - let mut wakeup_time: time_t = 0i32 as time_t; - let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; - stmt = dc_sqlite3_prepare( +unsafe fn get_next_wakeup_time(context: &dc_context_t, thread: libc::c_int) -> Duration { + let stmt = dc_sqlite3_prepare( context, &mut context.sql.clone().lock().unwrap(), b"SELECT MIN(desired_timestamp) FROM jobs WHERE thread=?;\x00" as *const u8 as *const libc::c_char, ); - sqlite3_bind_int(stmt, 1i32, thread); - if sqlite3_step(stmt) == 100i32 { - wakeup_time = sqlite3_column_int(stmt, 0i32) as time_t - } - if wakeup_time == 0i32 as libc::c_long { - wakeup_time = time(0 as *mut time_t) + (10i32 * 60i32) as libc::c_long + sqlite3_bind_int(stmt, 1, thread); + + let mut wakeup_time = Duration::new(10 * 60, 0); + + if sqlite3_step(stmt) == 100 { + let t = sqlite3_column_int(stmt, 0) as u64; + let now = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap(); + if t > 0 { + wakeup_time = Duration::new(t, 0) - now; + } } + sqlite3_finalize(stmt); - return wakeup_time; + wakeup_time } pub unsafe fn dc_maybe_network(mut context: &dc_context_t) { diff --git a/src/dc_securejoin.rs b/src/dc_securejoin.rs index 9e59e3147..46528cbb7 100644 --- a/src/dc_securejoin.rs +++ b/src/dc_securejoin.rs @@ -208,7 +208,13 @@ pub unsafe fn dc_join_securejoin( 0i32, b"Unknown contact.\x00" as *const u8 as *const libc::c_char, ); - } else if !(0 != *context.shall_stop_ongoing.clone().read().unwrap()) { + } else if !(context + .running_state + .clone() + .read() + .unwrap() + .shall_stop_ongoing) + { join_vg = ((*qr_scan).state == 202i32) as libc::c_int; let bob_a = context.bob.clone(); let mut bob = bob_a.write().unwrap(); @@ -262,7 +268,13 @@ pub unsafe fn dc_join_securejoin( ); } // Bob -> Alice - while !(0 != *context.shall_stop_ongoing.clone().read().unwrap()) { + while !(context + .running_state + .clone() + .read() + .unwrap() + .shall_stop_ongoing) + { usleep((300i32 * 1000i32) as useconds_t); } }