Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / ipc / chromium / src / base / at_exit.h
blob268d9e0fbeceaa01b5caed9c641d00ecd37881de
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_
10 #include <stack>
12 #include "base/basictypes.h"
14 #include "mozilla/Mutex.h"
16 namespace base {
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:
25 // int main(...) {
26 // base::AtExitManager exit_manager;
28 // }
29 // When the exit_manager object goes out of scope, all the registered
30 // callbacks and singleton destructors will be called.
32 class AtExitManager {
33 protected:
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);
40 public:
41 typedef void (*AtExitCallbackType)(void*);
43 AtExitManager();
45 // The dtor calls all the registered callbacks. Do not try to register more
46 // callbacks after this point.
47 ~AtExitManager();
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();
59 private:
60 struct CallbackAndParam {
61 CallbackAndParam(AtExitCallbackType func, void* param)
62 : func_(func), param_(param) {}
63 AtExitCallbackType func_;
64 void* param_;
67 mozilla::Mutex lock_;
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);
74 } // namespace base
76 #endif // BASE_AT_EXIT_H_