diff mbox

[v2] example: timer: free resources while termination

Message ID 1450372071-16026-1-git-send-email-ivan.khoronzhuk@linaro.org
State New
Headers show

Commit Message

Ivan Khoronzhuk Dec. 17, 2015, 5:07 p.m. UTC
Example should free resources in right order when terminates.
Also it should have correct error path.

Signed-off-by: Ivan Khoronzhuk <ivan.khoronzhuk@linaro.org>
---

Since v1:
Just rebase, no functional changes

 example/timer/odp_timer_test.c | 46 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 38 insertions(+), 8 deletions(-)

Comments

Mike Holmes Jan. 22, 2016, 1:08 p.m. UTC | #1
Ping this is referenced from https://projects.linaro.org/browse/ODP-269 but
it looks like it was not resloved

On 17 December 2015 at 12:07, Ivan Khoronzhuk <ivan.khoronzhuk@linaro.org>
wrote:

> Example should free resources in right order when terminates.

> Also it should have correct error path.

>

> Signed-off-by: Ivan Khoronzhuk <ivan.khoronzhuk@linaro.org>

> ---

>

> Since v1:

> Just rebase, no functional changes

>

>  example/timer/odp_timer_test.c | 46

> ++++++++++++++++++++++++++++++++++--------

>  1 file changed, 38 insertions(+), 8 deletions(-)

>

> diff --git a/example/timer/odp_timer_test.c

> b/example/timer/odp_timer_test.c

> index b7a4fd2..cb0d955 100644

> --- a/example/timer/odp_timer_test.c

> +++ b/example/timer/odp_timer_test.c

> @@ -334,18 +334,21 @@ int main(int argc, char *argv[])

>         char cpumaskstr[ODP_CPUMASK_STR_SIZE];

>         odp_shm_t shm;

>         test_globals_t  *gbls;

> +       int err = 0;

>

>         printf("\nODP timer example starts\n");

>

>         if (odp_init_global(NULL, NULL)) {

> +               err = 1;

>                 printf("ODP global init failed.\n");

> -               return -1;

> +               goto err;

>         }

>

>         /* Init this thread. */

>         if (odp_init_local(ODP_THREAD_CONTROL)) {

> +               err = 1;

>                 printf("ODP local init failed.\n");

> -               return -1;

> +               goto err_global;

>         }

>

>         printf("\n");

> @@ -363,14 +366,16 @@ int main(int argc, char *argv[])

>         shm = odp_shm_reserve("shm_test_globals", sizeof(test_globals_t),

>                               ODP_CACHE_LINE_SIZE, 0);

>         if (ODP_SHM_INVALID == shm) {

> +               err = 1;

>                 EXAMPLE_ERR("Error: shared mem reserve failed.\n");

> -               return -1;

> +               goto err_local;

>         }

>

>         gbls = odp_shm_addr(shm);

>         if (NULL == gbls) {

> +               err = 1;

>                 EXAMPLE_ERR("Error: shared mem alloc failed.\n");

> -               return -1;

> +               goto err_shm;

>         }

>         memset(gbls, 0, sizeof(test_globals_t));

>

> @@ -407,8 +412,9 @@ int main(int argc, char *argv[])

>         gbls->pool = odp_pool_create("msg_pool", &params);

>

>         if (gbls->pool == ODP_POOL_INVALID) {

> +               err = 1;

>                 EXAMPLE_ERR("Pool create failed.\n");

> -               return -1;

> +               goto err_shm;

>         }

>

>         tparams.res_ns = gbls->args.resolution_us * ODP_TIME_USEC_IN_NS;

> @@ -419,8 +425,9 @@ int main(int argc, char *argv[])

>         tparams.clk_src = ODP_CLOCK_CPU;

>         gbls->tp = odp_timer_pool_create("timer_pool", &tparams);

>         if (gbls->tp == ODP_TIMER_POOL_INVALID) {

> +               err = 1;

>                 EXAMPLE_ERR("Timer pool create failed.\n");

> -               return -1;

> +               goto err_msg_pool;

>         }

>         odp_timer_pool_start();

>

> @@ -445,8 +452,9 @@ int main(int argc, char *argv[])

>         queue = odp_queue_create("timer_queue", ODP_QUEUE_TYPE_SCHED,

> &param);

>

>         if (queue == ODP_QUEUE_INVALID) {

> +               err = 1;

>                 EXAMPLE_ERR("Timer queue create failed.\n");

> -               return -1;

> +               goto err_timer_pool;

>         }

>

>         printf("CPU freq %"PRIu64" Hz\n", odp_sys_cpu_hz());

> @@ -484,7 +492,29 @@ int main(int argc, char *argv[])

>         /* Wait for worker threads to exit */

>         odph_linux_pthread_join(thread_tbl, num_workers);

>

> -       printf("ODP timer test complete\n\n");

> +       /* free resources */

> +       if (odp_queue_destroy(queue))

> +               err = 1;

> +err_timer_pool:

> +       odp_timer_pool_destroy(gbls->tp);

> +err_msg_pool:

> +       if (odp_pool_destroy(gbls->pool))

> +               err = 1;

> +err_shm:

> +       if (odp_shm_free(shm))

> +               err = 1;

> +err_local:

> +       if (odp_term_local())

> +               err = 1;

> +err_global:

> +       if (odp_term_global())

> +               err = 1;

> +err:

> +       if (err) {

> +               printf("Err: ODP timer test failed\n\n");

> +               return -1;

> +       }

>

> +       printf("ODP timer test complete\n\n");

>         return 0;

>  }

> --

> 1.9.1

>

> _______________________________________________

> lng-odp mailing list

> lng-odp@lists.linaro.org

> https://lists.linaro.org/mailman/listinfo/lng-odp

>




-- 
Mike Holmes
Technical Manager - Linaro Networking Group
Linaro.org <http://www.linaro.org/> *│ *Open source software for ARM SoCs
Ivan Khoronzhuk Jan. 22, 2016, 2:52 p.m. UTC | #2
sent v3.

On 17.12.15 19:07, Ivan Khoronzhuk wrote:
> Example should free resources in right order when terminates.
> Also it should have correct error path.
>
> Signed-off-by: Ivan Khoronzhuk <ivan.khoronzhuk@linaro.org>
> ---
>
> Since v1:
> Just rebase, no functional changes
>
>   example/timer/odp_timer_test.c | 46 ++++++++++++++++++++++++++++++++++--------
>   1 file changed, 38 insertions(+), 8 deletions(-)
>
> diff --git a/example/timer/odp_timer_test.c b/example/timer/odp_timer_test.c
> index b7a4fd2..cb0d955 100644
> --- a/example/timer/odp_timer_test.c
> +++ b/example/timer/odp_timer_test.c
> @@ -334,18 +334,21 @@ int main(int argc, char *argv[])
>   	char cpumaskstr[ODP_CPUMASK_STR_SIZE];
>   	odp_shm_t shm;
>   	test_globals_t	*gbls;
> +	int err = 0;
>
>   	printf("\nODP timer example starts\n");
>
>   	if (odp_init_global(NULL, NULL)) {
> +		err = 1;
>   		printf("ODP global init failed.\n");
> -		return -1;
> +		goto err;
>   	}
>
>   	/* Init this thread. */
>   	if (odp_init_local(ODP_THREAD_CONTROL)) {
> +		err = 1;
>   		printf("ODP local init failed.\n");
> -		return -1;
> +		goto err_global;
>   	}
>
>   	printf("\n");
> @@ -363,14 +366,16 @@ int main(int argc, char *argv[])
>   	shm = odp_shm_reserve("shm_test_globals", sizeof(test_globals_t),
>   			      ODP_CACHE_LINE_SIZE, 0);
>   	if (ODP_SHM_INVALID == shm) {
> +		err = 1;
>   		EXAMPLE_ERR("Error: shared mem reserve failed.\n");
> -		return -1;
> +		goto err_local;
>   	}
>
>   	gbls = odp_shm_addr(shm);
>   	if (NULL == gbls) {
> +		err = 1;
>   		EXAMPLE_ERR("Error: shared mem alloc failed.\n");
> -		return -1;
> +		goto err_shm;
>   	}
>   	memset(gbls, 0, sizeof(test_globals_t));
>
> @@ -407,8 +412,9 @@ int main(int argc, char *argv[])
>   	gbls->pool = odp_pool_create("msg_pool", &params);
>
>   	if (gbls->pool == ODP_POOL_INVALID) {
> +		err = 1;
>   		EXAMPLE_ERR("Pool create failed.\n");
> -		return -1;
> +		goto err_shm;
>   	}
>
>   	tparams.res_ns = gbls->args.resolution_us * ODP_TIME_USEC_IN_NS;
> @@ -419,8 +425,9 @@ int main(int argc, char *argv[])
>   	tparams.clk_src = ODP_CLOCK_CPU;
>   	gbls->tp = odp_timer_pool_create("timer_pool", &tparams);
>   	if (gbls->tp == ODP_TIMER_POOL_INVALID) {
> +		err = 1;
>   		EXAMPLE_ERR("Timer pool create failed.\n");
> -		return -1;
> +		goto err_msg_pool;
>   	}
>   	odp_timer_pool_start();
>
> @@ -445,8 +452,9 @@ int main(int argc, char *argv[])
>   	queue = odp_queue_create("timer_queue", ODP_QUEUE_TYPE_SCHED, &param);
>
>   	if (queue == ODP_QUEUE_INVALID) {
> +		err = 1;
>   		EXAMPLE_ERR("Timer queue create failed.\n");
> -		return -1;
> +		goto err_timer_pool;
>   	}
>
>   	printf("CPU freq %"PRIu64" Hz\n", odp_sys_cpu_hz());
> @@ -484,7 +492,29 @@ int main(int argc, char *argv[])
>   	/* Wait for worker threads to exit */
>   	odph_linux_pthread_join(thread_tbl, num_workers);
>
> -	printf("ODP timer test complete\n\n");
> +	/* free resources */
> +	if (odp_queue_destroy(queue))
> +		err = 1;
> +err_timer_pool:
> +	odp_timer_pool_destroy(gbls->tp);
> +err_msg_pool:
> +	if (odp_pool_destroy(gbls->pool))
> +		err = 1;
> +err_shm:
> +	if (odp_shm_free(shm))
> +		err = 1;
> +err_local:
> +	if (odp_term_local())
> +		err = 1;
> +err_global:
> +	if (odp_term_global())
> +		err = 1;
> +err:
> +	if (err) {
> +		printf("Err: ODP timer test failed\n\n");
> +		return -1;
> +	}
>
> +	printf("ODP timer test complete\n\n");
>   	return 0;
>   }
>
diff mbox

Patch

diff --git a/example/timer/odp_timer_test.c b/example/timer/odp_timer_test.c
index b7a4fd2..cb0d955 100644
--- a/example/timer/odp_timer_test.c
+++ b/example/timer/odp_timer_test.c
@@ -334,18 +334,21 @@  int main(int argc, char *argv[])
 	char cpumaskstr[ODP_CPUMASK_STR_SIZE];
 	odp_shm_t shm;
 	test_globals_t	*gbls;
+	int err = 0;
 
 	printf("\nODP timer example starts\n");
 
 	if (odp_init_global(NULL, NULL)) {
+		err = 1;
 		printf("ODP global init failed.\n");
-		return -1;
+		goto err;
 	}
 
 	/* Init this thread. */
 	if (odp_init_local(ODP_THREAD_CONTROL)) {
+		err = 1;
 		printf("ODP local init failed.\n");
-		return -1;
+		goto err_global;
 	}
 
 	printf("\n");
@@ -363,14 +366,16 @@  int main(int argc, char *argv[])
 	shm = odp_shm_reserve("shm_test_globals", sizeof(test_globals_t),
 			      ODP_CACHE_LINE_SIZE, 0);
 	if (ODP_SHM_INVALID == shm) {
+		err = 1;
 		EXAMPLE_ERR("Error: shared mem reserve failed.\n");
-		return -1;
+		goto err_local;
 	}
 
 	gbls = odp_shm_addr(shm);
 	if (NULL == gbls) {
+		err = 1;
 		EXAMPLE_ERR("Error: shared mem alloc failed.\n");
-		return -1;
+		goto err_shm;
 	}
 	memset(gbls, 0, sizeof(test_globals_t));
 
@@ -407,8 +412,9 @@  int main(int argc, char *argv[])
 	gbls->pool = odp_pool_create("msg_pool", &params);
 
 	if (gbls->pool == ODP_POOL_INVALID) {
+		err = 1;
 		EXAMPLE_ERR("Pool create failed.\n");
-		return -1;
+		goto err_shm;
 	}
 
 	tparams.res_ns = gbls->args.resolution_us * ODP_TIME_USEC_IN_NS;
@@ -419,8 +425,9 @@  int main(int argc, char *argv[])
 	tparams.clk_src = ODP_CLOCK_CPU;
 	gbls->tp = odp_timer_pool_create("timer_pool", &tparams);
 	if (gbls->tp == ODP_TIMER_POOL_INVALID) {
+		err = 1;
 		EXAMPLE_ERR("Timer pool create failed.\n");
-		return -1;
+		goto err_msg_pool;
 	}
 	odp_timer_pool_start();
 
@@ -445,8 +452,9 @@  int main(int argc, char *argv[])
 	queue = odp_queue_create("timer_queue", ODP_QUEUE_TYPE_SCHED, &param);
 
 	if (queue == ODP_QUEUE_INVALID) {
+		err = 1;
 		EXAMPLE_ERR("Timer queue create failed.\n");
-		return -1;
+		goto err_timer_pool;
 	}
 
 	printf("CPU freq %"PRIu64" Hz\n", odp_sys_cpu_hz());
@@ -484,7 +492,29 @@  int main(int argc, char *argv[])
 	/* Wait for worker threads to exit */
 	odph_linux_pthread_join(thread_tbl, num_workers);
 
-	printf("ODP timer test complete\n\n");
+	/* free resources */
+	if (odp_queue_destroy(queue))
+		err = 1;
+err_timer_pool:
+	odp_timer_pool_destroy(gbls->tp);
+err_msg_pool:
+	if (odp_pool_destroy(gbls->pool))
+		err = 1;
+err_shm:
+	if (odp_shm_free(shm))
+		err = 1;
+err_local:
+	if (odp_term_local())
+		err = 1;
+err_global:
+	if (odp_term_global())
+		err = 1;
+err:
+	if (err) {
+		printf("Err: ODP timer test failed\n\n");
+		return -1;
+	}
 
+	printf("ODP timer test complete\n\n");
 	return 0;
 }