1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
7 #ifndef BASE_AT_EXIT_H_
8 #define BASE_AT_EXIT_H_
12 #include "base/basictypes.h"
14 #include "mozilla/Mutex.h"
18 // This class provides a facility similar to the CRT atexit(), except that
19 // we control when the callbacks are executed. Under Windows for a DLL they
20 // happen at a really bad time and under the loader lock. This facility is
21 // mostly used by base::Singleton.
23 // The usage is simple. Early in the main() or WinMain() scope create an
24 // AtExitManager object on the stack:
26 // base::AtExitManager exit_manager;
29 // When the exit_manager object goes out of scope, all the registered
30 // callbacks and singleton destructors will be called.
34 // This constructor will allow this instance of AtExitManager to be created
35 // even if one already exists. This should only be used for testing!
36 // AtExitManagers are kept on a global stack, and it will be removed during
37 // destruction. This allows you to shadow another AtExitManager.
38 explicit AtExitManager(bool shadow
);
41 typedef void (*AtExitCallbackType
)(void*);
45 // The dtor calls all the registered callbacks. Do not try to register more
46 // callbacks after this point.
49 // Registers the specified function to be called at exit. The prototype of
50 // the callback function is void func().
51 static void RegisterCallback(AtExitCallbackType func
, void* param
);
53 // Calls the functions registered with RegisterCallback in LIFO order. It
54 // is possible to register new callbacks after calling this function.
55 static void ProcessCallbacksNow();
57 static bool AlreadyRegistered();
60 struct CallbackAndParam
{
61 CallbackAndParam(AtExitCallbackType func
, void* param
)
62 : func_(func
), param_(param
) {}
63 AtExitCallbackType func_
;
68 std::stack
<CallbackAndParam
> stack_
MOZ_GUARDED_BY(lock_
);
69 AtExitManager
* next_manager_
; // Stack of managers to allow shadowing.
71 DISALLOW_COPY_AND_ASSIGN(AtExitManager
);
76 #endif // BASE_AT_EXIT_H_