This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [basic-improvements] try/finally support for c/c++ - more tests


On 06-Nov-2002, Geoff Keating <geoffk@geoffk.org> wrote:
> Richard Henderson <rth@redhat.com> writes:
> 
> > On Wed, Nov 06, 2002 at 03:32:20PM -0800, Mark Mitchell wrote:
> > > I do not think we should add exception support of any kind to GNU C; use
> > > C++ if you want exceptions.
> > 
> > See elsewhere about resistance compiling libc with a c++ compiler.
> 
> It wouldn't work, anyway.  User programs can also use
> pthread_cleanup_push/pthread_cleanup_pop, in C, so whatever mechanism
> is used for that must be accessible to C.

How about the following?
This uses the C++ compiler's exception support, and is accessible from C.
It makes use of the GNU C nested function extension.
The main drawback that I can see is that it is not as efficient as possible.

/* pthread.h */

void __pthread_try_finally(void (*)(void), void (*)(void *), void *, int);

#define pthread_cleanup_push(__routine,__arg) \
  { \
     void (*__cleanup_routine)(void *) = __routine; \
     void *__cleanup_arg = __arg; \
     void __body(void) {

#define pthread_cleanup_pop(__call_cleanup) \
     } \
     __pthread_try_finally(__body, __cleanup_routine, __cleanup_arg, \
                           __call_cleanup); \
  }

/* pthread_try_finally.cpp */

extern "C" {

void 
__pthread_try_finally(
     void (*body)(void),
     void (*cleanup_routine)(void *),
     void (*cleanup_arg)(void),
     int call_cleanup)
{
  class S {
    void (*cleanup_routine)(void *);
    void (*cleanup_arg)(void);
    int call_cleanup;
  public:
    S(void (*cleanup_routine)(void *),
      void (*cleanup_arg)(void),
      int call_cleanup)
    {
      this->cleanup_routine = cleanup_routine;
      this->cleanup_arg = cleanup_arg;
      this->call_cleanup = call_cleanup;
    }
    ~S()
    {
      if (call_cleanup)
        (*cleanup_routine)(cleanup_arg);
    }
  } s(cleanup_routine, cleanup_arg, call_cleanup);

  (*body)();
}

}



-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]