From patchwork Tue Feb 12 22:26:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 10808853 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 29FC517E0 for ; Tue, 12 Feb 2019 22:27:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1A1DB2C720 for ; Tue, 12 Feb 2019 22:27:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0EEED2C726; Tue, 12 Feb 2019 22:27:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BAEB02C720 for ; Tue, 12 Feb 2019 22:27:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727993AbfBLW1C (ORCPT ); Tue, 12 Feb 2019 17:27:02 -0500 Received: from mail-ed1-f65.google.com ([209.85.208.65]:39568 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726550AbfBLW1C (ORCPT ); Tue, 12 Feb 2019 17:27:02 -0500 Received: by mail-ed1-f65.google.com with SMTP id b14so254450edt.6 for ; Tue, 12 Feb 2019 14:27:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EjZdmoCpzjmQXbTddE7TrXbCYC7lkCVKOuquQIFRK80=; b=Uob11nBOxdxx46x6u3hkK0eba38GtluGGgBMU1zCBjQ7TxLjQoBU0cIcXPGFhvqVKF FG42hAWY+ZBTY5dXW0XWNsGlcD2fH6NC2T08E7y0gp3ge7GBhDXKz5wm3ZT0Wb0728RH r/Yz8axWYwHoQM2sSf0s//9DuBqsW0lKwWAWo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EjZdmoCpzjmQXbTddE7TrXbCYC7lkCVKOuquQIFRK80=; b=LGr8uOjLEDXDHGqvYDmE3sSYriH8mxHiv1+dcvzLhn0e368ZtAW9VHJwRQnYo1L6kp uxgGz56uSZTL//oNLLsYADq6+91vKDOk4nf/Ih697xmvXmnoWIvNM7EKKnBKaoDhHzXf YrIHTEyrbCoJlMUrBKzNwXqlSpSYn7aQp0vjdtZ7ofwUfbl4Mhfn4XFsZ8O3u5imamSx 533am6x2yrLoytjfeJMb3PCJDhgTny1cvgnCOpZ04t0oDYy6RZQJoNH5NQmyxlKRyarX flJ8B8lEcj2i3x4GkqGggasgQd41iOp4JMKOGpoG2BwYxKkXguvFDDU8nqwWOesGCOnT YKRg== X-Gm-Message-State: AHQUAuYV9VFDK61lWvethys2MfKvCAo6wSojc0QUbaUVPdOkyIIIO6LF UzHtN2rYgeestTXWiDWUXCjIqsUx8fVcCesx X-Google-Smtp-Source: AHgI3IaCUCpshi3U6oX1kob0hSHoHY0XIkWm9v3MX7KgyO1zdK/xQqtThC5KL1/qBkgqUjNuskIVxw== X-Received: by 2002:a17:906:bb16:: with SMTP id jz22mr4262745ejb.106.1550010420477; Tue, 12 Feb 2019 14:27:00 -0800 (PST) Received: from prevas-ravi.waoo.dk (ip-5-186-119-174.cgn.fibianet.dk. [5.186.119.174]) by smtp.gmail.com with ESMTPSA id ay20sm3349582ejb.32.2019.02.12.14.26.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Feb 2019 14:26:59 -0800 (PST) From: Rasmus Villemoes To: git@vger.kernel.org Cc: Rasmus Villemoes Subject: [RFC PATCH 1/5] builtin/grep.c: change todo_* variables to unsigned Date: Tue, 12 Feb 2019 23:26:50 +0100 Message-Id: <20190212222654.7432-2-rv@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190212222654.7432-1-rv@rasmusvillemoes.dk> References: <20190212222654.7432-1-rv@rasmusvillemoes.dk> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In preparation for subsequent patches that make todo_* free-running instead of reducing them mod TODO_SIZE, change their type to unsigned to avoid undefined behaviour in case anybody ever greps more than 2 billion files. Signed-off-by: Rasmus Villemoes --- builtin/grep.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/builtin/grep.c b/builtin/grep.c index 580fd38f41..6c1e90d43b 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -58,9 +58,9 @@ struct work_item { */ #define TODO_SIZE 128 static struct work_item todo[TODO_SIZE]; -static int todo_start; -static int todo_end; -static int todo_done; +static unsigned int todo_start; +static unsigned int todo_end; +static unsigned int todo_done; /* Has all work items been added? */ static int all_work_added; @@ -132,7 +132,7 @@ static struct work_item *get_work(void) static void work_done(struct work_item *w) { - int old_done; + unsigned int old_done; grep_lock(); w->done = 1; From patchwork Tue Feb 12 22:26:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 10808855 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2695217E0 for ; Tue, 12 Feb 2019 22:27:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 11B0F2C720 for ; Tue, 12 Feb 2019 22:27:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 05FC72C722; Tue, 12 Feb 2019 22:27:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B32EB2C726 for ; Tue, 12 Feb 2019 22:27:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729472AbfBLW1F (ORCPT ); Tue, 12 Feb 2019 17:27:05 -0500 Received: from mail-ed1-f66.google.com ([209.85.208.66]:39570 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726550AbfBLW1E (ORCPT ); Tue, 12 Feb 2019 17:27:04 -0500 Received: by mail-ed1-f66.google.com with SMTP id b14so254496edt.6 for ; Tue, 12 Feb 2019 14:27:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ePXM7FhkCgO3lJqYKMyCH6eac5RW533wyq2qZRgbEeI=; b=i/Q7kYHCyTupqr5fwpNOGsB6BQdCSqh3UtYrF+TZ+rWNjy6mBMHzopwU1WVBEEmUSv adMgZ2RuZS0yWJfdmBZISo74RbxOZTCvqRCXwSunkQyjzxW8bnJCg4qpevDBIrdwahHU nb0oFTyHZHOk3s5MGJCrvbrstc2oimE12qcoY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ePXM7FhkCgO3lJqYKMyCH6eac5RW533wyq2qZRgbEeI=; b=FzpFKCv/BqdeQ59yc2gBhqPRSwg8N4Hlq7/oB2ek0uH7W34LnjQDfKumJ8rHANSstH dz4WKjosTO4j8wdkYdNOYVfOEViAZ6qGOZWkHo6U05/ka9lTvzYg6f8KZscRLiHDRcau lEt4vbukS2MZb5/dTYgHn5lm4PYx6SlMW6+rIuMNCPRvySPpcD1UpasEwAfRv9OU8p/q IYDGYbJ8QUX9Qtgcnr29kO7sXI++uw0/z6t5xUDyu9loKeTLC1EhshbES1Fl2hTud9Zn CcPsuB46UA6Z+GcM7gmYAyBsK8FFFYBE4lNzLcAgia+CpJyJt2xmifhO4pFOLlx4OFsY X1JA== X-Gm-Message-State: AHQUAubbTStvmOHeTmUKuKg3kPqxZrviKb5u78+xCJaGodSfKQiG+pl7 NBu2DITTKKNSzXaBH0riJNedUMbnfMMFSe// X-Google-Smtp-Source: AHgI3IZoBAXlDRAYDTJauVP6l9wuCjbhHoGGuh2TYL2BpAtNvrhCJvtPZZrBjVD5nZlfAQwE/CtkoA== X-Received: by 2002:a17:906:7e57:: with SMTP id z23mr4190314ejr.222.1550010421942; Tue, 12 Feb 2019 14:27:01 -0800 (PST) Received: from prevas-ravi.waoo.dk (ip-5-186-119-174.cgn.fibianet.dk. [5.186.119.174]) by smtp.gmail.com with ESMTPSA id ay20sm3349582ejb.32.2019.02.12.14.27.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Feb 2019 14:27:01 -0800 (PST) From: Rasmus Villemoes To: git@vger.kernel.org Cc: Rasmus Villemoes Subject: [RFC PATCH 2/5] builtin/grep.c: refactor loop in work_done() slightly Date: Tue, 12 Feb 2019 23:26:51 +0100 Message-Id: <20190212222654.7432-3-rv@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190212222654.7432-1-rv@rasmusvillemoes.dk> References: <20190212222654.7432-1-rv@rasmusvillemoes.dk> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP As preparation for changing all accesses to the todo array to use a helper function, do the .done check in the loop body. Signed-off-by: Rasmus Villemoes --- builtin/grep.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/builtin/grep.c b/builtin/grep.c index 6c1e90d43b..211ae54222 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -137,9 +137,11 @@ static void work_done(struct work_item *w) grep_lock(); w->done = 1; old_done = todo_done; - for(; todo[todo_done].done && todo_done != todo_start; + for(; todo_done != todo_start; todo_done = (todo_done+1) % ARRAY_SIZE(todo)) { w = &todo[todo_done]; + if (!w->done) + break; if (w->out.len) { const char *p = w->out.buf; size_t len = w->out.len; From patchwork Tue Feb 12 22:26:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 10808861 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EB6CA922 for ; Tue, 12 Feb 2019 22:27:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D9C952C720 for ; Tue, 12 Feb 2019 22:27:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CE5482C725; Tue, 12 Feb 2019 22:27:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 795CC2C720 for ; Tue, 12 Feb 2019 22:27:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730882AbfBLW1S (ORCPT ); Tue, 12 Feb 2019 17:27:18 -0500 Received: from mail-ed1-f66.google.com ([209.85.208.66]:39974 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728320AbfBLW1F (ORCPT ); Tue, 12 Feb 2019 17:27:05 -0500 Received: by mail-ed1-f66.google.com with SMTP id 10so248840eds.7 for ; Tue, 12 Feb 2019 14:27:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9b6WJxQkqzKuMVIeWdt6VXP2PktlQs/OwfmEifFc4rg=; b=cDH4XQQ+5BeVv446UVHOXrQvyxmy7nRbJc3J4WP6dIXcxpge47SSBAg0mGPbQnXQCC R2Cdrd93LjIhHm03zCtx82BCc8Ek+kExXqpH9E66ImtVj/mitY5+/jwCo7BZskuxVY0l c3Ej58/xHl9WdZ8J7AhAQnDhBVDRmriHJSyOY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9b6WJxQkqzKuMVIeWdt6VXP2PktlQs/OwfmEifFc4rg=; b=q8QqGK8Dg4OjVdnpt2311n0T5KqyFKNcQWWZguTIW+l+fJi8mg2VAtmeMcG5s+1OZH h2dAMjwXQD4SlpW1PvykLxmrgqfTqdNIwXSr+rvSU4l+3F9ytA2wyGszCXadpf0FDjxQ YEPQDqLEXFUQv7PtB96n4iRDgSr+I75cE2yeZZ+nNorxBipbmoWuiz5bYO8zOghi2JP7 VcWPTaZyXuIzD5iL4rrq6fel13zNSzwuBvDjPRIexob0Cf4QLgpESs+gtkoMpPH42HTV WJ52Hcnzt7sWHk46hPdz3sVQ9zk8YbZnXT6FEcRqAVnGZYhUmro7Ay1Ubd0Vhbmc0uVe ajYQ== X-Gm-Message-State: AHQUAuYXn86oTIJ+zSilr/Jqos1enJSkAQjvWK8NgL6dTQFQtpHIQ3yO r/lrNij5fX+B3NS6OU9q+Fp8tjL6YoR2lCUp X-Google-Smtp-Source: AHgI3IZKpNR6y70/VKimazbEBu1SQ2IHZ0twafw2l/6TfE/I+uhordeqOns8YYzjsuRzo7z+yh37mQ== X-Received: by 2002:a17:906:595a:: with SMTP id g26mr4332621ejr.88.1550010423051; Tue, 12 Feb 2019 14:27:03 -0800 (PST) Received: from prevas-ravi.waoo.dk (ip-5-186-119-174.cgn.fibianet.dk. [5.186.119.174]) by smtp.gmail.com with ESMTPSA id ay20sm3349582ejb.32.2019.02.12.14.27.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Feb 2019 14:27:02 -0800 (PST) From: Rasmus Villemoes To: git@vger.kernel.org Cc: Rasmus Villemoes Subject: [RFC PATCH 3/5] builtin/grep.c: add shorthand for &todo[todo_end] in add_work() Date: Tue, 12 Feb 2019 23:26:52 +0100 Message-Id: <20190212222654.7432-4-rv@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190212222654.7432-1-rv@rasmusvillemoes.dk> References: <20190212222654.7432-1-rv@rasmusvillemoes.dk> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Rasmus Villemoes --- builtin/grep.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/builtin/grep.c b/builtin/grep.c index 211ae54222..92b9e6198d 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -93,18 +93,20 @@ static int skip_first_line; static void add_work(struct grep_opt *opt, const struct grep_source *gs) { + struct work_item *w; + grep_lock(); while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) { pthread_cond_wait(&cond_write, &grep_mutex); } - todo[todo_end].source = *gs; + w = &todo[todo_end]; + w->source = *gs; if (opt->binary != GREP_BINARY_TEXT) - grep_source_load_driver(&todo[todo_end].source, - opt->repo->index); - todo[todo_end].done = 0; - strbuf_reset(&todo[todo_end].out); + grep_source_load_driver(&w->source, opt->repo->index); + w->done = 0; + strbuf_reset(&w->out); todo_end = (todo_end + 1) % ARRAY_SIZE(todo); pthread_cond_signal(&cond_add); From patchwork Tue Feb 12 22:26:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 10808857 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C3737746 for ; Tue, 12 Feb 2019 22:27:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B27DD2C720 for ; Tue, 12 Feb 2019 22:27:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A6D6D2C725; Tue, 12 Feb 2019 22:27:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 571002C720 for ; Tue, 12 Feb 2019 22:27:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730727AbfBLW1K (ORCPT ); Tue, 12 Feb 2019 17:27:10 -0500 Received: from mail-ed1-f65.google.com ([209.85.208.65]:36499 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729578AbfBLW1G (ORCPT ); Tue, 12 Feb 2019 17:27:06 -0500 Received: by mail-ed1-f65.google.com with SMTP id o59so268601edb.3 for ; Tue, 12 Feb 2019 14:27:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=b+8rZxNPvstgKoHkzsXlQba4uwrXMEW7QZ/8bE1dygQ=; b=XZ6+TwFmWmVCFAnkQt/FFso3B2OnZmdhVcYCfhSd8+s03oYzW5fQDbLlSz5MljcII9 rhXPNTy9OFwZzAIvuEIe4unQJN5trtlGW4HrvOX2qZr9reG1mdtkRk/lRaALsTdJrKyl D0GzLv9X25LGXZpXPyj+OV6yly8eR6tfqHGvI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=b+8rZxNPvstgKoHkzsXlQba4uwrXMEW7QZ/8bE1dygQ=; b=JEAxAjdu/K4ffkVyevshy1zRKpqdffrbZml8UspKrhza5bQF6RSuxr4M3VWU/6RZik BHORv6u9lLngIBCAP+eR0JRzC2JFUgcs2RWoTMd8T/IiaoimDlHjtqhUMxMDr4Wtgz8P xq9WV1j5KW3fOpcr+6D6pk5OASg6ArpXRTDF3fsV5EDY8aU4yGuX55L1yVti0Gl0ZK7P 78NI7JddeFNGeZoQ5IDpf/vkE85X9I5KUNYlt8MMm4DVEg6ezvcn195HxEs/fy/IKM5T CSgSUOKVYG5G9TNhrWdARtCYtbgm6jJ6mmtuvFcgfbG8IZIoi2uKe6Y+BCHPAwfidvOb 2thg== X-Gm-Message-State: AHQUAubxDjlDcpw14Sy+3Jd/YHQrNTAY1uwIpcP/hjv+YgwtoYT28BXt Glz6JWR6UCUEHIOqZBg63n3tGUpJFKIWC2Ip X-Google-Smtp-Source: AHgI3IZH+0fv0lSlQVOj06SOZyv4+I5iOZlGL07a/+ZLFZYgFE6nPTZVCHmH3zLXwsjYYxVdrphiFw== X-Received: by 2002:a17:906:1343:: with SMTP id x3mr4145021ejb.76.1550010424303; Tue, 12 Feb 2019 14:27:04 -0800 (PST) Received: from prevas-ravi.waoo.dk (ip-5-186-119-174.cgn.fibianet.dk. [5.186.119.174]) by smtp.gmail.com with ESMTPSA id ay20sm3349582ejb.32.2019.02.12.14.27.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Feb 2019 14:27:03 -0800 (PST) From: Rasmus Villemoes To: git@vger.kernel.org Cc: Rasmus Villemoes Subject: [RFC PATCH 4/5] builtin/grep.c: add todo_item helper Date: Tue, 12 Feb 2019 23:26:53 +0100 Message-Id: <20190212222654.7432-5-rv@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190212222654.7432-1-rv@rasmusvillemoes.dk> References: <20190212222654.7432-1-rv@rasmusvillemoes.dk> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Use a helper for indexing into the todo array with any of the todo_* variables, in preparation for not keeping those reduced mod TODO_SIZE. Signed-off-by: Rasmus Villemoes --- builtin/grep.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/builtin/grep.c b/builtin/grep.c index 92b9e6198d..35ed79b0dd 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -62,6 +62,11 @@ static unsigned int todo_start; static unsigned int todo_end; static unsigned int todo_done; +static inline struct work_item *todo_item(unsigned int idx) +{ + return &todo[idx % ARRAY_SIZE(todo)]; +} + /* Has all work items been added? */ static int all_work_added; @@ -101,7 +106,7 @@ static void add_work(struct grep_opt *opt, const struct grep_source *gs) pthread_cond_wait(&cond_write, &grep_mutex); } - w = &todo[todo_end]; + w = todo_item(todo_end); w->source = *gs; if (opt->binary != GREP_BINARY_TEXT) grep_source_load_driver(&w->source, opt->repo->index); @@ -125,7 +130,7 @@ static struct work_item *get_work(void) if (todo_start == todo_end && all_work_added) { ret = NULL; } else { - ret = &todo[todo_start]; + ret = todo_item(todo_start); todo_start = (todo_start + 1) % ARRAY_SIZE(todo); } grep_unlock(); @@ -141,7 +146,7 @@ static void work_done(struct work_item *w) old_done = todo_done; for(; todo_done != todo_start; todo_done = (todo_done+1) % ARRAY_SIZE(todo)) { - w = &todo[todo_done]; + w = todo_item(todo_done); if (!w->done) break; if (w->out.len) { From patchwork Tue Feb 12 22:26:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 10808859 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 09EAE746 for ; Tue, 12 Feb 2019 22:27:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ED7C92C720 for ; Tue, 12 Feb 2019 22:27:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E217F2C725; Tue, 12 Feb 2019 22:27:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 837042C720 for ; Tue, 12 Feb 2019 22:27:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730405AbfBLW1J (ORCPT ); Tue, 12 Feb 2019 17:27:09 -0500 Received: from mail-ed1-f67.google.com ([209.85.208.67]:46356 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730386AbfBLW1H (ORCPT ); Tue, 12 Feb 2019 17:27:07 -0500 Received: by mail-ed1-f67.google.com with SMTP id f2so220792edy.13 for ; Tue, 12 Feb 2019 14:27:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=b5gylcfuDa0p7UhjHRVil7Fph9lV2JyHGLEhPqvNZpk=; b=cK4pa25tt8BB7UTxltfozCVb8ior3AIOZIEXXHtfD/NiKQwYM72E4zUP2ukCdaqRH6 QsZu8XbaxyIYcFW0yVIjCaM20/xtua4mBaCx7hsBVQlglDONuJQ6cLzfOa54ADWkSL+c gQO60TPhCcHPwynxonKH7P0JE16upQ0hHKWEI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=b5gylcfuDa0p7UhjHRVil7Fph9lV2JyHGLEhPqvNZpk=; b=lph0miC3IpiFtBU/k2gmvSz7LHz79qmdifRNAciwK7ON6xP+7xqOMOpnD4ctHKb9xD rpHLy7VWF06aVKHvD6onkO42lPaSWQPc3AmQ7noFqZoZgNIhMi7hDYD28oZawuoL3XwS wAmFwZpn+fKOK9Z2VLYCtAdBkODR3a067iSdXIek3FyknELe7TUSCCxcI6T+PtsbjBg8 P+VB0bkTaM6vBZmwPeXsrheyq/9FxLt9hvpFaWaXG/1gkJ0VmKWhrOoswMPBiCCAyMvv yGDit74vVl9XlB76AheR/dKA/+anDJ2T/GyCAUrF5tfTtJHgIUqjrw3yq/JnsS283L3D 96rA== X-Gm-Message-State: AHQUAubnStaPPP18soTsqNOOP0yrdHjntCtYBqK3vqA2eMC+eg4SkE5L MFcYPsdBEIuOZeKr+upDcIKZKUZpMEFVx41E X-Google-Smtp-Source: AHgI3IZNwy4iql+o3o9N+xmtL1YetEmAg08sSSVmdsD4OLrPB+UgOCTk9uOrVXKkYdueokEJM1WIqg== X-Received: by 2002:a50:cc84:: with SMTP id q4mr5007348edi.12.1550010425461; Tue, 12 Feb 2019 14:27:05 -0800 (PST) Received: from prevas-ravi.waoo.dk (ip-5-186-119-174.cgn.fibianet.dk. [5.186.119.174]) by smtp.gmail.com with ESMTPSA id ay20sm3349582ejb.32.2019.02.12.14.27.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 12 Feb 2019 14:27:04 -0800 (PST) From: Rasmus Villemoes To: git@vger.kernel.org Cc: Rasmus Villemoes Subject: [RFC PATCH 5/5] builtin/grep.c: fix fence-post error in add_work() Date: Tue, 12 Feb 2019 23:26:54 +0100 Message-Id: <20190212222654.7432-6-rv@rasmusvillemoes.dk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190212222654.7432-1-rv@rasmusvillemoes.dk> References: <20190212222654.7432-1-rv@rasmusvillemoes.dk> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP We're only using 127 of the slots in todo[], which can easily be seen by adding this hack --- a/builtin/grep.c +++ b/builtin/grep.c @@ -93,6 +93,8 @@ static int skip_first_line; static void add_work(struct grep_opt *opt, const struct grep_source *gs) { + static int count; + grep_lock(); while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) { @@ -108,6 +110,7 @@ static void add_work(struct grep_opt *opt, const struct grep_source *gs) todo_end = (todo_end + 1) % ARRAY_SIZE(todo); pthread_cond_signal(&cond_add); + fprintf(stderr, "added work item %3d\n", ++count); grep_unlock(); } @@ -173,6 +176,7 @@ static void *run(void *arg) int hit = 0; struct grep_opt *opt = arg; + sleep(2); while (1) { struct work_item *w = get_work(); if (!w) Of course, just removing the +1 after todo_end would be instant deadlock, since nothing would ever change todo_end or todo_done from 0. The problem boils down to the fact that arithmetic mod 128 cannot capture the 129 possible values of end-done (which is (end-start)+(start-done), i.e. the total number of items waiting to be picked up or that have been picked up by a worker). To fix this, don't keep the todo_* variables reduced mod 128, and only do that when using them as indices into todo[]. Then we can rewrite the condition in add_work() to the proper one: Wait until todo_end is not a full round ahead of todo_done. Signed-off-by: Rasmus Villemoes --- builtin/grep.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/builtin/grep.c b/builtin/grep.c index 35ed79b0dd..ce158cabbb 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -102,7 +102,7 @@ static void add_work(struct grep_opt *opt, const struct grep_source *gs) grep_lock(); - while ((todo_end+1) % ARRAY_SIZE(todo) == todo_done) { + while (todo_end - todo_done == ARRAY_SIZE(todo)) { pthread_cond_wait(&cond_write, &grep_mutex); } @@ -112,7 +112,7 @@ static void add_work(struct grep_opt *opt, const struct grep_source *gs) grep_source_load_driver(&w->source, opt->repo->index); w->done = 0; strbuf_reset(&w->out); - todo_end = (todo_end + 1) % ARRAY_SIZE(todo); + todo_end += 1; pthread_cond_signal(&cond_add); grep_unlock(); @@ -131,7 +131,7 @@ static struct work_item *get_work(void) ret = NULL; } else { ret = todo_item(todo_start); - todo_start = (todo_start + 1) % ARRAY_SIZE(todo); + todo_start += 1; } grep_unlock(); return ret; @@ -144,8 +144,7 @@ static void work_done(struct work_item *w) grep_lock(); w->done = 1; old_done = todo_done; - for(; todo_done != todo_start; - todo_done = (todo_done+1) % ARRAY_SIZE(todo)) { + for(; todo_done != todo_start; todo_done += 1) { w = todo_item(todo_done); if (!w->done) break;