STXXL  1.4-dev
 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 #ifndef STXXL_EXTERNAL_EXIT_HANDLER
21 #ifndef STXXL_NON_DEFAULT_EXIT_HANDLER
22 
23 #include <cstdlib>
24 
26 
27 // default exit handler
28 int register_exit_handler(void (* function)(void))
29 {
30  return atexit(function);
31 }
32 
33 // default exit handler
35 {
36  // nothing to do
37 }
38 
40 
41 #else // STXXL_NON_DEFAULT_EXIT_HANDLER
42 
43 #include <vector>
45 
47 
48 mutex exit_handler_mutex;
49 std::vector<void (*)(void)> exit_handlers;
50 
51 int register_exit_handler(void (* function)(void))
52 {
53  scoped_mutex_lock lock(exit_handler_mutex);
54  exit_handlers.push_back(function);
55  return 0;
56 }
57 
58 // default exit handler
59 void run_exit_handlers()
60 {
61  scoped_mutex_lock lock(exit_handler_mutex);
62  while (!exit_handlers.empty()) {
63  (*(exit_handlers.back()))();
64  exit_handlers.pop_back();
65  }
66 }
67 
69 
70 #endif // STXXL_NON_DEFAULT_EXIT_HANDLER
71 #endif // STXXL_EXTERNAL_EXIT_HANDLER
72 
73 // vim: et:ts=4:sw=4
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void run_exit_handlers()
Definition: exithandler.cpp:34
int register_exit_handler(void(*function)(void))
Definition: exithandler.cpp:28
#define STXXL_END_NAMESPACE
Definition: namespace.h:17