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