3 ms·
Am I weird that I've been programming in C for decades and have never used longjmp? For the longest time I thought it was some legacy function that was only us
by jandrese 2y ago
Am I weird that I've been programming in C for decades and have never used longjmp? For the longest time I thought it was some legacy function that was only used on ancient segmented memory systems, but it instead turns out to be a complete program flow destroyer that I can't see a good use for.
I have seen some libraries that do error handling via the user passing a callback for a longjmp, but even that seems ill advised. Fortunately it is always optional.
- jeff-davis 2y agoLongjmp is used by Postgres for transaction aborts. With C, there's not really a better option available.
- eqvinox 2y agoThat's not from within signal handlers, though. (i.e. it relates to this specific longjmp discussion but not the root post re. exceptions on other threads.)
- anarazel 2y agoIt unfortunately is used from within signal handlers, albeit only in specific cases (SIGFPE). There used to several more, but we luckily largely cleaned that up over the last few years.
- eqvinox 2y agoMeh. Well. Good to hear on the cleanup. Didn't know it used to be different :/ Re. SIGFPE, to be fair, it feels a bit like the "asynchronous vs. synchronous abort¹" thing on CPUs; synchronous aborts are reasonably doable while on asynchronous aborts you're pretty much left with torching things down far and wide. (SIGFPE should hopefully be synchronous; it's in fact closely connected to sync/async CPU aborts...) [¹ frequently also called exceptions, depending on the CPU architecture, but this post already uses "exception" for the language level concept]
- anarazel 2y ago> Meh. Well. Good to hear on the cleanup. Didn't know it used to be different :/ If you want to be scared: Until not too long ago postgres' supervisor process would start some types of subprocesses from within a signal handler... Not entirely surprisingly, that found bugs in various debugging tools (IIRC at least valgrind, rr, one of the sanitizer libs). > Re. SIGFPE, to be fair, it feels a bit like the "asynchronous vs. synchronous abort¹" thing on CPUs; synchronous aborts are reasonably doable while on asynchronous aborts you're pretty much left with torching things down far and wide. Agreed, I think it's quite reasonable to use signals + longjmp() for the FP error case. In fact, I think we should do so more widely - we loose a fair bit of performance due to all kinds of floating point error checking that we could set up to instead signal.
- chuckadams 2y agoLambdaMOO which dates from the early 90’s uses setjmp/longjmp to implement structured exceptions in C, which it throws on task timeouts (though not for anything else afaik). Even has try/throw/catch macros.