[FIX] sync start/stop target communication thread 96/27896/2
authorVyacheslav Cherkashin <v.cherkashin@samsung.com>
Mon, 22 Sep 2014 13:29:21 +0000 (17:29 +0400)
committerVyacheslav Cherkashin <v.cherkashin@samsung.com>
Tue, 23 Sep 2014 07:46:30 +0000 (11:46 +0400)
Change-Id: I91f619ce162096d5dc8453ac8fbf8d35555b1075
Signed-off-by: Vyacheslav Cherkashin <v.cherkashin@samsung.com>
daemon/Makefile
daemon/daemon.c
daemon/target.c
daemon/target.h
daemon/thread.c [new file with mode: 0644]
daemon/thread.h [new file with mode: 0644]
daemon/threads.c

index 9e84161..16a8273 100644 (file)
@@ -47,7 +47,8 @@ DAEMON_SRCS =                 \
        device_camera.c         \
        smack.c                                 \
        malloc_debug.c \
-       target.c
+       target.c \
+       thread.c
 
 DAEMON_OBJS = $(patsubst %.c,%.o, $(DAEMON_SRCS))
 
index 2f2bfc4..c287fa4 100644 (file)
@@ -562,6 +562,7 @@ static int target_event_stop_handler(struct target *target)
 
        ecore_main_fd_handler_del(target->handler);
 
+       target_wait(target);
        target_dtor(target);
        // all target client are closed
        cnt = target_cnt_sub_and_fetch();
index cbdedc8..c3fb0c0 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "target.h"
 
+#include "thread.h"
 #include "daemon.h"    // for manager (it is need delete)
 #include "smack.h"
 #include "debug.h"
@@ -48,9 +49,14 @@ struct target *target_ctor(void)
                t->pid = UNKNOWN_PID;
                t->socket = UNKNOWN_FD;
                t->event_fd = UNKNOWN_FD;
-               t->recv_thread = 0;
                t->initial_log = 0;
                t->allocmem = 0;
+
+               t->thread = thread_ctor();
+               if (t->thread == NULL) {
+                       target_free(t);
+                       t = NULL;
+               }
        }
 
        return t;
@@ -58,7 +64,6 @@ struct target *target_ctor(void)
 
 void target_dtor(struct target *t)
 {
-       t->recv_thread = -1;
        t->allocmem = 0;
        t->initial_log = 0;
 
@@ -70,6 +75,7 @@ void target_dtor(struct target *t)
                close(t->socket);
        t->socket = -1;
 
+       thread_dtor(t->thread);
        target_free(t);
 }
 
@@ -103,12 +109,12 @@ int target_recv_msg(struct target *t, struct msg_target_t *msg)
 
 int target_start(struct target *t, void *(*start_routine) (void *))
 {
-       return pthread_create(&t->recv_thread, NULL, start_routine, (void *)t);
+       return thread_start(t->thread, start_routine, (void *)t);
 }
 
 int target_wait(struct target *t)
 {
-       return pthread_join(t->recv_thread, NULL);
+       return thread_wait(t->thread);
 }
 
 
@@ -167,8 +173,12 @@ static struct target *target_malloc(void)
 
 static void target_free(struct target *t)
 {
+       int id = t - target_array;
+
        target_array_lock();
-       target_use[t - target_array] = 0;
+       if (target_use[id] == 0)
+               LOGE("double free t=%p\n", t);
+       target_use[id] = 0;
        target_array_unlock();
 }
 
index e30ea25..ce0a899 100644 (file)
 #define UNKNOWN_FD             (-1)
 
 
+struct thread;
+
+
 struct target {
        enum app_type_t app_type;       /* calculated when connecting */
        int64_t allocmem;               /* written only by recv thread */
        pid_t pid;                      /* written only by recv thread */
        pid_t ppid;                     /* written only by recv thread */
        int socket;                     /* written only by main thread */
-       pthread_t recv_thread;          /* written only by main thread */
        int event_fd;                   /* for thread communication
                                         * (from recv thread to main thread) */
        int initial_log;                /* written only by main thread */
        Ecore_Fd_Handler *handler;      /* calculated when connecting */
+
+       struct thread *thread;
 };
 
 
diff --git a/daemon/thread.c b/daemon/thread.c
new file mode 100644 (file)
index 0000000..f6b8f27
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ *  DA manager
+ *
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *
+ * Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - Samsung RnD Institute Russia
+ *
+ */
+
+
+#include <pthread.h>
+#include <errno.h>
+
+
+#include "debug.h"
+
+
+struct thread {
+       pthread_t thread;
+       pthread_mutex_t mutex;
+       unsigned run_flag:1;
+};
+
+
+struct thread *thread_ctor(void)
+{
+       struct thread *t;
+
+       t = malloc(sizeof(*t));
+       if (t) {
+               t->run_flag = 0;
+               pthread_mutex_init(&t->mutex, NULL);
+       }
+
+       return t;
+}
+
+void thread_dtor(struct thread *t)
+{
+       if (t->run_flag == 1)
+               LOGE("hanging thread: t=%p\n", t);
+
+       free(t);
+}
+
+int thread_start(struct thread *t, void *(*func) (void *), void *data)
+{
+       int ret = EBUSY;
+
+       pthread_mutex_lock(&t->mutex);
+       if (t->run_flag == 0) {
+               ret = pthread_create(&t->thread, NULL, func, data);
+               if (ret == 0)
+                       t->run_flag = 1;
+       }
+       pthread_mutex_unlock(&t->mutex);
+
+       return ret;
+}
+
+int thread_wait(struct thread *t)
+{
+       int ret = ESRCH;
+
+       pthread_mutex_lock(&t->mutex);
+       if (t->run_flag == 1) {
+               ret = pthread_join(t->thread, NULL);
+               t->run_flag = 0;
+       }
+       pthread_mutex_unlock(&t->mutex);
+
+       return ret;
+}
diff --git a/daemon/thread.h b/daemon/thread.h
new file mode 100644 (file)
index 0000000..529a2ee
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ *  DA manager
+ *
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *
+ * Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - Samsung RnD Institute Russia
+ *
+ */
+
+
+#ifndef _THREAD_H_
+#define _THREAD_H_
+
+
+struct thread;
+
+
+struct thread *thread_ctor(void);
+void thread_dtor(struct thread *t);
+
+
+int thread_start(struct thread *t, void *(*func) (void *), void *data);
+int thread_wait(struct thread *t);
+
+
+#endif /* _THREAD_H_ */
index d75b263..02c5634 100644 (file)
@@ -181,7 +181,6 @@ static void* recvThread(void* data)
                pass = 1;
        }
 
-       target->recv_thread = -1;
        return NULL;
 }