Navigation Menu

Skip to content

Commit

Permalink
[worker] Undo partially executed mode switch on failure
Browse files Browse the repository at this point in the history
If mode switch fails / is abandoned due to request from main thread,
undo any changes already made towards the current target.

Also do not shortcut synchronous sleeps during the cleanup
activity.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Sep 5, 2018
1 parent 566b7a6 commit 3b873f0
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/usb_moded-worker.c
Expand Up @@ -86,7 +86,19 @@ static pthread_t worker_thread_id = 0;

static pthread_mutex_t worker_mutex = PTHREAD_MUTEX_INITIALIZER;

static int worker_bailout = 0;
/** Flag for: Main thread has changed target mode worker should apply
*
* Worker should bailout from synchronous activities related to
* ongoing activation of a usb mode.
*/
static volatile bool worker_bailout_requested = false;

/** Flag for: Worker thread is cleaning up after abandoning mode switch
*
* Asynchronous activities on mode cleanup should be executed without
* bailing out.
*/
static volatile bool worker_bailout_handled = false;

#define WORKER_LOCKED_ENTER do {\
if( pthread_mutex_lock(&worker_mutex) != 0 ) { \
Expand Down Expand Up @@ -116,7 +128,9 @@ bool
worker_bailing_out(void)
{
// ref: see common_msleep_()
return worker_thread_p() && worker_bailout > 0;
return (worker_thread_p() &&
worker_bailout_requested &&
!worker_bailout_handled);
}

/* ------------------------------------------------------------------------- *
Expand Down Expand Up @@ -534,9 +548,21 @@ worker_switch_to_mode(const char *mode)
log_warning("Matching mode %s was not found.", mode);

FAILED:
worker_bailout_handled = true;

/* Undo any changes we might have might have already done */
if( worker_get_usb_mode_data() ) {
log_debug("Cleaning up failed mode switch");

if( worker_mode_is_mtp_mode(mode) )
worker_stop_mtpd();

modesetting_leave_dynamic_mode();
worker_set_usb_mode_data(NULL);
}

override = MODE_CHARGING;
log_warning("mode setting failed, try %s", override);
worker_set_usb_mode_data(NULL);

CHARGE:
if( worker_switch_to_charging() )
Expand Down Expand Up @@ -642,8 +668,8 @@ static void *worker_thread_cb(void *aptr)
continue;

if( cnt > 0 ) {
--worker_bailout;
log_debug("worker_bailout -> %d", worker_bailout);
worker_bailout_requested = false;
worker_bailout_handled = false;
worker_execute();
}

Expand Down Expand Up @@ -839,8 +865,7 @@ worker_quit(void)
void
worker_wakeup(void)
{
++worker_bailout;
log_debug("worker_bailout -> %d", worker_bailout);
worker_bailout_requested = true;

uint64_t cnt = 1;
if( write(worker_req_evfd, &cnt, sizeof cnt) == -1 ) {
Expand Down

0 comments on commit 3b873f0

Please sign in to comment.