diff mbox

[2/2] linux-generic: init: implement basic odp_term_local()

Message ID 1418901579-29209-2-git-send-email-taras.kondratiuk@linaro.org
State Accepted
Commit a75f7a0b76bda95bd7492904702aba7729df5af0
Headers show

Commit Message

Taras Kondratiuk Dec. 18, 2014, 11:19 a.m. UTC
Signed-off-by: Taras Kondratiuk <taras.kondratiuk@linaro.org>
---
 platform/linux-generic/include/odp_internal.h |  1 +
 platform/linux-generic/odp_init.c             |  3 +--
 platform/linux-generic/odp_linux.c            | 10 ++++++++--
 platform/linux-generic/odp_thread.c           | 14 ++++++++++++--
 4 files changed, 22 insertions(+), 6 deletions(-)
diff mbox

Patch

diff --git a/platform/linux-generic/include/odp_internal.h b/platform/linux-generic/include/odp_internal.h
index ee2ab1a..549d406 100644
--- a/platform/linux-generic/include/odp_internal.h
+++ b/platform/linux-generic/include/odp_internal.h
@@ -23,6 +23,7 @@  int odp_system_info_init(void);
 
 int odp_thread_init_global(void);
 int odp_thread_init_local(void);
+int odp_thread_term_local(void);
 
 int odp_shm_init_global(void);
 int odp_shm_init_local(void);
diff --git a/platform/linux-generic/odp_init.c b/platform/linux-generic/odp_init.c
index c661231..7210430 100644
--- a/platform/linux-generic/odp_init.c
+++ b/platform/linux-generic/odp_init.c
@@ -90,6 +90,5 @@  int odp_init_local(void)
 
 int odp_term_local(void)
 {
-	ODP_UNIMPLEMENTED();
-	return 0;
+	return (odp_thread_term_local() > 0) ? 1 : 0;
 }
diff --git a/platform/linux-generic/odp_linux.c b/platform/linux-generic/odp_linux.c
index 95761a9..229d24e 100644
--- a/platform/linux-generic/odp_linux.c
+++ b/platform/linux-generic/odp_linux.c
@@ -43,9 +43,15 @@  static void *odp_run_start_routine(void *arg)
 		return NULL;
 	}
 
-	void *ret = start_args->start_routine(start_args->arg);
+	void *ret_ptr = start_args->start_routine(start_args->arg);
 	_odp_flush_caches();
-	return ret;
+	int ret = odp_term_local();
+	if (ret < 0)
+		ODP_ERR("Local term failed\n");
+	else if (ret == 0 && odp_term_global())
+		ODP_ERR("Global term failed\n");
+
+	return ret_ptr;
 }
 
 
diff --git a/platform/linux-generic/odp_thread.c b/platform/linux-generic/odp_thread.c
index faa12a2..01c8147 100644
--- a/platform/linux-generic/odp_thread.c
+++ b/platform/linux-generic/odp_thread.c
@@ -32,6 +32,7 @@  typedef struct {
 typedef struct {
 	thread_state_t   thr[ODP_CONFIG_MAX_THREADS];
 	odp_atomic_u32_t num;
+	odp_atomic_u32_t next_id;
 
 } thread_globals_t;
 
@@ -58,6 +59,8 @@  int odp_thread_init_global(void)
 		return -1;
 
 	memset(thread_globals, 0, sizeof(thread_globals_t));
+	odp_atomic_init_u32(&thread_globals->next_id, 0);
+	odp_atomic_init_u32(&thread_globals->num, 0);
 	return 0;
 }
 
@@ -67,12 +70,13 @@  static int thread_id(void)
 	uint32_t id;
 	int cpu;
 
-	id = odp_atomic_fetch_inc_u32(&thread_globals->num);
+	id = odp_atomic_fetch_inc_u32(&thread_globals->next_id);
 
 	if (id >= ODP_CONFIG_MAX_THREADS) {
 		ODP_ERR("Too many threads\n");
 		return -1;
 	}
+	odp_atomic_inc_u32(&thread_globals->num);
 
 	cpu = sched_getcpu();
 
@@ -87,7 +91,6 @@  static int thread_id(void)
 	return id;
 }
 
-
 int odp_thread_init_local(void)
 {
 	int id;
@@ -101,6 +104,13 @@  int odp_thread_init_local(void)
 	return 0;
 }
 
+int odp_thread_term_local(void)
+{
+	uint32_t num;
+	num = odp_atomic_fetch_dec_u32(&thread_globals->num);
+	ODP_ASSERT(num > 0, "Number of threads should be > 0");
+	return num - 1; /* return a number of threads left */
+}
 
 int odp_thread_id(void)
 {