Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * common/exithandler.cpp 00003 * 00004 * Part of the STXXL. See http://stxxl.sourceforge.net 00005 * 00006 * Copyright (C) 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de> 00007 * 00008 * Distributed under the Boost Software License, Version 1.0. 00009 * (See accompanying file LICENSE_1_0.txt or copy at 00010 * http://www.boost.org/LICENSE_1_0.txt) 00011 **************************************************************************/ 00012 00013 #include <stxxl/bits/common/exithandler.h> 00014 00015 // 1. do nothing for default handler 00016 // 2. #define STXXL_NON_DEFAULT_EXIT_HANDLER for a handler that does not use atexit() 00017 // 3. #define STXXL_EXTERNAL_EXIT_HANDLER to provide your own implementation 00018 00019 00020 #ifndef STXXL_EXTERNAL_EXIT_HANDLER 00021 #ifndef STXXL_NON_DEFAULT_EXIT_HANDLER 00022 00023 #include <cstdlib> 00024 00025 __STXXL_BEGIN_NAMESPACE 00026 00027 // default exit handler 00028 int register_exit_handler(void (* function)(void)) 00029 { 00030 return atexit(function); 00031 } 00032 00033 // default exit handler 00034 void run_exit_handlers() 00035 { 00036 // nothing to do 00037 } 00038 00039 __STXXL_END_NAMESPACE 00040 00041 #else // STXXL_NON_DEFAULT_EXIT_HANDLER 00042 00043 #include <vector> 00044 #include <stxxl/bits/common/mutex.h> 00045 00046 __STXXL_BEGIN_NAMESPACE 00047 00048 mutex exit_handler_mutex; 00049 std::vector<void (*)(void)> exit_handlers; 00050 00051 int register_exit_handler(void (* function)(void)) 00052 { 00053 scoped_mutex_lock lock(exit_handler_mutex); 00054 exit_handlers.push_back(function); 00055 return 0; 00056 } 00057 00058 // default exit handler 00059 void run_exit_handlers() 00060 { 00061 scoped_mutex_lock lock(exit_handler_mutex); 00062 while (!exit_handlers.empty()) { 00063 (*(exit_handlers.back()))(); 00064 exit_handlers.pop_back(); 00065 } 00066 } 00067 00068 __STXXL_END_NAMESPACE 00069 00070 #endif // STXXL_NON_DEFAULT_EXIT_HANDLER 00071 #endif // STXXL_EXTERNAL_EXIT_HANDLER 00072 00073 // vim: et:ts=4:sw=4