[M73 Dev][Tizen] Fix compilation errors for TV profile
[platform/framework/web/chromium-efl.git] / base / at_exit.cc
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/at_exit.h"
6
7 #include <stddef.h>
8 #include <ostream>
9 #include <utility>
10
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/logging.h"
14
15 namespace base {
16
17 // Keep a stack of registered AtExitManagers.  We always operate on the most
18 // recent, and we should never have more than one outside of testing (for a
19 // statically linked version of this library).  Testing may use the shadow
20 // version of the constructor, and if we are building a dynamic library we may
21 // end up with multiple AtExitManagers on the same process.  We don't protect
22 // this for thread-safe access, since it will only be modified in testing.
23 static AtExitManager* g_top_manager = nullptr;
24
25 static bool g_disable_managers = false;
26
27 AtExitManager::AtExitManager() : next_manager_(g_top_manager) {
28 // If multiple modules instantiate AtExitManagers they'll end up living in this
29 // module... they have to coexist.
30 #if !defined(COMPONENT_BUILD)
31   DCHECK(!g_top_manager);
32 #endif
33   g_top_manager = this;
34 }
35
36 AtExitManager::~AtExitManager() {
37   if (!g_top_manager) {
38     NOTREACHED() << "Tried to ~AtExitManager without an AtExitManager";
39     return;
40   }
41   DCHECK_EQ(this, g_top_manager);
42
43   if (!g_disable_managers)
44     ProcessCallbacksNow();
45   g_top_manager = next_manager_;
46 }
47
48 // static
49 void AtExitManager::RegisterCallback(AtExitCallbackType func, void* param) {
50   DCHECK(func);
51   RegisterTask(base::Bind(func, param));
52 }
53
54 // static
55 void AtExitManager::RegisterTask(base::Closure task) {
56   if (!g_top_manager) {
57     NOTREACHED() << "Tried to RegisterCallback without an AtExitManager";
58     return;
59   }
60
61   AutoLock lock(g_top_manager->lock_);
62 #if DCHECK_IS_ON()
63   DCHECK(!g_top_manager->processing_callbacks_);
64 #endif
65   g_top_manager->stack_.push(std::move(task));
66 }
67
68 // static
69 void AtExitManager::ProcessCallbacksNow() {
70   if (!g_top_manager) {
71     NOTREACHED() << "Tried to ProcessCallbacksNow without an AtExitManager";
72     return;
73   }
74
75   // Callbacks may try to add new callbacks, so run them without holding
76   // |lock_|. This is an error and caught by the DCHECK in RegisterTask(), but
77   // handle it gracefully in release builds so we don't deadlock.
78   base::stack<base::Closure> tasks;
79   {
80     AutoLock lock(g_top_manager->lock_);
81     tasks.swap(g_top_manager->stack_);
82 #if DCHECK_IS_ON()
83     g_top_manager->processing_callbacks_ = true;
84 #endif
85   }
86
87   // Relax the cross-thread access restriction to non-thread-safe RefCount.
88   // It's safe since all other threads should be terminated at this point.
89   ScopedAllowCrossThreadRefCountAccess allow_cross_thread_ref_count_access;
90
91   while (!tasks.empty()) {
92     base::Closure task = tasks.top();
93     task.Run();
94     tasks.pop();
95   }
96
97 #if DCHECK_IS_ON()
98   AutoLock lock(g_top_manager->lock_);
99   // Expect that all callbacks have been run.
100   DCHECK(g_top_manager->stack_.empty());
101   g_top_manager->processing_callbacks_ = false;
102 #endif
103 }
104
105 void AtExitManager::DisableAllAtExitManagers() {
106   AutoLock lock(g_top_manager->lock_);
107   g_disable_managers = true;
108 }
109
110 AtExitManager::AtExitManager(bool shadow) : next_manager_(g_top_manager) {
111   DCHECK(shadow || !g_top_manager);
112   g_top_manager = this;
113 }
114
115 }  // namespace base