diff --git a/src/shogun/base/init.cpp b/src/shogun/base/init.cpp index 104636e925d..c7697aa2aeb 100644 --- a/src/shogun/base/init.cpp +++ b/src/shogun/base/init.cpp @@ -12,17 +12,19 @@ #include #include +#include +#include +#include +#include +#include #include #include #include -#include -#include -#include -#include -#include +#include #include #include +#include #ifdef TRACE_MEMORY_ALLOCS #include shogun::CMap* sg_mallocs=NULL; @@ -39,6 +41,7 @@ namespace shogun Version* sg_version=NULL; CMath* sg_math=NULL; CRandom* sg_rand=NULL; + CSignal* sg_signal = NULL; std::unique_ptr sg_linalg(nullptr); /// function called to print normal messages @@ -71,6 +74,8 @@ namespace shogun sg_rand = new shogun::CRandom(); if (!sg_linalg) sg_linalg = std::unique_ptr(new shogun::SGLinalg()); + if (!sg_signal) + sg_signal = new shogun::CSignal(); #ifdef TRACE_MEMORY_ALLOCS if (!sg_mallocs) @@ -83,12 +88,16 @@ namespace shogun SG_REF(sg_version); SG_REF(sg_math); SG_REF(sg_rand); + SG_REF(sg_signal); sg_print_message=print_message; sg_print_warning=print_warning; sg_print_error=print_error; sg_cancel_computations=cancel_computations; + // Set up signal handler + std::signal(SIGINT, sg_signal->handler); + init_from_env(); } @@ -116,6 +125,7 @@ namespace shogun sg_print_error=NULL; sg_cancel_computations=NULL; + SG_UNREF(sg_signal); SG_UNREF(sg_rand); SG_UNREF(sg_math); SG_UNREF(sg_version); @@ -191,12 +201,20 @@ namespace shogun SG_REF(sg_rand); return sg_rand; } + + CSignal* get_global_signal() + { + SG_REF(sg_signal); + return sg_signal; + } + #ifndef SWIG // SWIG should skip this part SGLinalg* get_global_linalg() { return sg_linalg.get(); } #endif + void init_from_env() { char* env_log_val = NULL; diff --git a/src/shogun/base/init.h b/src/shogun/base/init.h index 7f60097496a..143a281568a 100644 --- a/src/shogun/base/init.h +++ b/src/shogun/base/init.h @@ -23,91 +23,96 @@ namespace shogun class Parallel; class CRandom; class SGLinalg; - -/** This function must be called before libshogun is used. Usually shogun does - * not provide any output messages (neither debugging nor error; apart from - * exceptions). This function allows one to specify customized output - * callback functions and a callback function to check for exceptions: - * - * @param print_message function pointer to print a message - * @param print_warning function pointer to print a warning message - * @param print_error function pointer to print an error message (this will be - * printed before shogun throws an exception) - * - * @param cancel_computations function pointer to check for exception - * - */ -void init_shogun(void (*print_message)(FILE* target, const char* str) = NULL, - void (*print_warning)(FILE* target, const char* str) = NULL, - void (*print_error)(FILE* target, const char* str) = NULL, - void (*cancel_computations)(bool &delayed, bool &immediately)=NULL); - -/** init shogun with defaults */ -void init_shogun_with_defaults(); - -/** This function must be called when one stops using libshogun. It will - * perform a number of cleanups */ -void exit_shogun(); - -/** set the global io object - * - * @param io io object to use - */ -void set_global_io(SGIO* io); - -/** get the global io object - * - * @return io object - */ -SGIO* get_global_io(); - -/** set the global parallel object - * - * @param parallel parallel object to use - */ -void set_global_parallel(Parallel* parallel); - -/** get the global parallel object - * - * @return parallel object - */ -Parallel* get_global_parallel(); - -/** set the global version object - * - * @param version version object to use - */ -void set_global_version(Version* version); - -/** get the global version object - * - * @return version object - */ -Version* get_global_version(); - -/** set the global math object - * - * @param math math object to use - */ -void set_global_math(CMath* math); - -/** get the global math object - * - * @return math object - */ -CMath* get_global_math(); - -/** set the global random object - * - * @param rand random object to use - */ -void set_global_rand(CRandom* rand); - -/** get the global random object - * - * @return random object - */ -CRandom* get_global_rand(); + class CSignal; + + /** This function must be called before libshogun is used. Usually shogun + * does + * not provide any output messages (neither debugging nor error; apart from + * exceptions). This function allows one to specify customized output + * callback functions and a callback function to check for exceptions: + * + * @param print_message function pointer to print a message + * @param print_warning function pointer to print a warning message + * @param print_error function pointer to print an error message (this will + * be + * printed before shogun throws an + * exception) + * + * @param cancel_computations function pointer to check for exception + * + */ + void init_shogun( + void (*print_message)(FILE* target, const char* str) = NULL, + void (*print_warning)(FILE* target, const char* str) = NULL, + void (*print_error)(FILE* target, const char* str) = NULL, + void (*cancel_computations)(bool& delayed, bool& immediately) = NULL); + + /** init shogun with defaults */ + void init_shogun_with_defaults(); + + /** This function must be called when one stops using libshogun. It will + * perform a number of cleanups */ + void exit_shogun(); + + /** set the global io object + * + * @param io io object to use + */ + void set_global_io(SGIO* io); + + /** get the global io object + * + * @return io object + */ + SGIO* get_global_io(); + + /** set the global parallel object + * + * @param parallel parallel object to use + */ + void set_global_parallel(Parallel* parallel); + + /** get the global parallel object + * + * @return parallel object + */ + Parallel* get_global_parallel(); + + /** set the global version object + * + * @param version version object to use + */ + void set_global_version(Version* version); + + /** get the global version object + * + * @return version object + */ + Version* get_global_version(); + + /** set the global math object + * + * @param math math object to use + */ + void set_global_math(CMath* math); + + /** get the global math object + * + * @return math object + */ + CMath* get_global_math(); + + /** set the global random object + * + * @param rand random object to use + */ + void set_global_rand(CRandom* rand); + + /** get the global random object + * + * @return random object + */ + CRandom* get_global_rand(); #ifndef SWIG // SWIG should skip this part /** get the global linalg library object @@ -117,6 +122,12 @@ CRandom* get_global_rand(); SGLinalg* get_global_linalg(); #endif +/** get the global singnal handler object + * + * @return linalg object + */ +CSignal* get_global_signal(); + /** Checks environment variables and modifies global objects */ void init_from_env();