mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-27 19:52:17 -05:00
Handle thread interruptions better and fix some race conditions.
This commit is contained in:
parent
24b178773f
commit
8eeebc593a
@ -30,6 +30,7 @@ public class SleepService extends CoreService
|
|||||||
SleepDatum sleepDatum = new SleepDatum();
|
SleepDatum sleepDatum = new SleepDatum();
|
||||||
CountDownLatch latch = new CountDownLatch(1);
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
sleepDatum.latch = latch;
|
sleepDatum.latch = latch;
|
||||||
|
sleepDatum.reacquireLatch = new CountDownLatch(1);
|
||||||
sleepData.put(id, sleepDatum);
|
sleepData.put(id, sleepDatum);
|
||||||
|
|
||||||
Intent i = new Intent();
|
Intent i = new Intent();
|
||||||
@ -52,32 +53,48 @@ public class SleepService extends CoreService
|
|||||||
{
|
{
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
Log.d(K9.LOG_TAG, "SleepService latch timed out for id = " + id + ", thread " + Thread.currentThread().getName());
|
Log.d(K9.LOG_TAG, "SleepService latch timed out for id = " + id + ", thread " + Thread.currentThread().getName());
|
||||||
// don't call endSleep here or remove the sleepDatum here, instead of the following block.
|
|
||||||
// We might not get the wakeLock before
|
|
||||||
// falling asleep again, so we have to get the wakeLock *first* The alarmed version will
|
|
||||||
// already be running in a WakeLock due to the nature of AlarmManager
|
|
||||||
sleepDatum = sleepData.get(id);
|
|
||||||
if (sleepDatum != null)
|
|
||||||
{
|
|
||||||
reacquireWakeLock(sleepDatum);
|
|
||||||
// OK, we have the wakeLock, now we can remove the sleepDatum
|
|
||||||
sleepData.remove(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (InterruptedException ie)
|
catch (InterruptedException ie)
|
||||||
{
|
{
|
||||||
Log.e(K9.LOG_TAG, "SleepService Interrupted", ie);
|
Log.e(K9.LOG_TAG, "SleepService Interrupted while awaiting latch", ie);
|
||||||
}
|
}
|
||||||
|
SleepDatum releaseDatum = sleepData.remove(id);
|
||||||
|
if (releaseDatum == null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (K9.DEBUG)
|
||||||
|
Log.d(K9.LOG_TAG, "SleepService waiting for reacquireLatch for id = " + id + ", thread " + Thread.currentThread().getName());
|
||||||
|
if (sleepDatum.reacquireLatch.await(5000, TimeUnit.MILLISECONDS) == false)
|
||||||
|
{
|
||||||
|
Log.w(K9.LOG_TAG, "SleepService reacquireLatch timed out for id = " + id + ", thread " + Thread.currentThread().getName());
|
||||||
|
}
|
||||||
|
else if (K9.DEBUG)
|
||||||
|
Log.d(K9.LOG_TAG, "SleepService reacquireLatch finished for id = " + id + ", thread " + Thread.currentThread().getName());
|
||||||
|
}
|
||||||
|
catch (InterruptedException ie)
|
||||||
|
{
|
||||||
|
Log.e(K9.LOG_TAG, "SleepService Interrupted while awaiting reacquireLatch", ie);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
reacquireWakeLock(releaseDatum);
|
||||||
|
}
|
||||||
|
|
||||||
long endTime = System.currentTimeMillis();
|
long endTime = System.currentTimeMillis();
|
||||||
long actualSleep = endTime - startTime;
|
long actualSleep = endTime - startTime;
|
||||||
if (K9.DEBUG)
|
|
||||||
Log.d(K9.LOG_TAG, "SleepService requested sleep time was " + sleepTime + ", actual was " + actualSleep);
|
|
||||||
if (actualSleep < sleepTime)
|
if (actualSleep < sleepTime)
|
||||||
{
|
{
|
||||||
Log.w(K9.LOG_TAG, "SleepService sleep time too short: requested was " + sleepTime + ", actual was " + actualSleep);
|
Log.w(K9.LOG_TAG, "SleepService sleep time too short: requested was " + sleepTime + ", actual was " + actualSleep);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (K9.DEBUG)
|
||||||
|
Log.d(K9.LOG_TAG, "SleepService requested sleep time was " + sleepTime + ", actual was " + actualSleep);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void endSleep(Integer id)
|
private static void endSleep(Integer id)
|
||||||
@ -99,6 +116,7 @@ public class SleepService extends CoreService
|
|||||||
latch.countDown();
|
latch.countDown();
|
||||||
}
|
}
|
||||||
reacquireWakeLock(sleepDatum);
|
reacquireWakeLock(sleepDatum);
|
||||||
|
sleepDatum.reacquireLatch.countDown();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -139,6 +157,7 @@ public class SleepService extends CoreService
|
|||||||
CountDownLatch latch;
|
CountDownLatch latch;
|
||||||
TracingWakeLock wakeLock;
|
TracingWakeLock wakeLock;
|
||||||
long timeout;
|
long timeout;
|
||||||
|
CountDownLatch reacquireLatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user