z3 / src / util / scoped_ctrl_c.cpp

/*++
Copyright (c) 2011 Microsoft Corporation

Module Name:

    scoped_ctrl_c.cpp

Abstract:

    Scoped control-c handler.

Author:

    Leonardo de Moura (leonardo) 2011-04-27.

Revision History:

--*/
#include<signal.h>
#include<iostream>
#include"scoped_ctrl_c.h"

scoped_ctrl_c * scoped_ctrl_c::g_obj = 0;

void scoped_ctrl_c::on_ctrl_c(int) {
    if (g_obj->m_first) {
        g_obj->m_cancel_eh();
        if (g_obj->m_once) {
            g_obj->m_first = false;
            signal(SIGINT, on_ctrl_c); // re-install the handler
        }
    }
    else {
        signal(SIGINT, g_obj->m_old_handler); 
        raise(SIGINT);
    }
}

scoped_ctrl_c::scoped_ctrl_c(event_handler & eh, bool once, bool enabled):
    m_cancel_eh(eh), 
    m_first(true),
    m_once(once),
    m_enabled(enabled),
    m_old_scoped_ctrl_c(g_obj) {
    if (m_enabled) {
        g_obj = this;
        m_old_handler = signal(SIGINT, on_ctrl_c); 
    }
}

scoped_ctrl_c::~scoped_ctrl_c() { 
    if (m_enabled) {
        g_obj = m_old_scoped_ctrl_c;
        if (m_old_handler != SIG_ERR) {
            signal(SIGINT, m_old_handler);
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.