use start/stop io

This commit is contained in:
B. Petersen
2020-05-25 18:07:12 +02:00
parent d42eb1d920
commit 8fd3b374c2
8 changed files with 31 additions and 288 deletions
+6 -64
View File
@@ -250,79 +250,21 @@ JNIEXPORT jint Java_com_b44t_messenger_DcContext_isConfigured(JNIEnv *env, jobje
}
// TODO-ASYNC: delete these functions, also in the java-part
JNIEXPORT void Java_com_b44t_messenger_DcContext_performImapJobs(JNIEnv *env, jobject obj)
JNIEXPORT void Java_com_b44t_messenger_DcContext_startIo(JNIEnv *env, jobject obj)
{
dc_start_io(get_dc_context(env, obj));
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_performImapIdle(JNIEnv *env, jobject obj)
JNIEXPORT void Java_com_b44t_messenger_DcContext_stopIo(JNIEnv *env, jobject obj)
{
dc_stop_io(get_dc_context(env, obj));
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_performImapFetch(JNIEnv *env, jobject obj)
{
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptImapIdle(JNIEnv *env, jobject obj)
{
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_performSentboxJobs(JNIEnv *env, jobject obj)
{
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_performSentboxFetch(JNIEnv *env, jobject obj)
{
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_performSentboxIdle(JNIEnv *env, jobject obj)
{
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptSentboxIdle(JNIEnv *env, jobject obj)
{
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_performMvboxJobs(JNIEnv *env, jobject obj)
{
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_performMvboxFetch(JNIEnv *env, jobject obj)
{
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_performMvboxIdle(JNIEnv *env, jobject obj)
{
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptMvboxIdle(JNIEnv *env, jobject obj)
{
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_performSmtpJobs(JNIEnv *env, jobject obj)
{
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_performSmtpIdle(JNIEnv *env, jobject obj)
{
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_interruptSmtpIdle(JNIEnv *env, jobject obj)
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_isIoRunning(JNIEnv *env, jobject obj)
{
return dc_is_io_running(get_dc_context(env, obj)) != 0;
}
+3 -18
View File
@@ -99,24 +99,9 @@ public class DcContext {
public native void stopOngoingProcess ();
public native int isConfigured ();
public native void performImapJobs ();
public native void performImapFetch ();
public native void performImapIdle ();
public native void interruptImapIdle ();
public native void performMvboxJobs ();
public native void performMvboxFetch ();
public native void performMvboxIdle ();
public native void interruptMvboxIdle ();
public native void performSentboxJobs ();
public native void performSentboxFetch ();
public native void performSentboxIdle ();
public native void interruptSentboxIdle ();
public native void performSmtpJobs ();
public native void performSmtpIdle ();
public native void interruptSmtpIdle ();
public native void startIo ();
public native void stopIo ();
public native boolean isIoRunning ();
public native void maybeNetwork ();
public native void setConfig (String key, String value);
@@ -512,7 +512,7 @@ public class RegistrationActivity extends BaseActionBarActivity implements DcEve
// calling configure() results in
// receiving multiple DC_EVENT_CONFIGURE_PROGRESS events
DcHelper.getContext(this).captureNextError();
DcHelper.getContext(this).configure();
DcHelper.getContext(this).configure(); // TODO-ASYNC: make sure, io is stopped before and started afterwards
}
private void setConfig(@IdRes int viewId, String configTarget, boolean doTrim) {
@@ -147,7 +147,7 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen
// calling configure() results in
// receiving multiple DC_EVENT_CONFIGURE_PROGRESS events
dcContext.configure();
dcContext.configure(); // TODO-ASYNC: make sure, io is stopped before and started afterwards
}
private void progressError() {
@@ -98,7 +98,8 @@ public class AccountManager {
// create an empty DcContext object - this will be set up then, starting with
// getSelectedAccount()
ApplicationContext appContext = (ApplicationContext)context.getApplicationContext();
appContext.dcContext.stopThreads();
appContext.dcContext.notificationCenter.removeAllNotifiations();
appContext.dcContext.maybeStopIo();
appContext.dcContext.unref();
appContext.dcContext = new ApplicationDcContext(context);
}
@@ -100,27 +100,20 @@ public class ApplicationDcContext extends DcContext {
}
// /migration
try {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
imapWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "imapWakeLock");
imapWakeLock.setReferenceCounted(false); // if the idle-thread is killed for any reasons, it is better not to rely on reference counting
mvboxWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "mvboxWakeLock");
mvboxWakeLock.setReferenceCounted(false); // if the idle-thread is killed for any reasons, it is better not to rely on reference counting
sentboxWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sentboxWakeLock");
sentboxWakeLock.setReferenceCounted(false); // if the idle-thread is killed for any reasons, it is better not to rely on reference counting
smtpWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "smtpWakeLock");
smtpWakeLock.setReferenceCounted(false); // if the idle-thread is killed for any reasons, it is better not to rely on reference counting
} catch (Exception e) {
Log.e(TAG, "Cannot create wakeLocks");
}
notificationCenter = new NotificationCenter(this);
startThreads(0);
maybeStartIo();
}
public void maybeStartIo() {
if (!isIoRunning()) {
startIo();
}
}
public void maybeStopIo() {
if (isIoRunning()) {
stopIo();
}
}
public void setStockTranslations() {
@@ -310,181 +303,6 @@ public class ApplicationDcContext extends DcContext {
chat.getVisibility(), verified, chat.isSendingLocations(), chat.isMuted(), summary);
}
/***********************************************************************************************
* Working Threads
**********************************************************************************************/
private final Object threadsCritical = new Object();
private final Object incLoopsCritical= new Object();
public Thread imapThread = null;
private PowerManager.WakeLock imapWakeLock = null;
private int inboxLoops = 0;
public Thread mvboxThread = null;
private PowerManager.WakeLock mvboxWakeLock = null;
private int mvboxLoops = 0;
public Thread sentboxThread = null;
private PowerManager.WakeLock sentboxWakeLock = null;
public Thread smtpThread = null;
private PowerManager.WakeLock smtpWakeLock = null;
private int smtpLoops = 0;
public final static int INTERRUPT_IDLE = 0x01; // interrupt idle if the thread is already running
public boolean run = true;
public void startThreads(int flags) {
// TODO-ASYNC: remove this function, handle the flags differently
/*
synchronized (threadsCritical) {
if (imapThread == null || !imapThread.isAlive()) {
imapThread = new Thread(() -> {
Log.i(TAG, "###################### IMAP-Thread started. ######################");
while (run) {
imapWakeLock.acquire();
performImapJobs();
performImapFetch();
imapWakeLock.release();
synchronized (incLoopsCritical) {
inboxLoops++;
}
performImapIdle();
}
Log.i(TAG, "!!!!!!!!!!!! IMAP-Thread stopped");
}, "imapThread");
imapThread.setPriority(Thread.NORM_PRIORITY);
imapThread.start();
} else {
if ((flags & INTERRUPT_IDLE) != 0) {
interruptImapIdle();
}
}
if (mvboxThread == null || !mvboxThread.isAlive()) {
mvboxThread = new Thread(() -> {
Log.i(TAG, "###################### MVBOX-Thread started. ######################");
while (run) {
mvboxWakeLock.acquire();
performMvboxJobs();
performMvboxFetch();
mvboxWakeLock.release();
synchronized (incLoopsCritical) {
mvboxLoops++;
}
performMvboxIdle();
}
Log.i(TAG, "!!!!!!!!!!!! MVBOX-Thread stopped");
}, "mvboxThread");
mvboxThread.setPriority(Thread.NORM_PRIORITY);
mvboxThread.start();
} else {
if ((flags & INTERRUPT_IDLE) != 0) {
interruptMvboxIdle();
}
}
if (sentboxThread == null || !sentboxThread.isAlive()) {
sentboxThread = new Thread(() -> {
Log.i(TAG, "###################### SENTBOX-Thread started. ######################");
while (run) {
sentboxWakeLock.acquire();
performSentboxJobs();
performSentboxFetch();
sentboxWakeLock.release();
performSentboxIdle();
}
Log.i(TAG, "!!!!!!!!!!!! SENTBOX-Thread stopped");
}, "sentboxThread");
sentboxThread.setPriority(Thread.NORM_PRIORITY-1);
sentboxThread.start();
} else {
if ((flags & INTERRUPT_IDLE) != 0) {
interruptSentboxIdle();
}
}
if (smtpThread == null || !smtpThread.isAlive()) {
smtpThread = new Thread(() -> {
Log.i(TAG, "###################### SMTP-Thread started. ######################");
while (run) {
smtpWakeLock.acquire();
performSmtpJobs();
smtpWakeLock.release();
synchronized (incLoopsCritical) {
smtpLoops++;
}
performSmtpIdle();
}
Log.i(TAG, "!!!!!!!!!!!! SMTP-Thread stopped");
}, "smtpThread");
smtpThread.setPriority(Thread.MAX_PRIORITY);
smtpThread.start();
}
}
*/
}
public void waitForThreadsExecutedOnce() {
// TODO-ASYNC
/*
while(true) {
synchronized (incLoopsCritical) {
if(inboxLoops>0 && mvboxLoops>0 && smtpLoops>0) {
break;
}
}
Util.sleep(500);
}
*/
}
public void stopThreads() {
notificationCenter.removeAllNotifiations();
// TODO-ASYNC
/*
run = false;
synchronized (threadsCritical) {
while (true) {
// in theory, interrupting once outside the loop should be sufficient,
// but there are some corner cases, see https://github.com/deltachat/deltachat-core-rust/issues/925
Log.i(TAG, "!!!!!!!!!!!! Stopping threads ...");
if (imapThread!=null && imapThread.isAlive()) { interruptImapIdle(); }
if (mvboxThread!=null && mvboxThread.isAlive()) { interruptMvboxIdle(); }
if (sentboxThread!=null && sentboxThread.isAlive()) { interruptSentboxIdle(); }
if (smtpThread!=null && smtpThread.isAlive()) { interruptSmtpIdle(); }
Util.sleep(300);
if ( (imapThread==null || !imapThread.isAlive())
&& (mvboxThread==null || !mvboxThread.isAlive())
&& (sentboxThread==null || !sentboxThread.isAlive())
&& (smtpThread==null || !smtpThread.isAlive())) {
break;
}
}
}
Log.i(TAG, "!!!!!!!!!!!! threads stopped");
*/
}
/***********************************************************************************************
* Tools
**********************************************************************************************/
@@ -19,17 +19,14 @@ public class FetchWorker extends Worker {
@Override
public @NonNull Result doWork() {
// MAYBE TODO:
// - when no threads are running: fetch-inbox, maybe fetch-mvbox, do smtp-jobs.
// fetch-sendbox is not needed as these messages shall not be notified.
// - when threads are running: interrupt-all-idle
Log.i("DeltaChat", "-------------------- FetchWorker.doWork() started --------------------");
ApplicationDcContext dcContext = DcHelper.getContext(context);
dcContext.startThreads(ApplicationDcContext.INTERRUPT_IDLE);
dcContext.waitForThreadsExecutedOnce();
dcContext.maybeStartIo();
Log.i("DeltaChat", "-------------------- FetchWorker.doWork() done --------------------");
// TODO-ASYNC: check, that the threads get some minimal amount of time and the app is not terminated
// directly after returning
return Result.success(); // when returning, the os may terminate the app again
}
}
@@ -37,7 +37,7 @@ public class ForegroundDetector implements Application.ActivityLifecycleCallback
public void onActivityStarted(Activity activity) {
refs++;
application.dcContext.startThreads(0);
application.dcContext.maybeStartIo();
}