manager: fix long timeout if application was closed before stop msg. 32/154932/2
authorVitaliy Cherepanov <v.cherepanov@samsung.com>
Wed, 11 Oct 2017 08:12:21 +0000 (11:12 +0300)
committerVitaliy Cherepanov <v.cherepanov@samsung.com>
Wed, 11 Oct 2017 13:21:35 +0000 (16:21 +0300)
Add condition happens flag

Change-Id: I08fad39d65ee384bc57638ac9cd32c1f4e108c95
Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
daemon/target.c
daemon/target.h
daemon/threads.c

index 2c5dbe8..c90e0ad 100644 (file)
@@ -53,6 +53,8 @@ struct target *target_ctor(void)
                t->initial_log = 0;
                t->event_fd_released = 0;
                t->allocmem = 0;
+
+               t->target_condition_happens = 0;
                pthread_mutex_init(&t->target_mtx, NULL);
                pthread_cond_init(&t->target_cv, NULL);
 
@@ -210,6 +212,15 @@ static void target_free(struct target *t)
        target_array_unlock();
 }
 
+/* TODO move all condition work to separate file. */
+void target_thread_cond(struct target *target)
+{
+       pthread_mutex_lock(&target->target_mtx);
+       target->target_condition_happens = true;
+       pthread_cond_broadcast(&target->target_cv);
+       pthread_mutex_unlock(&target->target_mtx);
+}
+
 static void wait_for_acknowledge_or_timeout(struct target *t, int timeout)
 {
        struct timespec timeToWait;
@@ -221,7 +232,9 @@ static void wait_for_acknowledge_or_timeout(struct target *t, int timeout)
        timeToWait.tv_nsec = now.tv_usec;
 
        pthread_mutex_lock(&t->target_mtx);
-       pthread_cond_timedwait(&t->target_cv, &t->target_mtx, &timeToWait);
+       if (!t->target_condition_happens)
+               pthread_cond_timedwait(&t->target_cv, &t->target_mtx, &timeToWait);
+
        pthread_mutex_unlock(&t->target_mtx);
 }
 
index e6c812e..772bbb3 100644 (file)
@@ -60,6 +60,8 @@ struct target {
 
        pthread_mutex_t target_mtx;
        pthread_cond_t target_cv;
+       int target_condition_happens;
+
        struct thread *thread;
 };
 
@@ -96,6 +98,7 @@ int target_send_terminate_to_all(void);
 void target_wait_all(void);
 void target_stop_all(void);
 uint64_t target_get_total_alloc(pid_t pid);
+void target_thread_cond(struct target *target);
 
 
 #endif /* _TARGER_H_ */
index d07a9bb..074166d 100644 (file)
@@ -369,7 +369,7 @@ static void* recvThread(void* data)
 
                if (msg->type == APP_MSG_STOP) {
                        LOGI("APP_MSG_STOP arrived\n");
-                       pthread_cond_broadcast(&target->target_cv);
+                       target_thread_cond(target);
                        continue;
                }
                msg->data[msg->length] = '\0';
@@ -483,6 +483,7 @@ static void* recvThread(void* data)
                pass = 1;
        }
 
+       target_thread_cond(target);
        free(msg);
        LOGI("thread finished %u:%u\n", target->pid, target->ppid);
        return NULL;