Skip to content

Commit

Permalink
[inactivity] Block suspend during inactivity broadcast. Fixes JB#31927
Browse files Browse the repository at this point in the history
While mce wakes up from suspend to change activity state to inactive,
it might be possible that the device falls back to suspend before
other processes have had a chance to receive and process the D-Bus
signal.

Block suspend briefly when inactivity signal is broadcast.
  • Loading branch information
spiiroin committed Sep 2, 2015
1 parent 5d8ac67 commit 28afd35
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .depend
Expand Up @@ -630,6 +630,7 @@ modules/inactivity.o:\
modules/inactivity.c\
builtin-gconf.h\
datapipe.h\
libwakelock.h\
mce-dbus.h\
mce-hbtimer.h\
mce-log.h\
Expand All @@ -639,6 +640,7 @@ modules/inactivity.pic.o:\
modules/inactivity.c\
builtin-gconf.h\
datapipe.h\
libwakelock.h\
mce-dbus.h\
mce-hbtimer.h\
mce-log.h\
Expand Down
1 change: 1 addition & 0 deletions mce.c
Expand Up @@ -116,6 +116,7 @@ static void mce_cleanup_wakelocks(void)
wakelock_unlock("mce_lpm_off");
wakelock_unlock("mce_tklock_notify");
wakelock_unlock("mce_hbtimer_dispatch");
wakelock_unlock("mce_inactivity_notify");
}
#endif // ENABLE_WAKELOCKS

Expand Down
98 changes: 98 additions & 0 deletions modules/inactivity.c
Expand Up @@ -26,6 +26,10 @@
#include "../mce-dbus.h"
#include "../mce-hbtimer.h"

#ifdef ENABLE_WAKELOCKS
# include "../libwakelock.h"
#endif

#include <string.h>

#include <mce/dbus-names.h>
Expand Down Expand Up @@ -57,6 +61,9 @@ G_MODULE_EXPORT module_info_struct module_info = {
/** Maximum amount of monitored activity callbacks */
#define ACTIVITY_CB_MAX_MONITORED 16

/** Duration of suspend blocking after sending inactivity signals */
#define MIA_KEEPALIVE_DURATION_MS 5000

/* ========================================================================= *
* PROTOTYPES
* ========================================================================= */
Expand Down Expand Up @@ -112,6 +119,17 @@ static void mia_datapipe_check_initial_state (void);
static void mia_datapipe_init(void);
static void mia_datapipe_quit(void);

/* ------------------------------------------------------------------------- *
* SUSPEND_BLOCK
* ------------------------------------------------------------------------- */

#ifdef ENABLE_WAKELOCKS
static void mia_keepalive_rethink (void);
static gboolean mia_keepalive_cb (gpointer aptr);
static void mia_keepalive_start (void);
static void mia_keepalive_stop (void);
#endif

/* ------------------------------------------------------------------------- *
* DBUS_HANDLERS
* ------------------------------------------------------------------------- */
Expand Down Expand Up @@ -672,6 +690,75 @@ static void mia_datapipe_quit(void)
datapipe_bindings_quit(&mia_datapipe_bindings);
}

/* ========================================================================= *
* SUSPEND_BLOCK
* ========================================================================= */

#ifdef ENABLE_WAKELOCKS
/** Timer ID for ending suspend blocking */
static guint mia_keepalive_id = 0;

/** Evaluate need for suspend blocking
*/
static void mia_keepalive_rethink(void)
{
static bool have_lock = false;

bool need_lock = (mia_keepalive_id != 0);

if( have_lock == need_lock )
goto EXIT;

mce_log(LL_DEBUG, "inactivity notify wakelock: %s",
need_lock ? "OBTAIN" : "RELEASE");

if( (have_lock = need_lock) )
wakelock_lock("mce_inactivity_notify", -1);
else
wakelock_unlock("mce_inactivity_notify");

EXIT:
return;
}

/** Timer callback for ending suspend blocking
*/
static gboolean mia_keepalive_cb(gpointer aptr)
{
(void)aptr;

mia_keepalive_id = 0;
mia_keepalive_rethink();

return FALSE;
}

/** Start/restart temporary suspend blocking
*/
static void mia_keepalive_start(void)
{
if( mia_keepalive_id ) {
g_source_remove(mia_keepalive_id),
mia_keepalive_id = 0;
}

mia_keepalive_id = g_timeout_add(MIA_KEEPALIVE_DURATION_MS,
mia_keepalive_cb, 0);
mia_keepalive_rethink();
}

/** Cancel suspend blocking
*/
static void mia_keepalive_stop(void)
{
if( mia_keepalive_id ) {
g_source_remove(mia_keepalive_id),
mia_keepalive_id = 0;
}
mia_keepalive_rethink();
}
#endif /* ENABLE_WAKELOCKS */

/* ========================================================================= *
* DBUS_HANDLERS
* ========================================================================= */
Expand Down Expand Up @@ -817,6 +904,13 @@ static gboolean mia_dbus_send_inactivity_state(DBusMessage *const method_call)
}
else {
/* Broadcast state change */

#ifdef ENABLE_WAKELOCKS
/* Block suspend for a while to give other processes
* a chance to get and process the signal. */
mia_keepalive_start();
#endif

msg = dbus_new_signal(MCE_SIGNAL_PATH, MCE_SIGNAL_IF,
MCE_INACTIVITY_SIG);
}
Expand Down Expand Up @@ -1134,6 +1228,10 @@ void g_module_unload(GModule *module)
/* Do not leave any timers active */
mia_timer_quit();

#ifdef ENABLE_WAKELOCKS
mia_keepalive_stop();
#endif

/* Flush activity actions */
mia_activity_action_remove_all();
return;
Expand Down

0 comments on commit 28afd35

Please sign in to comment.