monitor: Prevent possible deadlock and psuedo wakeup
authorDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 7 Mar 2024 06:53:19 +0000 (15:53 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 7 Mar 2024 09:37:40 +0000 (18:37 +0900)
Change-Id: I040e53817b102cb5db6c557fec914912f450302e
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
src/monitor/monitor-command.c
src/monitor/monitor-thread.c

index b9b9a0854a42068a9866ed424fa5c5b380bfd12c..adc1567048f77102ce63c2a05d8b5354b2a2dd9e 100644 (file)
 
 static void _monitor_command_wait_done(struct monitor_command *cmd, int polling)
 {
+       mtx_lock(&cmd->lock);
        while (!cmd->done) {
                smp_rmb();
                if (polling-- <= 0) {
-                       mtx_lock(&cmd->lock);
                        cnd_wait(&cmd->signal, &cmd->lock);
-                       mtx_unlock(&cmd->lock);
-                       /* done at this time */
-                       break;
                }
        }
+       mtx_unlock(&cmd->lock);
 }
 
 void monitor_command_wait_done(struct monitor_command *cmd)
index 5c2dc8e450b0c32614ccf8149363cdaec53d4bba..663b73e3dff351c3b6c1483ff9fb2295fa42ca33 100644 (file)
@@ -28,17 +28,18 @@ static int monitor_func(void *data, void **result)
        struct monitor *monitor = data;
        struct monitor_command *cmd;
 
+       mtx_lock(&monitor->lock);
 new_command:
        if (dequeue(monitor->q, (void *)&cmd) < 0) {
                if (empty_counter++ > MONITOR_POLLING_DURATION) {
                        empty_counter = 0;
-                       mtx_lock(&monitor->lock);
                        cnd_wait(&monitor->signal, &monitor->lock);
-                       mtx_unlock(&monitor->lock);
                        goto new_command;
                }
+               mtx_unlock(&monitor->lock);
                return THREAD_RETURN_CONTINUE;
        }
+       mtx_unlock(&monitor->lock);
 
        syscommon_resman_monitor_update_resource_attrs(cmd->resource_id);