2009-03-04 04:32:55 +01:00
|
|
|
/* http://frotznet.googlecode.com/svn/trunk/utils/fdevent.c
|
|
|
|
**
|
|
|
|
** Copyright 2006, Brian Swetland <swetland@frotz.net>
|
|
|
|
**
|
2015-08-03 19:38:08 +02:00
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
2009-03-04 04:32:55 +01:00
|
|
|
**
|
2015-08-03 19:38:08 +02:00
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
2009-03-04 04:32:55 +01:00
|
|
|
**
|
2015-08-03 19:38:08 +02:00
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
2009-03-04 04:32:55 +01:00
|
|
|
** limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-23 00:52:57 +02:00
|
|
|
#define TRACE_TAG FDEVENT
|
2011-03-16 23:57:42 +01:00
|
|
|
|
2015-03-19 23:21:08 +01:00
|
|
|
#include "sysdeps.h"
|
|
|
|
#include "fdevent.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
2018-03-16 22:25:42 +01:00
|
|
|
#include <stdint.h>
|
2015-03-19 23:21:08 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-02-10 23:49:00 +01:00
|
|
|
#include <atomic>
|
2018-02-23 23:37:07 +01:00
|
|
|
#include <deque>
|
2017-05-03 23:10:39 +02:00
|
|
|
#include <functional>
|
2015-09-05 01:19:56 +02:00
|
|
|
#include <list>
|
2017-05-03 23:10:39 +02:00
|
|
|
#include <mutex>
|
2015-09-05 01:19:56 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
2017-05-03 23:10:39 +02:00
|
|
|
#include <android-base/thread_annotations.h>
|
2018-03-16 22:25:42 +01:00
|
|
|
#include <android-base/threads.h>
|
2015-09-05 01:19:56 +02:00
|
|
|
|
2015-02-25 06:26:58 +01:00
|
|
|
#include "adb_io.h"
|
2014-07-29 21:50:02 +02:00
|
|
|
#include "adb_trace.h"
|
2017-05-03 23:10:39 +02:00
|
|
|
#include "adb_unique_fd.h"
|
2015-10-07 00:10:05 +02:00
|
|
|
#include "adb_utils.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#define FDE_EVENTMASK 0x00ff
|
|
|
|
#define FDE_STATEMASK 0xff00
|
|
|
|
|
|
|
|
#define FDE_ACTIVE 0x0100
|
|
|
|
#define FDE_PENDING 0x0200
|
|
|
|
#define FDE_CREATED 0x0400
|
|
|
|
|
2015-09-05 01:19:56 +02:00
|
|
|
struct PollNode {
|
|
|
|
fdevent* fde;
|
2016-02-17 02:34:53 +01:00
|
|
|
adb_pollfd pollfd;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-04-30 00:44:04 +02:00
|
|
|
explicit PollNode(fdevent* fde) : fde(fde) {
|
2015-09-05 01:19:56 +02:00
|
|
|
memset(&pollfd, 0, sizeof(pollfd));
|
2018-05-14 22:42:49 +02:00
|
|
|
pollfd.fd = fde->fd.get();
|
2015-09-29 21:25:33 +02:00
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
// Always enable POLLRDHUP, so the host server can take action when some clients disconnect.
|
|
|
|
// Then we can avoid leaving many sockets in CLOSE_WAIT state. See http://b/23314034.
|
|
|
|
pollfd.events = POLLRDHUP;
|
|
|
|
#endif
|
2015-09-05 01:19:56 +02:00
|
|
|
}
|
|
|
|
};
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-09-05 01:19:56 +02:00
|
|
|
// All operations to fdevent should happen only in the main thread.
|
|
|
|
// That's why we don't need a lock for fdevent.
|
2015-11-12 02:56:12 +01:00
|
|
|
static auto& g_poll_node_map = *new std::unordered_map<int, PollNode>();
|
|
|
|
static auto& g_pending_list = *new std::list<fdevent*>();
|
2016-02-10 23:49:00 +01:00
|
|
|
static std::atomic<bool> terminate_loop(false);
|
2015-10-31 02:37:26 +01:00
|
|
|
static bool main_thread_valid;
|
2018-03-16 22:25:42 +01:00
|
|
|
static uint64_t main_thread_id;
|
2015-10-31 02:37:26 +01:00
|
|
|
|
2018-03-30 01:23:43 +02:00
|
|
|
static bool run_needs_flush = false;
|
2017-05-03 23:10:39 +02:00
|
|
|
static auto& run_queue_notify_fd = *new unique_fd();
|
|
|
|
static auto& run_queue_mutex = *new std::mutex();
|
2018-02-23 23:37:07 +01:00
|
|
|
static auto& run_queue GUARDED_BY(run_queue_mutex) = *new std::deque<std::function<void()>>();
|
2017-05-03 23:10:39 +02:00
|
|
|
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
void check_main_thread() {
|
2015-10-31 02:37:26 +01:00
|
|
|
if (main_thread_valid) {
|
2018-03-16 22:25:42 +01:00
|
|
|
CHECK_EQ(main_thread_id, android::base::GetThreadId());
|
2015-10-31 02:37:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
adb: fix two device offline problems.
When device goes offline, user usually has to manually replug the
usb device. This patch tries to solve two offline situations, all
because when adb on host is killed, the adbd on device is not notified.
1. When adb server is killed while pushing a large file to device,
the device is still reading the unfinished large message. So the
device thinks of the CNXN message as part of the previous unfinished
message, so it doesn't reply and the device is in offline state.
The solution is to add a write_msg_lock in atransport struct. And it
kicks the transport only after sending a whole message. By kicking
all transports before exit, we ensure that we don't write part of
a message to any device. So next time we start adb server, the device
should be waiting for a new message.
2. When adb server is killed while pulling a large file from device,
the device is still trying to send the unfinished large message. So
adb on host usually reads data with EOVERFLOW error. This is because
adb on host is reading less than one packet sent from device.
The solution is to use buffered read on host. The max packet size
of bulk transactions in USB 3.0 is 1024 bytes. By preparing an at least
1024 bytes buffer when reading, EOVERFLOW no longer occurs. And teach
adb host to ignore wrong messages.
To be safe, this patch doesn't change any logic on device.
Bug: http://b/32952319
Test: run python -m unittest -q test_device.DeviceOfflineTest
Test: on linux/mac/windows with bullhead, ryu.
Change-Id: Ib149d30028a62a6f03857b8a95ab5a1d6e9b9c4e
2017-03-11 01:01:01 +01:00
|
|
|
void set_main_thread() {
|
2015-10-31 02:37:26 +01:00
|
|
|
main_thread_valid = true;
|
2018-03-16 22:25:42 +01:00
|
|
|
main_thread_id = android::base::GetThreadId();
|
2015-10-31 02:37:26 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-09-05 01:19:56 +02:00
|
|
|
static std::string dump_fde(const fdevent* fde) {
|
|
|
|
std::string state;
|
|
|
|
if (fde->state & FDE_ACTIVE) {
|
|
|
|
state += "A";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-05 01:19:56 +02:00
|
|
|
if (fde->state & FDE_PENDING) {
|
|
|
|
state += "P";
|
2014-09-26 00:10:34 +02:00
|
|
|
}
|
2015-09-05 01:19:56 +02:00
|
|
|
if (fde->state & FDE_CREATED) {
|
|
|
|
state += "C";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-05 01:19:56 +02:00
|
|
|
if (fde->state & FDE_READ) {
|
|
|
|
state += "R";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-05 01:19:56 +02:00
|
|
|
if (fde->state & FDE_WRITE) {
|
|
|
|
state += "W";
|
|
|
|
}
|
|
|
|
if (fde->state & FDE_ERROR) {
|
|
|
|
state += "E";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-05-14 22:42:49 +02:00
|
|
|
return android::base::StringPrintf("(fdevent %d %s)", fde->fd.get(), state.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:14:33 +02:00
|
|
|
void fdevent_install(fdevent* fde, int fd, fd_func func, void* arg) {
|
2015-10-31 02:37:26 +01:00
|
|
|
check_main_thread();
|
2018-05-14 20:14:33 +02:00
|
|
|
CHECK_GE(fd, 0);
|
|
|
|
memset(fde, 0, sizeof(fdevent));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:14:33 +02:00
|
|
|
fdevent* fdevent_create(int fd, fd_func func, void* arg) {
|
2015-10-31 02:37:26 +01:00
|
|
|
check_main_thread();
|
2015-09-05 01:19:56 +02:00
|
|
|
CHECK_GE(fd, 0);
|
2018-05-14 20:14:33 +02:00
|
|
|
|
|
|
|
fdevent* fde = new fdevent();
|
2015-09-05 01:19:56 +02:00
|
|
|
fde->state = FDE_ACTIVE;
|
2018-05-14 22:42:49 +02:00
|
|
|
fde->fd.reset(fd);
|
2015-09-05 01:19:56 +02:00
|
|
|
fde->func = func;
|
|
|
|
fde->arg = arg;
|
2015-10-07 00:10:05 +02:00
|
|
|
if (!set_file_block_mode(fd, false)) {
|
2015-09-16 01:27:09 +02:00
|
|
|
// Here is not proper to handle the error. If it fails here, some error is
|
|
|
|
// likely to be detected by poll(), then we can let the callback function
|
|
|
|
// to handle it.
|
2015-10-07 00:10:05 +02:00
|
|
|
LOG(ERROR) << "failed to set non-blocking mode for fd " << fd;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-05-14 22:42:49 +02:00
|
|
|
auto pair = g_poll_node_map.emplace(fde->fd.get(), PollNode(fde));
|
2015-09-05 01:19:56 +02:00
|
|
|
CHECK(pair.second) << "install existing fd " << fd;
|
2018-05-14 20:14:33 +02:00
|
|
|
|
|
|
|
fde->state |= FDE_CREATED;
|
|
|
|
return fde;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 20:14:33 +02:00
|
|
|
void fdevent_destroy(fdevent* fde) {
|
2015-10-31 02:37:26 +01:00
|
|
|
check_main_thread();
|
2018-05-14 20:14:33 +02:00
|
|
|
if (fde == 0) return;
|
|
|
|
if (!(fde->state & FDE_CREATED)) {
|
|
|
|
LOG(FATAL) << "destroying fde not created by fdevent_create(): " << dump_fde(fde);
|
|
|
|
}
|
|
|
|
|
2015-09-05 01:19:56 +02:00
|
|
|
if (fde->state & FDE_ACTIVE) {
|
2018-05-14 22:42:49 +02:00
|
|
|
g_poll_node_map.erase(fde->fd.get());
|
2015-09-05 01:19:56 +02:00
|
|
|
if (fde->state & FDE_PENDING) {
|
|
|
|
g_pending_list.remove(fde);
|
|
|
|
}
|
2018-05-14 22:42:49 +02:00
|
|
|
fde->fd.reset();
|
2015-09-05 01:19:56 +02:00
|
|
|
fde->state = 0;
|
|
|
|
fde->events = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-05-14 20:14:33 +02:00
|
|
|
|
|
|
|
delete fde;
|
2015-09-05 01:19:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fdevent_update(fdevent* fde, unsigned events) {
|
2018-05-14 22:42:49 +02:00
|
|
|
auto it = g_poll_node_map.find(fde->fd.get());
|
2015-09-05 01:19:56 +02:00
|
|
|
CHECK(it != g_poll_node_map.end());
|
|
|
|
PollNode& node = it->second;
|
|
|
|
if (events & FDE_READ) {
|
|
|
|
node.pollfd.events |= POLLIN;
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2015-09-05 01:19:56 +02:00
|
|
|
node.pollfd.events &= ~POLLIN;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-05 01:19:56 +02:00
|
|
|
|
|
|
|
if (events & FDE_WRITE) {
|
|
|
|
node.pollfd.events |= POLLOUT;
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2015-09-05 01:19:56 +02:00
|
|
|
node.pollfd.events &= ~POLLOUT;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2011-03-16 23:57:42 +01:00
|
|
|
fde->state = (fde->state & FDE_STATEMASK) | events;
|
|
|
|
}
|
|
|
|
|
2015-09-05 01:19:56 +02:00
|
|
|
void fdevent_set(fdevent* fde, unsigned events) {
|
2015-10-31 02:37:26 +01:00
|
|
|
check_main_thread();
|
2015-09-05 01:19:56 +02:00
|
|
|
events &= FDE_EVENTMASK;
|
|
|
|
if ((fde->state & FDE_EVENTMASK) == events) {
|
|
|
|
return;
|
2011-03-16 23:57:42 +01:00
|
|
|
}
|
2015-09-30 03:33:38 +02:00
|
|
|
CHECK(fde->state & FDE_ACTIVE);
|
|
|
|
fdevent_update(fde, events);
|
|
|
|
D("fdevent_set: %s, events = %u", dump_fde(fde).c_str(), events);
|
2015-09-05 01:19:56 +02:00
|
|
|
|
2015-09-30 03:33:38 +02:00
|
|
|
if (fde->state & FDE_PENDING) {
|
|
|
|
// If we are pending, make sure we don't signal an event that is no longer wanted.
|
|
|
|
fde->events &= events;
|
|
|
|
if (fde->events == 0) {
|
|
|
|
g_pending_list.remove(fde);
|
|
|
|
fde->state &= ~FDE_PENDING;
|
2011-03-16 23:57:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-09-05 01:19:56 +02:00
|
|
|
void fdevent_add(fdevent* fde, unsigned events) {
|
2015-10-31 02:37:26 +01:00
|
|
|
check_main_thread();
|
2015-09-05 01:19:56 +02:00
|
|
|
fdevent_set(fde, (fde->state & FDE_EVENTMASK) | events);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-09-05 01:19:56 +02:00
|
|
|
void fdevent_del(fdevent* fde, unsigned events) {
|
2015-10-31 02:37:26 +01:00
|
|
|
check_main_thread();
|
2015-09-05 01:19:56 +02:00
|
|
|
fdevent_set(fde, (fde->state & FDE_EVENTMASK) & ~events);
|
|
|
|
}
|
2009-05-18 17:36:28 +02:00
|
|
|
|
2016-02-17 02:34:53 +01:00
|
|
|
static std::string dump_pollfds(const std::vector<adb_pollfd>& pollfds) {
|
2015-09-05 01:19:56 +02:00
|
|
|
std::string result;
|
2015-10-08 00:59:35 +02:00
|
|
|
for (const auto& pollfd : pollfds) {
|
2015-09-05 01:19:56 +02:00
|
|
|
std::string op;
|
|
|
|
if (pollfd.events & POLLIN) {
|
|
|
|
op += "R";
|
|
|
|
}
|
|
|
|
if (pollfd.events & POLLOUT) {
|
|
|
|
op += "W";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-05 01:19:56 +02:00
|
|
|
android::base::StringAppendF(&result, " %d(%s)", pollfd.fd, op.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-05 01:19:56 +02:00
|
|
|
return result;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-09-05 01:19:56 +02:00
|
|
|
static void fdevent_process() {
|
2016-02-17 02:34:53 +01:00
|
|
|
std::vector<adb_pollfd> pollfds;
|
2015-10-08 00:59:35 +02:00
|
|
|
for (const auto& pair : g_poll_node_map) {
|
|
|
|
pollfds.push_back(pair.second.pollfd);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-05 01:19:56 +02:00
|
|
|
CHECK_GT(pollfds.size(), 0u);
|
|
|
|
D("poll(), pollfds = %s", dump_pollfds(pollfds).c_str());
|
2016-02-17 02:34:53 +01:00
|
|
|
int ret = adb_poll(&pollfds[0], pollfds.size(), -1);
|
2015-09-05 01:19:56 +02:00
|
|
|
if (ret == -1) {
|
2015-09-16 01:27:09 +02:00
|
|
|
PLOG(ERROR) << "poll(), ret = " << ret;
|
|
|
|
return;
|
2015-09-05 01:19:56 +02:00
|
|
|
}
|
2015-10-08 00:59:35 +02:00
|
|
|
for (const auto& pollfd : pollfds) {
|
2015-09-16 01:27:09 +02:00
|
|
|
if (pollfd.revents != 0) {
|
|
|
|
D("for fd %d, revents = %x", pollfd.fd, pollfd.revents);
|
|
|
|
}
|
2015-09-05 01:19:56 +02:00
|
|
|
unsigned events = 0;
|
|
|
|
if (pollfd.revents & POLLIN) {
|
|
|
|
events |= FDE_READ;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-05 01:19:56 +02:00
|
|
|
if (pollfd.revents & POLLOUT) {
|
|
|
|
events |= FDE_WRITE;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-05 01:19:56 +02:00
|
|
|
if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
|
|
|
// We fake a read, as the rest of the code assumes that errors will
|
|
|
|
// be detected at that point.
|
|
|
|
events |= FDE_READ | FDE_ERROR;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-09-29 21:25:33 +02:00
|
|
|
#if defined(__linux__)
|
|
|
|
if (pollfd.revents & POLLRDHUP) {
|
|
|
|
events |= FDE_READ | FDE_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
2015-09-05 01:19:56 +02:00
|
|
|
if (events != 0) {
|
|
|
|
auto it = g_poll_node_map.find(pollfd.fd);
|
|
|
|
CHECK(it != g_poll_node_map.end());
|
|
|
|
fdevent* fde = it->second.fde;
|
2018-05-14 22:42:49 +02:00
|
|
|
CHECK_EQ(fde->fd.get(), pollfd.fd);
|
2015-09-05 01:19:56 +02:00
|
|
|
fde->events |= events;
|
|
|
|
D("%s got events %x", dump_fde(fde).c_str(), events);
|
|
|
|
fde->state |= FDE_PENDING;
|
|
|
|
g_pending_list.push_back(fde);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 23:10:39 +02:00
|
|
|
static void fdevent_call_fdfunc(fdevent* fde) {
|
2011-03-16 23:57:42 +01:00
|
|
|
unsigned events = fde->events;
|
|
|
|
fde->events = 0;
|
2015-09-30 03:33:38 +02:00
|
|
|
CHECK(fde->state & FDE_PENDING);
|
2011-03-16 23:57:42 +01:00
|
|
|
fde->state &= (~FDE_PENDING);
|
2015-09-05 01:19:56 +02:00
|
|
|
D("fdevent_call_fdfunc %s", dump_fde(fde).c_str());
|
2018-05-14 22:42:49 +02:00
|
|
|
fde->func(fde->fd.get(), events, fde->arg);
|
2011-03-16 23:57:42 +01:00
|
|
|
}
|
|
|
|
|
2018-02-23 23:37:07 +01:00
|
|
|
static void fdevent_run_flush() EXCLUDES(run_queue_mutex) {
|
|
|
|
// We need to be careful around reentrancy here, since a function we call can queue up another
|
|
|
|
// function.
|
|
|
|
while (true) {
|
|
|
|
std::function<void()> fn;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(run_queue_mutex);
|
|
|
|
if (run_queue.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fn = run_queue.front();
|
|
|
|
run_queue.pop_front();
|
|
|
|
}
|
|
|
|
fn();
|
2017-05-03 23:10:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fdevent_run_func(int fd, unsigned ev, void* /* userdata */) {
|
|
|
|
CHECK_GE(fd, 0);
|
|
|
|
CHECK(ev & FDE_READ);
|
|
|
|
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
// Empty the fd.
|
|
|
|
if (adb_read(fd, buf, sizeof(buf)) == -1) {
|
|
|
|
PLOG(FATAL) << "failed to empty run queue notify fd";
|
|
|
|
}
|
|
|
|
|
2018-03-30 01:23:43 +02:00
|
|
|
// Mark that we need to flush, and then run it at the end of fdevent_loop.
|
|
|
|
run_needs_flush = true;
|
2017-05-03 23:10:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fdevent_run_setup() {
|
2018-02-23 23:37:07 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(run_queue_mutex);
|
|
|
|
CHECK(run_queue_notify_fd.get() == -1);
|
|
|
|
int s[2];
|
|
|
|
if (adb_socketpair(s) != 0) {
|
|
|
|
PLOG(FATAL) << "failed to create run queue notify socketpair";
|
|
|
|
}
|
2017-05-03 23:10:39 +02:00
|
|
|
|
2018-03-19 23:19:45 +01:00
|
|
|
if (!set_file_block_mode(s[0], false) || !set_file_block_mode(s[1], false)) {
|
|
|
|
PLOG(FATAL) << "failed to make run queue notify socket nonblocking";
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:37:07 +01:00
|
|
|
run_queue_notify_fd.reset(s[0]);
|
|
|
|
fdevent* fde = fdevent_create(s[1], fdevent_run_func, nullptr);
|
|
|
|
CHECK(fde != nullptr);
|
|
|
|
fdevent_add(fde, FDE_READ);
|
|
|
|
}
|
2017-05-03 23:10:39 +02:00
|
|
|
|
|
|
|
fdevent_run_flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void fdevent_run_on_main_thread(std::function<void()> fn) {
|
|
|
|
std::lock_guard<std::mutex> lock(run_queue_mutex);
|
|
|
|
run_queue.push_back(std::move(fn));
|
|
|
|
|
|
|
|
// run_queue_notify_fd could still be -1 if we're called before fdevent has finished setting up.
|
|
|
|
// In that case, rely on the setup code to flush the queue without a notification being needed.
|
|
|
|
if (run_queue_notify_fd != -1) {
|
2018-03-19 23:19:45 +01:00
|
|
|
int rc = adb_write(run_queue_notify_fd.get(), "", 1);
|
|
|
|
|
|
|
|
// It's possible that we get EAGAIN here, if lots of notifications came in while handling.
|
|
|
|
if (rc == 0) {
|
|
|
|
PLOG(FATAL) << "run queue notify fd was closed?";
|
|
|
|
} else if (rc == -1 && errno != EAGAIN) {
|
2017-05-03 23:10:39 +02:00
|
|
|
PLOG(FATAL) << "failed to write to run queue notify fd";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fdevent_loop() {
|
2015-10-31 02:37:26 +01:00
|
|
|
set_main_thread();
|
2017-05-03 23:10:39 +02:00
|
|
|
fdevent_run_setup();
|
2009-05-18 17:36:28 +02:00
|
|
|
|
2015-08-03 19:38:08 +02:00
|
|
|
while (true) {
|
2016-02-10 23:49:00 +01:00
|
|
|
if (terminate_loop) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-05 01:19:56 +02:00
|
|
|
D("--- --- waiting for events");
|
2011-03-16 23:57:42 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
fdevent_process();
|
2009-05-18 17:36:28 +02:00
|
|
|
|
2015-09-05 01:19:56 +02:00
|
|
|
while (!g_pending_list.empty()) {
|
|
|
|
fdevent* fde = g_pending_list.front();
|
|
|
|
g_pending_list.pop_front();
|
2011-03-16 23:57:42 +01:00
|
|
|
fdevent_call_fdfunc(fde);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-03-30 01:23:43 +02:00
|
|
|
|
|
|
|
if (run_needs_flush) {
|
|
|
|
fdevent_run_flush();
|
|
|
|
run_needs_flush = false;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-16 01:27:09 +02:00
|
|
|
|
2016-02-10 23:49:00 +01:00
|
|
|
void fdevent_terminate_loop() {
|
|
|
|
terminate_loop = true;
|
|
|
|
}
|
|
|
|
|
2015-09-16 01:27:09 +02:00
|
|
|
size_t fdevent_installed_count() {
|
|
|
|
return g_poll_node_map.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void fdevent_reset() {
|
|
|
|
g_poll_node_map.clear();
|
|
|
|
g_pending_list.clear();
|
2017-05-03 23:10:39 +02:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(run_queue_mutex);
|
|
|
|
run_queue_notify_fd.reset();
|
|
|
|
run_queue.clear();
|
|
|
|
|
2015-10-31 02:37:26 +01:00
|
|
|
main_thread_valid = false;
|
2016-02-10 23:49:00 +01:00
|
|
|
terminate_loop = false;
|
2015-09-16 01:27:09 +02:00
|
|
|
}
|