All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Elad Lahav <e2lahav@gmail.com>
Cc: Akira Yokosawa <akiyks@gmail.com>, perfbook@vger.kernel.org
Subject: Re: Section 4.2: wrong error reporting for pthread functions
Date: Tue, 17 Jul 2018 09:27:11 -0700	[thread overview]
Message-ID: <20180717162711.GN12945@linux.vnet.ibm.com> (raw)
In-Reply-To: <CAJbg=FWbVmimBhPb_C35uwSepfnuJGRJKtN=3dEL7LsggGBg1g@mail.gmail.com>

On Tue, Jul 17, 2018 at 07:05:03AM -0400, Elad Lahav wrote:
> Sure, that works for me ;-)
> 
> One more nitpick - on sub-section 4.2.7 there is an unmatched left
> parenthesis before "and":
> 
> "POSIX supplies the pthread_key_create() function to create
> a per-thread variable (and return the corresponding key, ..."

Good eyes, but SeongJae Park beat you to this one:

97bae8658482 ("toolsoftrade: Close uncompleted parentheses")

Hmmm...  That was fixed in January 2017.  I suggest upgrading to the
most recent release (November 2017), which may be found here:

https://kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html

Or, if you wish, build from the current git archive, which may be cloned
like this:

git clone git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/perfbook.git

							Thanx, Paul

> --Elad
> 
> 
> On Mon, Jul 16, 2018 at 7:51 PM, Akira Yokosawa <akiyks@gmail.com> wrote:
> > Hi Elad,
> >
> > On 2018/07/17 1:35, Elad Lahav wrote:
> >> On Mon, Jul 16, 2018 at 11:42 AM, Akira Yokosawa <akiyks@gmail.com> wrote:
> >>> First, two helpers are defined as follows:
> >>>
> >>>        #define handle_error_en(en, msg) \
> >>>                do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
> >>>
> >>>        #define handle_error(msg) \
> >>>                do { perror(msg); exit(EXIT_FAILURE); } while (0)
> >>>
> >>> Then, one of the call sites looks as follows:
> >>>
> >>>                s = pthread_create(&tinfo[tnum].thread_id, &attr,
> >>>                                   &thread_start, &tinfo[tnum]);
> >>>                if (s != 0)
> >>>                    handle_error_en(s, "pthread_create");
> >>>
> >>> If we employ this pattern, one of the hunks in your patch will look like:
> >>>
> >>> -       if (pthread_mutex_lock(pmlp) != 0) {
> >>> -               perror("lock_reader:pthread_mutex_lock");
> >>> -               exit(-1);
> >>> -       }
> >>> +       if ((en = pthread_mutex_lock(pmlp)) != 0)
> >>> +               handle_error_en(en, "lock_reader:pthread_mutex_lock");
> >>>
> >>> Thoughts?
> >>
> >> You are, of course, free to do as you want with your book, but I would
> >> advise against the proposal. Novice software developers will often
> >> copy patterns from books, which means that the examples need to be
> >> held to a higher standard. I do agree that error handling is not the
> >> point of these examples, so you shouldn't spend too much time on it,
> >> but at least at one point it should show the correct pattern. The rest
> >> of the code can just have a "// Report error and exit" comment.
> >> If you do want a helper, then a better solution would be:
> >>
> >> static inline void __attribute__((noreturn))
> >> fatal(char const * const msg, int const err)
> >> {
> >>     fprintf(stderr, "%s: %s\n", msg, strerror(err));
> >>     exit(EXIT_FAILURE);
> >> }
> >>
> >> The name 'fatal' better conveys to the reader of the code the fact
> >> that the call doesn't return. The snippet also demonstrates a more
> >> modern approach to C code (use of inline functions, const, function
> >> attributes).
> >
> > Thank you for your feedback!
> >
> > I agree with you that inline functions are better choice.
> > You might also want to update the example in the man pages? ;-)
> >
> > Would function names of "fatal_en()" and "fatal()" as defined below
> > work with you?
> >
> > static inline void __attribute__((noreturn))
> > fatal_en(char const * const msg, int const err)
> > {
> >     fprintf(stderr, "%s: %s\n", msg, strerror(err));
> >     exit(EXIT_FAILURE);
> > }
> >
> > static inline void __attribute__((noreturn))
> > fatal(char const * const msg)
> > {
> >     perror(msg);
> >     exit(EXIT_FAILURE);
> > }
> >
> >       Thanks, Akira
> >>
> >> --Elad
> >>
> >
> 


      reply	other threads:[~2018-07-17 16:58 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-14 12:59 Section 4.2: wrong error reporting for pthread functions Elad Lahav
2018-07-14 23:33 ` Paul E. McKenney
2018-07-16 15:42   ` Akira Yokosawa
2018-07-16 16:39     ` Paul E. McKenney
2018-07-17 15:43       ` Akira Yokosawa
2018-07-17 16:15         ` Paul E. McKenney
2018-07-18 12:15           ` Akira Yokosawa
2018-07-18 13:14             ` Paul E. McKenney
2018-07-18 13:42               ` Akira Yokosawa
2018-07-18 18:44                 ` Paul E. McKenney
     [not found]     ` <CAJbg=FW7vpepT_0wq+ffpQBHgKX+s1ruDb5Bs-m3FCPHKcPV3A@mail.gmail.com>
2018-07-16 23:51       ` Akira Yokosawa
2018-07-17 11:05         ` Elad Lahav
2018-07-17 16:27           ` Paul E. McKenney [this message]

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=20180717162711.GN12945@linux.vnet.ibm.com \
    --to=paulmck@linux.vnet.ibm.com \
    --cc=akiyks@gmail.com \
    --cc=e2lahav@gmail.com \
    --cc=perfbook@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.