ipc/chromium/src/base/at_exit.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/at_exit.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,73 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#ifndef BASE_AT_EXIT_H_
     1.9 +#define BASE_AT_EXIT_H_
    1.10 +
    1.11 +#include <stack>
    1.12 +
    1.13 +#include "base/basictypes.h"
    1.14 +#include "base/lock.h"
    1.15 +
    1.16 +namespace base {
    1.17 +
    1.18 +// This class provides a facility similar to the CRT atexit(), except that
    1.19 +// we control when the callbacks are executed. Under Windows for a DLL they
    1.20 +// happen at a really bad time and under the loader lock. This facility is
    1.21 +// mostly used by base::Singleton.
    1.22 +//
    1.23 +// The usage is simple. Early in the main() or WinMain() scope create an
    1.24 +// AtExitManager object on the stack:
    1.25 +// int main(...) {
    1.26 +//    base::AtExitManager exit_manager;
    1.27 +//
    1.28 +// }
    1.29 +// When the exit_manager object goes out of scope, all the registered
    1.30 +// callbacks and singleton destructors will be called.
    1.31 +
    1.32 +class AtExitManager {
    1.33 + protected:
    1.34 +  // This constructor will allow this instance of AtExitManager to be created
    1.35 +  // even if one already exists.  This should only be used for testing!
    1.36 +  // AtExitManagers are kept on a global stack, and it will be removed during
    1.37 +  // destruction.  This allows you to shadow another AtExitManager.
    1.38 +  AtExitManager(bool shadow);
    1.39 +
    1.40 + public:
    1.41 +  typedef void (*AtExitCallbackType)(void*);
    1.42 +
    1.43 +  AtExitManager();
    1.44 +
    1.45 +  // The dtor calls all the registered callbacks. Do not try to register more
    1.46 +  // callbacks after this point.
    1.47 +  ~AtExitManager();
    1.48 +
    1.49 +  // Registers the specified function to be called at exit. The prototype of
    1.50 +  // the callback function is void func().
    1.51 +  static void RegisterCallback(AtExitCallbackType func, void* param);
    1.52 +
    1.53 +  // Calls the functions registered with RegisterCallback in LIFO order. It
    1.54 +  // is possible to register new callbacks after calling this function.
    1.55 +  static void ProcessCallbacksNow();
    1.56 +
    1.57 +  static bool AlreadyRegistered();
    1.58 +
    1.59 + private:
    1.60 +  struct CallbackAndParam {
    1.61 +    CallbackAndParam(AtExitCallbackType func, void* param)
    1.62 +        : func_(func), param_(param) { }
    1.63 +    AtExitCallbackType func_;
    1.64 +    void* param_;
    1.65 +  };
    1.66 +
    1.67 +  Lock lock_;
    1.68 +  std::stack<CallbackAndParam> stack_;
    1.69 +  AtExitManager* next_manager_;  // Stack of managers to allow shadowing.
    1.70 +
    1.71 +  DISALLOW_COPY_AND_ASSIGN(AtExitManager);
    1.72 +};
    1.73 +
    1.74 +}  // namespace base
    1.75 +
    1.76 +#endif  // BASE_AT_EXIT_H_

mercurial