From patchwork Thu Nov 19 10:54:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Khoronzhuk X-Patchwork-Id: 56982 Delivered-To: patch@linaro.org Received: by 10.112.155.196 with SMTP id vy4csp3102892lbb; Thu, 19 Nov 2015 02:54:24 -0800 (PST) X-Received: by 10.140.246.69 with SMTP id r66mr6798513qhc.56.1447930464067; Thu, 19 Nov 2015 02:54:24 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id 129si6203539qhi.75.2015.11.19.02.54.23; Thu, 19 Nov 2015 02:54:24 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dkim=neutral (body hash did not verify) header.i=@linaro-org.20150623.gappssmtp.com Received: by lists.linaro.org (Postfix, from userid 109) id B04E961D75; Thu, 19 Nov 2015 10:54:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 926076197A; Thu, 19 Nov 2015 10:54:21 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id B2B9761CF5; Thu, 19 Nov 2015 10:54:19 +0000 (UTC) Received: from mail-lf0-f47.google.com (mail-lf0-f47.google.com [209.85.215.47]) by lists.linaro.org (Postfix) with ESMTPS id BD59D61847 for ; Thu, 19 Nov 2015 10:54:18 +0000 (UTC) Received: by lfaz4 with SMTP id z4so45394733lfa.0 for ; Thu, 19 Nov 2015 02:54:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=q2p0uWKu3jv50fdGgJlbiilicyac2ru2dLoos0J3+lM=; b=vzhn/4TWxjRSgaGV+36hGgZ+dfUnAif3vBLdrFeUaBcegz5XpWvYJHyZ07/iZ0hwMr 82CkVe1Wm7pnqWyt6SG1L8f/pyT5WHPYUemRTfW8DROP3nL3tc8jBxnfQXeTeOc0eYLT kueHMYw0F+tahGnqohMHf57lbglknmFv2N3/Wg4a65eI6+k88nOAbgMI8PwbvpttvhPZ uh/kaYTyRMFKhgmPAtPSzmJ0Xjap0vaARjVsNc7bpaPLp+NJVeR6ECpZD6BNy0FJHmHY YlwXNvVWderH+tZrhFPsuq1M92ekDPYRecM71OfwL4xtz4TLIX1kaSjIu4Fa7brDAA1d kGag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=q2p0uWKu3jv50fdGgJlbiilicyac2ru2dLoos0J3+lM=; b=l2Im3CPS8SXzxQkrP5SvygirKxYicfMWWMXOXG6mkKPrHV8htnZoLFRRQ9vrHuO2+S 1Syqy9qyFp6uk/pVzQ/J7LawqCGpmAFafHEq1XymfF1WJQ9O0PfYC1CFoMF2jsKFJA8J X2oFD/ZAM3P8KfSa6OQ+kK3wQK3/112srukGFyOnTjT2E0Qgi+HElZxBP1rkEZlUBgOf 857G5nHpLK57FCOLokAr53vwsxpdV9Q1bHIy0dWtxK7G9Yur7qTEx6s8gylTWqt9e/JH rDSCkXYPxVW+kwDLQ4MECuPCZrfYMQPvKZHsCzlsbWJ72JC44xw3npHpJGfCO7C/dRUP Gz1Q== X-Gm-Message-State: ALoCoQlEfpojsPiLily5iZxWFRK+AflBDJYFns14q9Qp1PN8dgMM+4EMNwf8NW7r6i24h1BbegKe X-Received: by 10.25.26.210 with SMTP id a201mr2534596lfa.58.1447930457676; Thu, 19 Nov 2015 02:54:17 -0800 (PST) Received: from localhost.localdomain ([195.238.92.128]) by smtp.gmail.com with ESMTPSA id j11sm1107382lbz.21.2015.11.19.02.54.16 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 19 Nov 2015 02:54:17 -0800 (PST) From: Ivan Khoronzhuk To: lng-odp@lists.linaro.org Date: Thu, 19 Nov 2015 12:54:08 +0200 Message-Id: <1447930448-22273-1-git-send-email-ivan.khoronzhuk@linaro.org> X-Mailer: git-send-email 1.9.1 X-Topics: timers patch Subject: [lng-odp] [PATCH] example: timer: free resources while termination X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Example should free resources in right order when terminates. Also it should have correct error path. Signed-off-by: Ivan Khoronzhuk --- 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 94619e4..2d74e4c 100644 --- a/example/timer/odp_timer_test.c +++ b/example/timer/odp_timer_test.c @@ -331,18 +331,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"); @@ -360,14 +363,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)); @@ -404,8 +409,9 @@ int main(int argc, char *argv[]) gbls->pool = odp_pool_create("msg_pool", ¶ms); 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; @@ -416,8 +422,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(); @@ -442,8 +449,9 @@ int main(int argc, char *argv[]) queue = odp_queue_create("timer_queue", ODP_QUEUE_TYPE_SCHED, ¶m); 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()); @@ -481,7 +489,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; }