From: Jeff King <peff@peff.net>
To: Stefan Beller <sbeller@google.com>
Cc: Junio C Hamano <gitster@pobox.com>,
"git@vger.kernel.org" <git@vger.kernel.org>,
Jonathan Nieder <jrnieder@gmail.com>,
Johannes Schindelin <johannes.schindelin@gmail.com>,
Jens Lehmann <Jens.Lehmann@web.de>,
Vitali Lovich <vlovich@gmail.com>
Subject: Re: [PATCH 01/10] strbuf: Add strbuf_read_noblock
Date: Thu, 17 Sep 2015 13:35:37 -0400 [thread overview]
Message-ID: <20150917173536.GA28987@sigill.intra.peff.net> (raw)
In-Reply-To: <CAGZ79kaZOyqwbf+BpG2oPBBt5zj3=q-abk+F-HdkZPL3GzTzsw@mail.gmail.com>
On Thu, Sep 17, 2015 at 10:26:19AM -0700, Stefan Beller wrote:
> > If we return -1, though, we have a similar annoyance. If the caller
> > notices a -1 return value and finds EAGAIN, they still may need to check
> > sb->len to see if they made forward progress and have data they should
> > be dealing with.
>
> If errno == EAGAIN, we know it is a non blocking fd, so we could encode
> the length read as (- 2 - length), such that
>
> ...-2 the length read if EAGAIN was ignored
> -1 for error, check errno!
> 0 for EOF
> +1... length if we just read it or restarted it due to EINTR.
>
> The call site should know if it is non blocking (i.e. if the <-2 case can
> happen) and handle it appropriately.
I actually wonder if callers who are _expecting_ non-blocking want to
loop in strbuf_read() at all.
strbuf_read() is really about reading to EOF, and growing the buffer to
fit all of the input. But that's not at all what you want to do with
non-blocking. There you believe for some reason that data may be
available (probably due to poll), and you want to read one chunk of it,
maybe act, and then go back to polling.
You _can_ loop on read until you hit EAGAIN, but I think in general you
shouldn't; if you get a lot of input on this fd, you'll starve all of
the other descriptors you're polling. You're better off to read a
finite amount from each descriptor, and then check again who is ready to
read.
And then the return value becomes a no-brainer, because it's just the
return value of read(). Either you got some data, you got EOF, or you
get an error, which might be EAGAIN. You never have the case where you
got some data, but then you also got EOF and EAGAIN, and the caller has
to figure out which.
So I think you really want something like:
ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
{
ssize_t cnt;
strbuf_grow(hint ? hint : 8192);
cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
if (cnt > 0)
strbuf_setlen(sb->len + cnt);
return cnt;
}
(where I'm assuming xread passes us back EAGAIN; we could also replace
it with read and loop on EINTR ourselves).
-Peff
next prev parent reply other threads:[~2015-09-17 17:35 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-17 1:38 [PATCH 00/10] fetch submodules in parallel and a preview on parallel "submodule update" Stefan Beller
2015-09-17 1:38 ` [PATCH 01/10] strbuf: Add strbuf_read_noblock Stefan Beller
2015-09-17 16:13 ` Junio C Hamano
2015-09-17 16:30 ` Jeff King
2015-09-17 16:44 ` Junio C Hamano
2015-09-17 16:51 ` Stefan Beller
2015-09-17 16:57 ` Jeff King
2015-09-17 16:58 ` Junio C Hamano
2015-09-17 17:13 ` Jeff King
2015-09-17 17:26 ` Stefan Beller
2015-09-17 17:35 ` Jeff King [this message]
2015-09-17 17:45 ` Stefan Beller
2015-09-17 17:50 ` Jeff King
2015-09-17 17:53 ` Stefan Beller
2015-09-17 17:57 ` Junio C Hamano
2015-09-17 17:54 ` Junio C Hamano
2015-09-17 18:02 ` Jeff King
2015-09-17 17:20 ` Stefan Beller
2015-09-17 1:39 ` [PATCH 02/10] run-command: factor out return value computation Stefan Beller
2015-09-17 10:33 ` Jeff King
2015-09-17 1:39 ` [PATCH 03/10] run-command: add an asynchronous parallel child processor Stefan Beller
2015-09-17 21:44 ` Junio C Hamano
2015-09-17 23:19 ` Stefan Beller
2015-09-18 1:05 ` Junio C Hamano
2015-09-18 16:36 ` Stefan Beller
2015-09-17 1:39 ` [PATCH 04/10] fetch_populated_submodules: use new parallel job processing Stefan Beller
2015-09-17 1:39 ` [PATCH 05/10] submodules: Allow parallel fetching, add tests and documentation Stefan Beller
2015-09-17 1:39 ` [PATCH 06/10] git submodule update: Redirect any output to stderr Stefan Beller
2015-09-17 20:31 ` Eric Sunshine
2015-09-17 20:38 ` Stefan Beller
2015-09-17 1:39 ` [PATCH 07/10] git submodule update: pass --prefix only with a non empty prefix Stefan Beller
2015-09-17 20:33 ` Eric Sunshine
2015-09-17 1:39 ` [PATCH 08/10] git submodule update: cmd_update_recursive Stefan Beller
2015-09-17 1:39 ` [PATCH 09/10] " Stefan Beller
2015-09-17 20:37 ` Eric Sunshine
2015-09-17 1:39 ` [PATCH 10/10] git submodule update: cmd_update_fetch Stefan Beller
2015-09-17 17:06 ` [PATCH 00/10] fetch submodules in parallel and a preview on parallel "submodule update" Jacob Keller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150917173536.GA28987@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=Jens.Lehmann@web.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=johannes.schindelin@gmail.com \
--cc=jrnieder@gmail.com \
--cc=sbeller@google.com \
--cc=vlovich@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).