device_camera.c \
smack.c \
malloc_debug.c \
- target.c
+ target.c \
+ thread.c
DAEMON_OBJS = $(patsubst %.c,%.o, $(DAEMON_SRCS))
ecore_main_fd_handler_del(target->handler);
+ target_wait(target);
target_dtor(target);
// all target client are closed
cnt = target_cnt_sub_and_fetch();
#include "target.h"
+#include "thread.h"
#include "daemon.h" // for manager (it is need delete)
#include "smack.h"
#include "debug.h"
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;
void target_dtor(struct target *t)
{
- t->recv_thread = -1;
t->allocmem = 0;
t->initial_log = 0;
close(t->socket);
t->socket = -1;
+ thread_dtor(t->thread);
target_free(t);
}
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);
}
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();
}
#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;
};
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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_ */
pass = 1;
}
- target->recv_thread = -1;
return NULL;
}