public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Mark Mitchell <mark@codesourcery.com>
Cc: gcc@gcc.gnu.org, Jason Merrill <jason@redhat.com>
Subject: Re: __attribute__((cleanup(function)) versus try/finally
Date: Wed, 07 May 2003 21:21:00 -0000	[thread overview]
Message-ID: <wvladdy8nd0.fsf@prospero.boston.redhat.com> (raw)
In-Reply-To: <1052256289.2583.412.camel@doubledemon.codesourcery.com> (Mark Mitchell's message of "06 May 2003 14:24:49 -0700")

On 06 May 2003 14:24:49 -0700, Mark Mitchell <mark@codesourcery.com> wrote:

>> cancellation exception specially.  When you run out of unwind info, rather
>> than call terminate you need to hand off to the old pthread_exit.
>
> I had suggested simply having the pthread-launching routine (the first
> thing called in the new thread) be written in C++.  It can then just
> catch the exception and do whatever's required.

Sure, that works so long as you don't run out of unwind info.

>> >> > The try/finally solution cannot be implemented with this same
>> >> > performance; it will, in fact, exact a performance price, in terms of
>> >> > both code space and the speed at which cleanups are run, on all
>> >> > systems.  On systems with setjmp/longjmp exceptions, both costs will be
>> >> > very high indeed.
>> 
>> I think you're exaggerating the costs.  This is the way the C++ frontend
>> works, and you only incur the costs when you use the feature.  The design
>> philosophy of EH has always been that the speed of the normal code is what
>> matters, not the cleanups.
>
> With setjmp/longjmp, there are big costs even if you don't use the
> feature.

There are no costs unless there are cleanups.

> With the scheme I propose, you shouldn't need as many setjmps -- you only
> need one setjmp at the beginning of the function to be able to unwind.
> You never need to land in the middle of the function.

The C++ setjmp/longjump EH scheme also only requires one setjmp per
function unless there are nested catches; the EH runtime jumps there, runs
all the cleanups for the current function, and calls back into the
unwinder.

> Without setjmp/longjmp, you save mostly code space.  You also improve
> cleanup time (in that you need not transfer control to the local frame
> of the function before running the cleanup), but this is a smaller
> advantage.

Which may or may not outweigh the advantages of having the cleanups in the
function (code motion into the landing pad, inlining destructors, not
forcing the argument into the stack).

How to run simple cleanups (a call to a function which takes a pointer to
an object in the current frame) is an optimization question.  We already
have to handle more complex cleanups for Java try/finally, and for inlined
destructors in C++.  Allowing people to write them in C doesn't make our
job any harder.

>> > The observation behind this is that pthread cleanups -- unlike C++ catch
>> > clauses -- do not need to execute in the frame of the function that
>> > pushed them.  They just need to execute while the stack is still there,
>> > in case the "arg" to the function directly or indirectly references
>> > stuff on the stack.
>> 
>> No, they don't need to.  Nor do C++ destructors.  But they do anyway.  
>
> Catch-clauses and destructors are different.

I don't disagree.

> Given:
>
>   int i;
>   try { ... } catch (...) { i = 3; }
>
> the catch-clause had better execute in the frame of this function.
>
> With a destructor or (analogously) a pthread cleanup handler, the
> execution just has to take place while the frame is still active.

Well, if you really wanted to, you could turn the catch clause into a
nested function which takes an argument pointing to the frame, though you
probably wouldn't want to, as it would force all variables used in the
catch clause into the stack.

My point, however, was that in the current implementation, destructors are
run within the frame of the function, even though they don't need to be.
We could make a different choice, but again, it's just a question of
optimization strategy.  The same choices would apply to Java and C.

Basically, what I'm saying is that the question of what sorts of cleanups
to allow can be separated from the question of how to implement them.  If
we can set aside the implementation issues, it becomes just a question of
language design, and I still think that try/finally is the elegant
solution.

Jason

  reply	other threads:[~2003-05-07 21:21 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <Pine.BSF.4.55.0305061457450.57349@acrux.dbai.tuwien.ac.at>
     [not found] ` <1052245742.2583.315.camel@doubledemon.codesourcery.com>
     [not found]   ` <wvlissnc2e3.fsf@prospero.boston.redhat.com>
     [not found]     ` <1052249890.31850.338.camel@doubledemon.codesourcery.com>
2003-05-06 21:04       ` Jason Merrill
2003-05-06 21:24         ` Mark Mitchell
2003-05-07 21:21           ` Jason Merrill [this message]
2003-05-07 22:18             ` Mark Mitchell
2003-05-07 23:01               ` Jason Merrill
2003-05-08 12:05               ` Gabriel Dos Reis
2003-05-09  5:46               ` Kai Henningsen
2003-05-06 21:52         ` Anthony Green
2003-05-08 17:44         ` Mike Stump
2003-05-08 17:45           ` Jason Merrill
2003-05-08 18:40             ` Mark Mitchell
2003-05-08 19:06               ` Alexandre Oliva
2003-05-08 19:47                 ` Mark Mitchell
2003-05-08 20:19                   ` Alexandre Oliva
2003-05-08 21:18                   ` Jason Merrill
2003-05-13 21:10                     ` Mark Mitchell
2003-05-13 21:25                       ` Richard Henderson
2003-05-13 21:41                         ` Mark Mitchell
2003-05-13 22:16                           ` Richard Henderson
2003-05-13 21:31                       ` Gabriel Dos Reis
2003-05-15 17:00                       ` Jason Merrill
2003-05-15 17:23                         ` Mark Mitchell
2003-05-09 19:41                   ` Kai Henningsen
2003-05-08 19:37               ` Jason Merrill
2003-05-07  0:14   ` Richard Henderson
2003-05-07  2:32     ` Mark Mitchell
2003-05-13 21:33 Richard Kenner
2003-05-13 22:11 ` Richard Henderson
  -- strict thread matches above, loose matches on Subject: below --
2003-05-09  9:54 Ranjit Mathew
2003-05-09 10:16 ` Andrew Haley
2003-05-09 12:08   ` Fergus Henderson
2003-05-09 12:49   ` Jamie Lokier
2003-05-09  9:23 Ranjit Mathew
2003-05-09  9:31 ` Andrew Haley
2003-05-08  7:49 Ranjit Mathew
2003-05-08 21:21 ` Richard Henderson
2003-05-07 10:18 Ranjit Mathew
2003-05-07 13:54 ` Jason Merrill
2003-05-07 18:23 ` Richard Henderson
2003-05-08 18:02 ` Mike Stump
2003-05-06 19:56 Jason Merrill
2003-05-08 11:59 ` Gabriel Dos Reis
2003-05-08 15:02   ` Jason Merrill
2003-05-08 18:30 ` Mike Stump
2003-05-08 20:49   ` Richard Henderson
2003-05-08 22:29     ` Mike Stump
2003-05-13  0:07       ` Geoff Keating
2003-05-13 21:27         ` Richard Henderson
2003-05-14  1:14           ` Geoff Keating
2003-05-14  7:41             ` Richard Henderson
2003-05-14 21:11               ` Geoff Keating
2003-05-14 22:20                 ` Richard Henderson

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=wvladdy8nd0.fsf@prospero.boston.redhat.com \
    --to=jason@redhat.com \
    --cc=gcc@gcc.gnu.org \
    --cc=mark@codesourcery.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 read-only IMAP folder(s) and NNTP newsgroup(s).