From patchwork Sun Nov 9 22:16:55 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "S. Fricke" X-Patchwork-Id: 5261951 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 650B59F4D4 for ; Sun, 9 Nov 2014 22:17:21 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7E7B420142 for ; Sun, 9 Nov 2014 22:17:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7402120127 for ; Sun, 9 Nov 2014 22:17:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751510AbaKIWRL (ORCPT ); Sun, 9 Nov 2014 17:17:11 -0500 Received: from mail-wi0-f182.google.com ([209.85.212.182]:33977 "EHLO mail-wi0-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751424AbaKIWRJ (ORCPT ); Sun, 9 Nov 2014 17:17:09 -0500 Received: by mail-wi0-f182.google.com with SMTP id d1so8772838wiv.9 for ; Sun, 09 Nov 2014 14:17:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=fQbvah1v8WCQLm3QF7vkRCyGwC9JRkzcN6xjt4ATAAQ=; b=uoVLALAluX4WuXREdbu4C5K3ndj2gdbFM7JFoM62DqIb8JVYF5J5ZajobqgIdRlncU 882tUCumhDzbn3oI2C4IEcKAwxuue/n7wWzMUWSmPTfU+/CHPnxsdRHjZIADHfmc8R7Q CCORw3a/X1NfIpqwqa3OZGA0WqPNhdUnW9p8OfKp617w4z0u9DmvLj0kRKPeBcps9cwD SxSHh2hyHUraaVnj0gV3Mt3q9BYIe2Kcj8Q/tg0y8KTUEuHH6sGiD35CHx+KhZO4JaJS RZswplLTaZCslwnY3h9MtIMS1mK2t5C1wgHSSNa3f+ZdMAxTKs/LGU3HsLu9t8UDBYTs l9qQ== X-Received: by 10.195.11.6 with SMTP id ee6mr37682641wjd.95.1415571427803; Sun, 09 Nov 2014 14:17:07 -0800 (PST) Received: from zwielicht.localdomain (ipbcc17ba2.dynamic.kabel-deutschland.de. [188.193.123.162]) by mx.google.com with ESMTPSA id cz3sm20772346wjb.23.2014.11.09.14.17.06 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 09 Nov 2014 14:17:07 -0800 (PST) From: Silvio Fricke To: linux-btrfs@vger.kernel.org Cc: Silvio Fricke Subject: [PATCH 1/2] btrfs-progs: add task-utils Date: Sun, 9 Nov 2014 23:16:55 +0100 Message-Id: X-Mailer: git-send-email 2.1.2 In-Reply-To: References: In-Reply-To: References: Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Silvio Fricke --- task-util.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ task-util.h | 33 +++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 task-util.c create mode 100644 task-util.h diff --git a/task-util.c b/task-util.c new file mode 100644 index 0000000..9268df7 --- /dev/null +++ b/task-util.c @@ -0,0 +1,121 @@ + +#include +#include +#include +#include +#include + +#include "task-util.h" + +struct task_info *task_init(void *(*threadfn)(void *), int (*postfn)(void *), + void *thread_private) +{ + struct task_info *info = calloc(1, sizeof(struct task_info)); + + if (!info) + return NULL; + + info->private_data = thread_private; + info->threadfn = threadfn; + info->postfn = postfn; + + return info; +} + +int task_start(struct task_info *info) +{ + int ret; + + if (!info) + return -1; + + if (!info->threadfn) + return -1; + + ret = pthread_create(&info->id, NULL, info->threadfn, + info->private_data); + + if (ret == 0) + pthread_detach(info->id); + else + info->id = -1; + + return ret; +} + +void task_stop(struct task_info *info) +{ + if (!info) + return; + + if (info->periodic.timer_fd) + close(info->periodic.timer_fd); + + if (info->id > 0) + pthread_cancel(info->id); + + if (info->postfn) + info->postfn(info->private_data); +} + +void task_deinit(struct task_info *info) +{ + if (!info) + return; + + free(info); +} + +int task_period_start(struct task_info *info, unsigned int period_ms) +{ + unsigned int ns; + unsigned int sec; + struct itimerspec itval; + + if (!info) + return -1; + + info->periodic.timer_fd = timerfd_create(CLOCK_MONOTONIC, 0); + if (info->periodic.timer_fd == -1) + return info->periodic.timer_fd; + + info->periodic.wakeups_missed = 0; + + sec = period_ms/1000; + ns = (period_ms - (sec * 1000)) * 1000; + itval.it_interval.tv_sec = sec; + itval.it_interval.tv_nsec = ns; + itval.it_value.tv_sec = sec; + itval.it_value.tv_nsec = ns; + + return timerfd_settime(info->periodic.timer_fd, 0, &itval, NULL); +}; + +void task_period_wait(struct task_info *info) +{ + unsigned long long missed; + int ret; + + if (!info) + return; + + ret = read(info->periodic.timer_fd, &missed, sizeof (missed)); + if (ret == -1) { + perror ("read timer"); + return; + } + + if (missed > 0) + info->periodic.wakeups_missed += (missed - 1); +} + +void task_period_stop(struct task_info *info) +{ + if (!info) + return; + + if (info->periodic.timer_fd) { + timerfd_settime(info->periodic.timer_fd, 0, NULL, NULL); + close(info->periodic.timer_fd); + } +} diff --git a/task-util.h b/task-util.h new file mode 100644 index 0000000..95f7b5b --- /dev/null +++ b/task-util.h @@ -0,0 +1,33 @@ + +#ifndef __PROGRESS_ +#define __PROGRESS_ + +#include + +struct periodic_info { + int timer_fd; + unsigned long long wakeups_missed; +}; + +struct task_info { + struct periodic_info periodic; + pthread_t id; + void *private_data; + void *(*threadfn)(void *); + int (*postfn)(void *); +}; + +/* task life cycle */ +struct task_info *task_init(void *(*threadfn)(void *), int (*postfn)(void *), + void *thread_private); +int task_start(struct task_info *info); +void task_stop(struct task_info *info); +void task_deinit(struct task_info *info); + +/* periodic life cycle */ +int task_period_start(struct task_info *info, unsigned int period_ms); +void task_period_wait(struct task_info *info); +void task_period_stop(struct task_info *info); + +#endif /* __PROGRESS_ */ +