From patchwork Thu Dec 17 17:07:51 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Khoronzhuk X-Patchwork-Id: 58612 Delivered-To: patch@linaro.org Received: by 10.112.89.199 with SMTP id bq7csp494927lbb; Thu, 17 Dec 2015 09:08:23 -0800 (PST) X-Received: by 10.140.81.71 with SMTP id e65mr69049824qgd.27.1450372087648; Thu, 17 Dec 2015 09:08:07 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id y69si12082457qka.73.2015.12.17.09.08.07; Thu, 17 Dec 2015 09:08:07 -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 Received: by lists.linaro.org (Postfix, from userid 109) id 2FA546163F; Thu, 17 Dec 2015 17:08:07 +0000 (UTC) Authentication-Results: lists.linaro.org; dkim=fail reason="verification failed; unprotected key" header.d=linaro.org header.i=@linaro.org header.b=NPqqp0ia; dkim-adsp=none (unprotected policy); dkim-atps=neutral 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 55C24615EA; Thu, 17 Dec 2015 17:08:02 +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 CB44A6161C; Thu, 17 Dec 2015 17:08:00 +0000 (UTC) Received: from mail-lb0-f174.google.com (mail-lb0-f174.google.com [209.85.217.174]) by lists.linaro.org (Postfix) with ESMTPS id B5DFF615E6 for ; Thu, 17 Dec 2015 17:07:59 +0000 (UTC) Received: by mail-lb0-f174.google.com with SMTP id yq9so29934960lbb.3 for ; Thu, 17 Dec 2015 09:07:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=DFGCuruPoS/cqjOjVd25KqmPGnQq8UKYl6/g3uO/E0U=; b=NPqqp0iaXrRM/2SQneP7B4rphMVSkXMXdFJeOvThsPAe6WkYvEvIIqxyl55DpeIuQ4 tFzYRZKZo4mCmsbUJcjgawSCvndt7OwrMl3AIk8snhaGqi1389vhI8DkPyY3DXrsrqeu xpPnVTA5rEJ8E1Frs9j5ph+0JUfYqePjIjAO8= 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=DFGCuruPoS/cqjOjVd25KqmPGnQq8UKYl6/g3uO/E0U=; b=bUfkbBh6X62lLOn1SNEfG/yB/ogCj3GJGh940FD6NNBDHBKFo2XLUQupgNFEN+zwzC MlRHzAIDyXtZaa/5Q16qdlx/evmqPDH5f9YQQpTsGLxuITwbo6pkG/Fmu2EWEOyFuj5f KmcbWCeRzOTtU7YQITYOZEjH+P9ZhoMwuz+m2obbzPadJTzq9C6t9w/ajq9CEkAodcRO 6vm/S0lB/aeBHDJk/15UEFR2vVnPWQHJVmkzGpVzbPigLalQbTBpZiNTlvMLqi9Zk4De dBYugUdncQ1lV3fcHncRKSne1bt8lMM0JOysEecd/HSCqzNZtuTxIsB3/IdVwMMajU0P qtHw== X-Gm-Message-State: ALoCoQl3t0BekpmSgUZdLbaeaF09trJ4LgXFU6HKPEshZ2kvnlDf+qwWh1IsFwF471PEXBkZy2sNYG19c1YLtYJ95JlFo/1btg== X-Received: by 10.112.143.129 with SMTP id se1mr23276179lbb.55.1450372078099; Thu, 17 Dec 2015 09:07:58 -0800 (PST) Received: from localhost.localdomain ([195.238.92.128]) by smtp.gmail.com with ESMTPSA id y15sm2090807lfd.3.2015.12.17.09.07.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 17 Dec 2015 09:07:56 -0800 (PST) From: Ivan Khoronzhuk To: lng-odp@lists.linaro.org Date: Thu, 17 Dec 2015 19:07:51 +0200 Message-Id: <1450372071-16026-1-git-send-email-ivan.khoronzhuk@linaro.org> X-Mailer: git-send-email 1.9.1 X-Topics: timers patch Subject: [lng-odp] [PATCH v2] 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 --- 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", ¶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_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, ¶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()); @@ -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; }