Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
exithandler.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/common/exithandler.cpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
5  *
6  * Copyright (C) 2009 Andreas Beckmann <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #include <cstdlib>
14 
16 
17 // 1. do nothing for default handler
18 // 2. #define FOXXLL_NON_DEFAULT_EXIT_HANDLER for a handler that does not use atexit()
19 // 3. #define FOXXLL_EXTERNAL_EXIT_HANDLER to provide your own implementation
20 
21 #ifdef STXXL_EXTERNAL_EXIT_HANDLER
22 static_assert(false, "STXXL_EXTERNAL_EXIT_HANDLER was renamed to FOXXLL_EXTERNAL_EXIT_HANDLER");
23 #endif
24 
25 #ifdef STXXL_NON_DEFAULT_EXIT_HANDLER
26 static_assert(false, "STXXL_NON_DEFAULT_EXIT_HANDLER was renamed to FOXXLL_NON_DEFAULT_EXIT_HANDLER");
27 #endif
28 
29 #ifndef FOXXLL_EXTERNAL_EXIT_HANDLER
30 #ifndef FOXXLL_NON_DEFAULT_EXIT_HANDLER
31 
32 namespace foxxll {
33 
34 // default exit handler
35 int register_exit_handler(void (* function)(void))
36 {
37  return atexit(function);
38 }
39 
40 // default exit handler
42 {
43  // nothing to do
44 }
45 
46 } // namespace foxxll
47 
48 #else // FOXXLL_NON_DEFAULT_EXIT_HANDLER
49 
50 #include <mutex>
51 #include <vector>
52 
53 namespace foxxll {
54 
55 std::mutex exit_handler_mutex;
56 std::vector<void (*)(void)> exit_handlers;
57 
58 int register_exit_handler(void (* function)(void))
59 {
60  std::unique_lock<std::mutex> lock(exit_handler_mutex);
61  exit_handlers.push_back(function);
62  return 0;
63 }
64 
65 // default exit handler
66 void run_exit_handlers()
67 {
68  std::unique_lock<std::mutex> lock(exit_handler_mutex);
69  while (!exit_handlers.empty()) {
70  (*(exit_handlers.back()))();
71  exit_handlers.pop_back();
72  }
73 }
74 
75 } // namespace foxxll
76 
77 #endif // FOXXLL_NON_DEFAULT_EXIT_HANDLER
78 #endif // FOXXLL_EXTERNAL_EXIT_HANDLER
79 
80 /**************************************************************************/
void run_exit_handlers()
Definition: exithandler.cpp:41
int register_exit_handler(void(*function)(void))
Definition: exithandler.cpp:35