Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: tools/android/forwarder2/forwarders_manager.cc

Issue 148113003: forwarder2: Make the Forwarder instances operate on a single thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Create ScopedClosureRunner later Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "tools/android/forwarder2/forwarders_manager.h" 5 #include "tools/android/forwarder2/forwarders_manager.h"
6 6
7 #include <sys/select.h>
8 #include <unistd.h>
9
7 #include <algorithm> 10 #include <algorithm>
8 11
9 #include "base/basictypes.h" 12 #include "base/basictypes.h"
10 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/callback_helpers.h"
11 #include "base/location.h" 15 #include "base/location.h"
12 #include "base/logging.h" 16 #include "base/logging.h"
13 #include "base/message_loop/message_loop_proxy.h" 17 #include "base/message_loop/message_loop_proxy.h"
18 #include "base/posix/eintr_wrapper.h"
14 #include "tools/android/forwarder2/forwarder.h" 19 #include "tools/android/forwarder2/forwarder.h"
15 #include "tools/android/forwarder2/socket.h" 20 #include "tools/android/forwarder2/socket.h"
16 21
17 namespace forwarder2 { 22 namespace forwarder2 {
18 23
19 ForwardersManager::ForwardersManager() : delegate_(new Delegate()) {} 24 ForwardersManager::ForwardersManager() : thread_("ForwardersManagerThread") {
25 thread_.Start();
26 WaitForEventsOnInternalThreadSoon();
27 }
28
20 29
21 ForwardersManager::~ForwardersManager() { 30 ForwardersManager::~ForwardersManager() {
22 delegate_->Clear(); 31 deletion_notifier_.Notify();
23 } 32 }
24 33
25 void ForwardersManager::CreateAndStartNewForwarder(scoped_ptr<Socket> socket1, 34 void ForwardersManager::CreateAndStartNewForwarder(scoped_ptr<Socket> socket1,
26 scoped_ptr<Socket> socket2) { 35 scoped_ptr<Socket> socket2) {
27 delegate_->CreateAndStartNewForwarder(socket1.Pass(), socket2.Pass()); 36 // Note that the internal Forwarder vector is populated on the internal thread
37 // which is the only thread from which it's accessed.
38 thread_.message_loop_proxy()->PostTask(
39 FROM_HERE,
40 base::Bind(&ForwardersManager::CreateNewForwarderOnInternalThread,
41 base::Unretained(this), base::Passed(&socket1),
42 base::Passed(&socket2)));
43
44 // Guarantees that the CreateNewForwarderOnInternalThread callback posted to
45 // the internal thread gets executed immediately.
46 wakeup_notifier_.Notify();
28 } 47 }
29 48
30 ForwardersManager::Delegate::Delegate() {} 49 void ForwardersManager::CreateNewForwarderOnInternalThread(
31 50 scoped_ptr<Socket> socket1,
32 ForwardersManager::Delegate::~Delegate() { 51 scoped_ptr<Socket> socket2) {
33 // The forwarder instances should already have been deleted on their 52 DCHECK(thread_.message_loop_proxy()->RunsTasksOnCurrentThread());
34 // construction thread. Deleting them here would be unsafe since we don't know 53 forwarders_.push_back(new Forwarder(socket1.Pass(), socket2.Pass()));
35 // which thread this destructor is called on.
36 DCHECK(forwarders_.empty());
37 } 54 }
38 55
39 void ForwardersManager::Delegate::Clear() { 56 void ForwardersManager::WaitForEventsOnInternalThreadSoon() {
40 if (!forwarders_constructor_runner_) { 57 thread_.message_loop_proxy()->PostTask(
41 DCHECK(forwarders_.empty()); 58 FROM_HERE,
59 base::Bind(&ForwardersManager::WaitForEventsOnInternalThread,
60 base::Unretained(this)));
61 }
62
63 void ForwardersManager::WaitForEventsOnInternalThread() {
64 DCHECK(thread_.message_loop_proxy()->RunsTasksOnCurrentThread());
65 fd_set read_fds;
66 fd_set write_fds;
67
68 FD_ZERO(&read_fds);
69 FD_ZERO(&write_fds);
70
71 // Populate the file descriptor sets.
72 int max_fd = -1;
73 for (ScopedVector<Forwarder>::iterator it = forwarders_.begin();
74 it != forwarders_.end(); ++it) {
75 Forwarder* const forwarder = *it;
76 forwarder->RegisterFDs(&read_fds, &write_fds, &max_fd);
77 }
78
79 const int notifier_fds[] = {
80 wakeup_notifier_.receiver_fd(),
81 deletion_notifier_.receiver_fd(),
82 };
83
84 for (int i = 0; i < arraysize(notifier_fds); ++i) {
85 const int notifier_fd = notifier_fds[i];
86 DCHECK_GT(notifier_fd, -1);
87 FD_SET(notifier_fd, &read_fds);
88 max_fd = std::max(max_fd, notifier_fd);
89 }
90
91 const int ret = HANDLE_EINTR(
92 select(max_fd + 1, &read_fds, &write_fds, NULL, NULL));
93 if (ret < 0) {
94 PLOG(ERROR) << "select";
42 return; 95 return;
43 } 96 }
44 if (forwarders_constructor_runner_->RunsTasksOnCurrentThread()) { 97
45 ClearOnForwarderConstructorThread(); 98 const bool must_shutdown = FD_ISSET(
99 deletion_notifier_.receiver_fd(), &read_fds);
100 if (must_shutdown && forwarders_.empty())
101 return;
102
103 base::ScopedClosureRunner wait_for_events_soon(
digit1 2014/01/28 17:28:33 Can you avoid using this? a base::Bind() is pretty
qsr 2014/01/29 09:30:17 Given that this is only called when select returns
104 base::Bind(&ForwardersManager::WaitForEventsOnInternalThreadSoon,
105 base::Unretained(this)));
106
107 if (FD_ISSET(wakeup_notifier_.receiver_fd(), &read_fds)) {
108 // Note that the events on FDs other than the wakeup notifier one, if any,
109 // will be processed upon the next select().
110 wakeup_notifier_.Reset();
46 return; 111 return;
47 } 112 }
48 forwarders_constructor_runner_->PostTask(
49 FROM_HERE,
50 base::Bind(
51 &ForwardersManager::Delegate::ClearOnForwarderConstructorThread,
52 this));
53 }
54 113
55 void ForwardersManager::Delegate::CreateAndStartNewForwarder( 114 // Notify the Forwarder instances and remove the ones that are closed.
56 scoped_ptr<Socket> socket1, 115 for (size_t i = 0; i < forwarders_.size(); ) {
57 scoped_ptr<Socket> socket2) { 116 Forwarder* const forwarder = forwarders_[i];
58 const scoped_refptr<base::SingleThreadTaskRunner> current_task_runner( 117 forwarder->ProcessEvents(read_fds, write_fds);
59 base::MessageLoopProxy::current()); 118
60 DCHECK(current_task_runner); 119 if (must_shutdown)
61 if (forwarders_constructor_runner_) { 120 forwarder->Shutdown();
62 DCHECK_EQ(current_task_runner, forwarders_constructor_runner_); 121
63 } else { 122 if (!forwarder->IsClosed()) {
pasko 2014/01/27 16:59:02 I think if you replace one loop with three, it wou
Philippe 2014/01/28 08:59:09 You slightly cheated by using a foreach loop :) So
pasko 2014/01/28 10:48:56 Oh, I did not realize that. It can get straight to
64 forwarders_constructor_runner_ = current_task_runner; 123 ++i;
124 continue;
125 }
126
127 std::swap(forwarders_[i], forwarders_.back());
128 forwarders_.pop_back();
65 } 129 }
66 forwarders_.push_back(
67 new Forwarder(socket1.Pass(), socket2.Pass(),
68 &deletion_notifier_,
69 base::Bind(&ForwardersManager::Delegate::OnForwarderError,
70 this)));
71 forwarders_.back()->Start();
72 }
73
74 void ForwardersManager::Delegate::OnForwarderError(
75 scoped_ptr<Forwarder> forwarder) {
76 DCHECK(forwarders_constructor_runner_->RunsTasksOnCurrentThread());
77 const ScopedVector<Forwarder>::iterator it = std::find(
78 forwarders_.begin(), forwarders_.end(), forwarder.get());
79 DCHECK(it != forwarders_.end());
80 std::swap(*it, forwarders_.back());
81 forwarders_.pop_back();
82 ignore_result(forwarder.release()); // Deleted by the pop_back() above.
83 }
84
85 void ForwardersManager::Delegate::ClearOnForwarderConstructorThread() {
86 DCHECK(forwarders_constructor_runner_->RunsTasksOnCurrentThread());
87 deletion_notifier_.Notify();
88 forwarders_.clear();
89 } 130 }
90 131
91 } // namespace forwarder2 132 } // namespace forwarder2
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698