STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
exithandler.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/common/exithandler.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
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 
14 #include <stxxl/bits/namespace.h>
15 
16 // 1. do nothing for default handler
17 // 2. #define STXXL_NON_DEFAULT_EXIT_HANDLER for a handler that does not use atexit()
18 // 3. #define STXXL_EXTERNAL_EXIT_HANDLER to provide your own implementation
19 
20 
21 #ifndef STXXL_EXTERNAL_EXIT_HANDLER
22 #ifndef STXXL_NON_DEFAULT_EXIT_HANDLER
23 
24 #include <cstdlib>
25 
27 
28 // default exit handler
29 int register_exit_handler(void (* function)(void))
30 {
31  return atexit(function);
32 }
33 
34 // default exit handler
36 {
37  // nothing to do
38 }
39 
41 
42 #else // STXXL_NON_DEFAULT_EXIT_HANDLER
43 
44 #include <vector>
46 
48 
49 mutex exit_handler_mutex;
50 std::vector<void (*)(void)> exit_handlers;
51 
52 int register_exit_handler(void (* function)(void))
53 {
54  scoped_mutex_lock lock(exit_handler_mutex);
55  exit_handlers.push_back(function);
56  return 0;
57 }
58 
59 // default exit handler
60 void run_exit_handlers()
61 {
62  scoped_mutex_lock lock(exit_handler_mutex);
63  while (!exit_handlers.empty()) {
64  (*(exit_handlers.back()))();
65  exit_handlers.pop_back();
66  }
67 }
68 
70 
71 #endif // STXXL_NON_DEFAULT_EXIT_HANDLER
72 #endif // STXXL_EXTERNAL_EXIT_HANDLER
73 
74 // vim: et:ts=4:sw=4
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void run_exit_handlers()
Definition: exithandler.cpp:35
int register_exit_handler(void(*function)(void))
Definition: exithandler.cpp:29
#define STXXL_END_NAMESPACE
Definition: namespace.h:17