tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

at_exit.h (2444B)


      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.
      6 
      7 #ifndef BASE_AT_EXIT_H_
      8 #define BASE_AT_EXIT_H_
      9 
     10 #include <stack>
     11 
     12 #include "base/basictypes.h"
     13 
     14 #include "mozilla/Mutex.h"
     15 
     16 namespace base {
     17 
     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.
     22 //
     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;
     27 //
     28 // }
     29 // When the exit_manager object goes out of scope, all the registered
     30 // callbacks and singleton destructors will be called.
     31 
     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);
     39 
     40 public:
     41  typedef void (*AtExitCallbackType)(void*);
     42 
     43  AtExitManager();
     44 
     45  // The dtor calls all the registered callbacks. Do not try to register more
     46  // callbacks after this point.
     47  ~AtExitManager();
     48 
     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);
     52 
     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();
     56 
     57  static bool AlreadyRegistered();
     58 
     59 private:
     60  struct CallbackAndParam {
     61    CallbackAndParam(AtExitCallbackType func, void* param)
     62        : func_(func), param_(param) {}
     63    AtExitCallbackType func_;
     64    void* param_;
     65  };
     66 
     67  mozilla::Mutex lock_;
     68  std::stack<CallbackAndParam> stack_ MOZ_GUARDED_BY(lock_);
     69  AtExitManager* next_manager_;  // Stack of managers to allow shadowing.
     70 
     71  DISALLOW_COPY_AND_ASSIGN(AtExitManager);
     72 };
     73 
     74 }  // namespace base
     75 
     76 #endif  // BASE_AT_EXIT_H_