OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 #include "chrome/browser/sync/engine/syncer_thread_timed_stop.h" | |
5 | |
6 #include "build/build_config.h" | |
7 | |
8 #ifdef OS_MACOSX | |
9 #include <CoreFoundation/CFNumber.h> | |
10 #include <IOKit/IOTypes.h> | |
11 #include <IOKit/IOKitLib.h> | |
12 #endif | |
13 | |
14 #include <algorithm> | |
15 #include <map> | |
16 #include <queue> | |
17 | |
18 #include "chrome/browser/sync/engine/auth_watcher.h" | |
19 #include "chrome/browser/sync/engine/model_safe_worker.h" | |
20 #include "chrome/browser/sync/engine/net/server_connection_manager.h" | |
21 #include "chrome/browser/sync/engine/syncer.h" | |
22 #include "chrome/browser/sync/notifier/listener/talk_mediator.h" | |
23 #include "chrome/browser/sync/notifier/listener/talk_mediator_impl.h" | |
24 #include "chrome/browser/sync/syncable/directory_manager.h" | |
25 | |
26 using std::priority_queue; | |
27 using std::min; | |
28 using base::Time; | |
29 using base::TimeDelta; | |
30 using base::TimeTicks; | |
31 | |
32 namespace browser_sync { | |
33 | |
34 SyncerThreadTimedStop::SyncerThreadTimedStop( | |
35 ClientCommandChannel* command_channel, | |
36 syncable::DirectoryManager* mgr, | |
37 ServerConnectionManager* connection_manager, | |
38 AllStatus* all_status, | |
39 ModelSafeWorker* model_safe_worker) | |
40 : SyncerThread(command_channel, mgr, connection_manager, all_status, | |
41 model_safe_worker), | |
42 in_thread_main_loop_(false) { | |
43 } | |
44 | |
45 // Stop processing. A max wait of at least 2*server RTT time is recommended. | |
46 // Returns true if we stopped, false otherwise. | |
47 bool SyncerThreadTimedStop::Stop(int max_wait) { | |
48 AutoLock lock(lock_); | |
49 // If the thread has been started, then we either already have or are about to | |
50 // enter ThreadMainLoop so we have to proceed with shutdown and wait for it to | |
51 // finish. If the thread has not been started --and we now own the lock-- | |
52 // then we can early out because the caller has not called Start(). | |
53 if (!thread_.IsRunning()) | |
54 return true; | |
55 | |
56 LOG(INFO) << "SyncerThread::Stop - setting ThreadMain exit condition to " | |
57 << "true (vault_.stop_syncer_thread_)"; | |
58 // Exit the ThreadMainLoop once the syncer finishes (we tell it to exit | |
59 // below). | |
60 vault_.stop_syncer_thread_ = true; | |
61 if (NULL != vault_.syncer_) { | |
62 // Try to early exit the syncer. | |
63 vault_.syncer_->RequestEarlyExit(); | |
64 } | |
65 | |
66 // stop_syncer_thread_ is now true and the Syncer has been told to exit. | |
67 // We want to wake up all waiters so they can re-examine state. We signal, | |
68 // causing all waiters to try to re-acquire the lock, and then we atomically | |
69 // release the lock and wait. Our wait can be spuriously signaled, so we | |
70 // recalculate the remaining sleep time each time through and re- | |
71 // check the condition before exiting the loop. | |
72 vault_field_changed_.Broadcast(); | |
73 TimeTicks start = TimeTicks::Now(); | |
74 TimeTicks end = start + TimeDelta::FromMilliseconds(max_wait); | |
75 bool timed_out = false; | |
76 // Eventually the combination of RequestEarlyExit and setting | |
77 // stop_syncer_thread_ to true above will cause in_thread_main_loop_ to become | |
78 // false. | |
79 while (in_thread_main_loop_) { | |
80 TimeDelta sleep_time = end - TimeTicks::Now(); | |
81 if (sleep_time < TimeDelta::FromSeconds(0)) { | |
82 timed_out = true; | |
83 break; | |
84 } | |
85 LOG(INFO) << "Waiting in stop for " << sleep_time.InSeconds() << "s."; | |
86 vault_field_changed_.TimedWait(sleep_time); | |
87 } | |
88 | |
89 if (timed_out) { | |
90 LOG(ERROR) << "SyncerThread::Stop timed out or error. Problems likely."; | |
91 return false; | |
92 } | |
93 | |
94 // Stop() should not block on anything at this point, given above madness. | |
95 DLOG(INFO) << "Calling SyncerThread::thread_.Stop() at " | |
96 << Time::Now().ToInternalValue(); | |
97 thread_.Stop(); | |
98 DLOG(INFO) << "SyncerThread::thread_.Stop() finished at " | |
99 << Time::Now().ToInternalValue(); | |
100 return true; | |
101 } | |
102 | |
103 void SyncerThreadTimedStop::ThreadMain() { | |
104 AutoLock lock(lock_); | |
105 // Signal Start() to let it know we've made it safely are now running on the | |
106 // message loop, and unblock it's caller. | |
107 thread_main_started_.Signal(); | |
108 | |
109 // The only thing that could be waiting on this value is Stop, and we don't | |
110 // release the lock until we're far enough along to Stop safely. | |
111 in_thread_main_loop_ = true; | |
112 vault_field_changed_.Broadcast(); | |
113 ThreadMainLoop(); | |
114 in_thread_main_loop_ = false; | |
115 vault_field_changed_.Broadcast(); | |
116 LOG(INFO) << "Syncer thread ThreadMain is done."; | |
117 } | |
118 | |
119 } // namespace browser_sync | |
OLD | NEW |