Skip to content

Commit

Permalink
Edited mutex lock of dlt_housekeeper_running_mutex.
Browse files Browse the repository at this point in the history
    The original place of mutex lock was wrong because it didn't protect dlt_housekeeper_running
    and there was no mutex lock when sending signal.
  • Loading branch information
ranoongs committed Jul 28, 2024
1 parent 358ab08 commit ca34020
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/lib/dlt_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -3827,13 +3827,18 @@ void dlt_user_housekeeperthread_function(void *ptr)

pthread_cleanup_push(dlt_user_cleanup_handler, NULL);


pthread_mutex_lock(&dlt_housekeeper_running_mutex);

// signal dlt thread to be running
*dlt_housekeeper_running = true;
signal_status = pthread_cond_signal(&dlt_housekeeper_running_cond);
if (signal_status != 0) {
dlt_log(LOG_CRIT, "Housekeeper thread failed to signal running state\n");
}

pthread_mutex_unlock(&dlt_housekeeper_running_mutex);

while (in_loop) {
/* Check for new messages from DLT daemon */
if (!dlt_user.disable_injection_msg)
Expand Down Expand Up @@ -5052,6 +5057,10 @@ int dlt_start_threads()
* (spurious wakeup)
* To protect against this, a while loop with a timeout is added
* */

// pthread_cond_timedwait has to be called on a locked mutex
pthread_mutex_lock(&dlt_housekeeper_running_mutex);

while (!dlt_housekeeper_running
&& now.tv_sec <= time_to_wait.tv_sec) {

Expand All @@ -5064,20 +5073,19 @@ int dlt_start_threads()
single_wait.tv_sec = now.tv_sec;
single_wait.tv_nsec = now.tv_nsec + 500000000;

// pthread_cond_timedwait has to be called on a locked mutex
pthread_mutex_lock(&dlt_housekeeper_running_mutex);
signal_status = pthread_cond_timedwait(
&dlt_housekeeper_running_cond,
&dlt_housekeeper_running_mutex,
&single_wait);
pthread_mutex_unlock(&dlt_housekeeper_running_mutex);

/* otherwise it might be a spurious wakeup, try again until the time is over */
if (signal_status == 0) {
break;
}
}

pthread_mutex_unlock(&dlt_housekeeper_running_mutex);

if (signal_status != 0 && !dlt_housekeeper_running) {
dlt_log(LOG_CRIT, "Failed to wait for house keeper thread!\n");
dlt_stop_threads();
Expand Down

0 comments on commit ca34020

Please sign in to comment.