nothrow function callbacks in extern(C) code - solution

Marco Leise via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 8 10:22:13 PDT 2014


Am Thu, 19 Jun 2014 12:59:00 -0700
schrieb Walter Bright <newshound2 at digitalmars.com>:

> With nothrow and @nogc annotations, we've been motivated to add these 
> annotations to C system API functions, because obviously such functions aren't 
> going to throw D exceptions or call the D garbage collector.
> 
> But this exposed a problem - functions like C's qsort() take a pointer to a 
> callback function. The callback function, being supplied by the D programmer, 
> may throw and may call the garbage collector. By requiring the callback function 
> to be also nothrow @nogc, this is an unreasonable requirement besides breaking 
> most existing D code that uses qsort().
> 
> This problem applies as well to the Windows APIs and the Posix APIs with callbacks.

I just stumbled upon this thread now...
In general you cannot throw exceptions unless you know how the
call stack outside of your language's barrier is constructed.
What I mean is that while DMD on 64-bit Linux uses frame
pointers that druntime uses to unwind, GCC omits them. So
druntime bails out once it reaches the C part of the call
stack.

That makes for two options:
1) D callbacks passed to other languages must not throw, like
   Andrei proposes if I understood that right.
2) Druntime must adapt to the system's C compiler's ABI.
   (by the use of libunwind)

-- 
Marco



More information about the Digitalmars-d mailing list