File: expect_abort.cpp

package info (click to toggle)
halide 21.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 55,420 kB
  • sloc: cpp: 289,327; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (39 lines) | stat: -rw-r--r-- 1,112 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <Halide.h>

#include <csignal>
#include <cstdlib>
#include <exception>
#include <iostream>

std::atomic<bool> suppress_abort{true};

auto handler = ([]() {
#ifdef HALIDE_WITH_EXCEPTIONS
    std::set_terminate([]() {  //
        try {
            if (const auto e = std::current_exception()) {
                std::rethrow_exception(e);
            }
        } catch (const Halide::InternalError &e) {
            std::cerr << e.what() << "\n"
                      << std::flush;
            suppress_abort = false;
            std::abort();  // We should never EXPECT an internal error
        } catch (const Halide::Error &e) {
            std::cerr << e.what() << "\n"
                      << std::flush;
        } catch (const std::exception &e) {
            std::cerr << e.what() << "\n"
                      << std::flush;
        } catch (...) {}
        std::_Exit(EXIT_FAILURE);
    });
#endif

    // If exceptions are disabled, we just hope for the best.
    return std::signal(SIGABRT, [](int) {
        if (suppress_abort) {
            std::_Exit(EXIT_FAILURE);
        }
    });
})();