{"action":"created","comment":{"url":"https://api.github.com/repos/ArduPilot/ardupilot/pulls/comments/63463497","id":63463497,"diff_hunk":"@@ -0,0 +1,403 @@\n+/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-\n+/*\n+ * Copyright (C) 2016  Intel Corporation. All rights reserved.\n+ *\n+ * This file is free software: you can redistribute it and/or modify it\n+ * under the terms of the GNU General Public License as published by the\n+ * Free Software Foundation, either version 3 of the License, or\n+ * (at your option) any later version.\n+ *\n+ * This file is distributed in the hope that it will be useful, but\n+ * WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n+ * See the GNU General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU General Public License along\n+ * with this program.  If not, see <http://www.gnu.org/licenses/>.\n+ */\n+#include <algorithm>\n+#include <arpa/inet.h>\n+#include <cstddef>\n+#include <errno.h>\n+#include <fcntl.h>\n+#include <netinet/in.h>\n+#include <netinet/tcp.h>\n+#include <pthread.h>\n+#include <stdint.h>\n+#include <sys/epoll.h>\n+#include <sys/socket.h>\n+#include <sys/types.h>\n+#include <unistd.h>\n+\n+#include \"Poller.h\"\n+#include \"Scheduler.h\"\n+\n+extern const AP_HAL::HAL& hal;\n+\n+namespace Linux {\n+\n+uint32_t Poller::to_epoll_events(Poller::Event ev) {\n+    // EPOLLWAKEUP prevents the system from hibernating or suspending when\n+    // inside epoll_wait() for this particular event.  It is silently\n+    // ignored if the process does not have the CAP_BLOCK_SUSPEND\n+    // capability.\n+    uint32_t op = EPOLLWAKEUP;\n+\n+    if (ev & Poller::Event::Read) {\n+        op |= EPOLLIN;\n+    }\n+    if (ev & Poller::Event::Write) {\n+        op |= EPOLLOUT;\n+    }\n+    if (ev & Poller::Event::Error) {\n+        op |= EPOLLERR;\n+    }\n+\n+    return op;\n+}\n+\n+bool Poller::register_pollable(Pollable *p, const Poller::Event ev) {\n+    if (_epfd < 0) {\n+        return false;\n+    }\n+\n+    struct epoll_event epev = {0};\n+    epev.events = Poller::to_epoll_events(ev);\n+    epev.data.ptr = static_cast<void *>(p);\n+\n+    return epoll_ctl(_epfd, EPOLL_CTL_ADD, p->get_fd(), &epev) == 0;\n+}\n+\n+void Poller::unregister_pollable(const Pollable *p) {\n+    if (_epfd >= 0) {\n+        epoll_ctl(_epfd, EPOLL_CTL_DEL, p->get_fd(), NULL);\n+    }\n+}\n+\n+int Poller::poll(int timeout_ms) const {\n+    const int max_events = 16;\n+    epoll_event events[max_events];\n+    auto before_wait = AP_HAL::millis();\n+    int r = epoll_wait(_epfd, events, max_events, timeout_ms);\n+\n+    if (r > 0) {\n+        const auto delta = AP_HAL::millis() - before_wait;\n+        const auto max_time_ms = std::max((timeout_ms - delta) / r, 1U);\n+\n+        for (int i = 0; i < r; i++) {\n+            Pollable *p = static_cast<Pollable *>(events[i].data.ptr);\n+\n+            if (events[i].events & EPOLLIN) {\n+                p->on_can_read(max_time_ms);\n+            }\n+            if (events[i].events & EPOLLOUT) {\n+                p->on_can_write(max_time_ms);\n+            }\n+            if (events[i].events & EPOLLERR) {\n+                p->on_error(max_time_ms);\n+            }\n+            if (events[i].events & EPOLLHUP) {\n+                p->on_hang_up(max_time_ms);\n+            }\n+        }\n+    } else if (r < 0) {\n+        if (errno == EINTR) {\n+            // A possible nudge from another thread; return 0 to signal\n+            // tmout.\n+            r = 0;\n+        }\n+    }\n+\n+    return r;\n+}\n+\n+Pollable::~Pollable() {\n+    auto scheduler = Linux::Scheduler::from(hal.scheduler);\n+    scheduler->unregister_pollable(this);\n+\n+    close(_fd);\n+}\n+\n+bool Pollable::set_blocking(bool setting) {\n+    auto curflags = fcntl(_fd, F_GETFL, 0);\n+\n+    if (curflags < 0) {\n+        return false;\n+    }\n+\n+    if (setting) {\n+        curflags &= ~O_NONBLOCK;\n+    } else {\n+        curflags |= O_NONBLOCK;\n+    }\n+\n+    return fcntl(_fd, F_SETFL, curflags) == 0;\n+}\n+\n+void BufferedPollable::on_can_read(int max_time_ms) {\n+    if (!_read_sem.take(max_time_ms)) {\n+        return;\n+    }\n+\n+    auto space = _read_buffer.space();\n+    uint8_t buf[space];\n+\n+    auto r = read(_fd, buf, space);\n+    if (r > 0) {\n+        _read_buffer.write(buf, static_cast<uint32_t>(r));","path":"libraries/AP_HAL_Linux/Poller.cpp","position":148,"original_position":147,"commit_id":"b47387dc9b1b86f2c2f82470a84396b10f1917c7","original_commit_id":"648a2a9e180369e7302dcb65609d9d526c91b268","user":{"login":"lucasdemarchi","id":31864,"avatar_url":"https://avatars.githubusercontent.com/u/31864?v=3","gravatar_id":"","url":"https://api.github.com/users/lucasdemarchi","html_url":"https://github.com/lucasdemarchi","followers_url":"https://api.github.com/users/lucasdemarchi/followers","following_url":"https://api.github.com/users/lucasdemarchi/following{/other_user}","gists_url":"https://api.github.com/users/lucasdemarchi/gists{/gist_id}","starred_url":"https://api.github.com/users/lucasdemarchi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lucasdemarchi/subscriptions","organizations_url":"https://api.github.com/users/lucasdemarchi/orgs","repos_url":"https://api.github.com/users/lucasdemarchi/repos","events_url":"https://api.github.com/users/lucasdemarchi/events{/privacy}","received_events_url":"https://api.github.com/users/lucasdemarchi/received_events","type":"User","site_admin":false},"body":"Can we have an interface in which a call to space() reserves that space and read copies directly there? This way we don't need the double copy.","created_at":"2016-05-17T04:37:44Z","updated_at":"2016-05-17T04:37:44Z","html_url":"https://github.com/ArduPilot/ardupilot/pull/4024#discussion_r63463497","pull_request_url":"https://api.github.com/repos/ArduPilot/ardupilot/pulls/4024","_links":{"self":{"href":"https://api.github.com/repos/ArduPilot/ardupilot/pulls/comments/63463497"},"html":{"href":"https://github.com/ArduPilot/ardupilot/pull/4024#discussion_r63463497"},"pull_request":{"href":"https://api.github.com/repos/ArduPilot/ardupilot/pulls/4024"}}},"pull_request":{"url":"https://api.github.com/repos/ArduPilot/ardupilot/pulls/4024","id":68620965,"html_url":"https://github.com/ArduPilot/ardupilot/pull/4024","diff_url":"https://github.com/ArduPilot/ardupilot/pull/4024.diff","patch_url":"https://github.com/ArduPilot/ardupilot/pull/4024.patch","issue_url":"https://api.github.com/repos/ArduPilot/ardupilot/issues/4024","number":4024,"state":"open","locked":false,"title":"AP_HAL_Linux: Refactor UARTDriver to use epoll","user":{"login":"lpereira","id":15001,"avatar_url":"https://avatars.githubusercontent.com/u/15001?v=3","gravatar_id":"","url":"https://api.github.com/users/lpereira","html_url":"https://github.com/lpereira","followers_url":"https://api.github.com/users/lpereira/followers","following_url":"https://api.github.com/users/lpereira/following{/other_user}","gists_url":"https://api.github.com/users/lpereira/gists{/gist_id}","starred_url":"https://api.github.com/users/lpereira/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lpereira/subscriptions","organizations_url":"https://api.github.com/users/lpereira/orgs","repos_url":"https://api.github.com/users/lpereira/repos","events_url":"https://api.github.com/users/lpereira/events{/privacy}","received_events_url":"https://api.github.com/users/lpereira/received_events","type":"User","site_admin":false},"body":"This is a step in the direction of moving off of I/O with timer ticks to use the system's polling infrastructure to be notified whenever a device is ready to be read from or written to.\r\n\r\nThis removes a lot of code from UARTDriver, and introduces a few helper classes:\r\n        * Poller, as an interface to epoll()\r\n        * Pollable, as an interface to a file descriptor\r\n        * BufferedPollable, that has read and write ring buffers\r\n        * PacketedBufferedPollable, that's used for MAVLink packets\r\n\r\nThis creates a new thread that does the call to epoll_wait(); the timeout is fixed, but the remaining time is evenly split between each Pollable so that the maximum time they should wait to lock their semaphores is fairer.\r\n\r\nThe `packetise` flag from UARTDriver has been removed, and the write()-call-per-MAVLink-message has been slightly improved from a performance perspective (fast byte scans, etc), and improved from a readability perspective (ByteBuffer from AP_HAL is used instead of an open coded version).\r\n\r\nInterfaces still need some improvement from a readability perspective.\r\n\r\nThis has not been flight-tested yet but strace seems sane enough.\r\n","created_at":"2016-05-02T21:02:45Z","updated_at":"2016-05-17T04:37:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5509f3231d28a4e336ec3fff9f5b4d412ce0e0a2","assignee":{"login":"lucasdemarchi","id":31864,"avatar_url":"https://avatars.githubusercontent.com/u/31864?v=3","gravatar_id":"","url":"https://api.github.com/users/lucasdemarchi","html_url":"https://github.com/lucasdemarchi","followers_url":"https://api.github.com/users/lucasdemarchi/followers","following_url":"https://api.github.com/users/lucasdemarchi/following{/other_user}","gists_url":"https://api.github.com/users/lucasdemarchi/gists{/gist_id}","starred_url":"https://api.github.com/users/lucasdemarchi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lucasdemarchi/subscriptions","organizations_url":"https://api.github.com/users/lucasdemarchi/orgs","repos_url":"https://api.github.com/users/lucasdemarchi/repos","events_url":"https://api.github.com/users/lucasdemarchi/events{/privacy}","received_events_url":"https://api.github.com/users/lucasdemarchi/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/ArduPilot/ardupilot/pulls/4024/commits","review_comments_url":"https://api.github.com/repos/ArduPilot/ardupilot/pulls/4024/comments","review_comment_url":"https://api.github.com/repos/ArduPilot/ardupilot/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ArduPilot/ardupilot/issues/4024/comments","statuses_url":"https://api.github.com/repos/ArduPilot/ardupilot/statuses/b47387dc9b1b86f2c2f82470a84396b10f1917c7","head":{"label":"lpereira:epoll","ref":"epoll","sha":"b47387dc9b1b86f2c2f82470a84396b10f1917c7","user":{"login":"lpereira","id":15001,"avatar_url":"https://avatars.githubusercontent.com/u/15001?v=3","gravatar_id":"","url":"https://api.github.com/users/lpereira","html_url":"https://github.com/lpereira","followers_url":"https://api.github.com/users/lpereira/followers","following_url":"https://api.github.com/users/lpereira/following{/other_user}","gists_url":"https://api.github.com/users/lpereira/gists{/gist_id}","starred_url":"https://api.github.com/users/lpereira/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lpereira/subscriptions","organizations_url":"https://api.github.com/users/lpereira/orgs","repos_url":"https://api.github.com/users/lpereira/repos","events_url":"https://api.github.com/users/lpereira/events{/privacy}","received_events_url":"https://api.github.com/users/lpereira/received_events","type":"User","site_admin":false},"repo":{"id":57894089,"name":"ardupilot","full_name":"lpereira/ardupilot","owner":{"login":"lpereira","id":15001,"avatar_url":"https://avatars.githubusercontent.com/u/15001?v=3","gravatar_id":"","url":"https://api.github.com/users/lpereira","html_url":"https://github.com/lpereira","followers_url":"https://api.github.com/users/lpereira/followers","following_url":"https://api.github.com/users/lpereira/following{/other_user}","gists_url":"https://api.github.com/users/lpereira/gists{/gist_id}","starred_url":"https://api.github.com/users/lpereira/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lpereira/subscriptions","organizations_url":"https://api.github.com/users/lpereira/orgs","repos_url":"https://api.github.com/users/lpereira/repos","events_url":"https://api.github.com/users/lpereira/events{/privacy}","received_events_url":"https://api.github.com/users/lpereira/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/lpereira/ardupilot","description":"ArduPlane, ArduCopter, ArduRover source","fork":true,"url":"https://api.github.com/repos/lpereira/ardupilot","forks_url":"https://api.github.com/repos/lpereira/ardupilot/forks","keys_url":"https://api.github.com/repos/lpereira/ardupilot/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lpereira/ardupilot/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lpereira/ardupilot/teams","hooks_url":"https://api.github.com/repos/lpereira/ardupilot/hooks","issue_events_url":"https://api.github.com/repos/lpereira/ardupilot/issues/events{/number}","events_url":"https://api.github.com/repos/lpereira/ardupilot/events","assignees_url":"https://api.github.com/repos/lpereira/ardupilot/assignees{/user}","branches_url":"https://api.github.com/repos/lpereira/ardupilot/branches{/branch}","tags_url":"https://api.github.com/repos/lpereira/ardupilot/tags","blobs_url":"https://api.github.com/repos/lpereira/ardupilot/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lpereira/ardupilot/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lpereira/ardupilot/git/refs{/sha}","trees_url":"https://api.github.com/repos/lpereira/ardupilot/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lpereira/ardupilot/statuses/{sha}","languages_url":"https://api.github.com/repos/lpereira/ardupilot/languages","stargazers_url":"https://api.github.com/repos/lpereira/ardupilot/stargazers","contributors_url":"https://api.github.com/repos/lpereira/ardupilot/contributors","subscribers_url":"https://api.github.com/repos/lpereira/ardupilot/subscribers","subscription_url":"https://api.github.com/repos/lpereira/ardupilot/subscription","commits_url":"https://api.github.com/repos/lpereira/ardupilot/commits{/sha}","git_commits_url":"https://api.github.com/repos/lpereira/ardupilot/git/commits{/sha}","comments_url":"https://api.github.com/repos/lpereira/ardupilot/comments{/number}","issue_comment_url":"https://api.github.com/repos/lpereira/ardupilot/issues/comments{/number}","contents_url":"https://api.github.com/repos/lpereira/ardupilot/contents/{+path}","compare_url":"https://api.github.com/repos/lpereira/ardupilot/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lpereira/ardupilot/merges","archive_url":"https://api.github.com/repos/lpereira/ardupilot/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lpereira/ardupilot/downloads","issues_url":"https://api.github.com/repos/lpereira/ardupilot/issues{/number}","pulls_url":"https://api.github.com/repos/lpereira/ardupilot/pulls{/number}","milestones_url":"https://api.github.com/repos/lpereira/ardupilot/milestones{/number}","notifications_url":"https://api.github.com/repos/lpereira/ardupilot/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lpereira/ardupilot/labels{/name}","releases_url":"https://api.github.com/repos/lpereira/ardupilot/releases{/id}","deployments_url":"https://api.github.com/repos/lpereira/ardupilot/deployments","created_at":"2016-05-02T13:58:47Z","updated_at":"2016-05-02T13:58:58Z","pushed_at":"2016-05-11T16:31:58Z","git_url":"git://github.com/lpereira/ardupilot.git","ssh_url":"git@github.com:lpereira/ardupilot.git","clone_url":"https://github.com/lpereira/ardupilot.git","svn_url":"https://github.com/lpereira/ardupilot","homepage":"http://ardupilot.org/","size":73672,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"ArduPilot:master","ref":"master","sha":"7142bacfaa789be31bb1c50f4af838764d4cd24f","user":{"login":"ArduPilot","id":17919847,"avatar_url":"https://avatars.githubusercontent.com/u/17919847?v=3","gravatar_id":"","url":"https://api.github.com/users/ArduPilot","html_url":"https://github.com/ArduPilot","followers_url":"https://api.github.com/users/ArduPilot/followers","following_url":"https://api.github.com/users/ArduPilot/following{/other_user}","gists_url":"https://api.github.com/users/ArduPilot/gists{/gist_id}","starred_url":"https://api.github.com/users/ArduPilot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ArduPilot/subscriptions","organizations_url":"https://api.github.com/users/ArduPilot/orgs","repos_url":"https://api.github.com/users/ArduPilot/repos","events_url":"https://api.github.com/users/ArduPilot/events{/privacy}","received_events_url":"https://api.github.com/users/ArduPilot/received_events","type":"Organization","site_admin":false},"repo":{"id":7512484,"name":"ardupilot","full_name":"ArduPilot/ardupilot","owner":{"login":"ArduPilot","id":17919847,"avatar_url":"https://avatars.githubusercontent.com/u/17919847?v=3","gravatar_id":"","url":"https://api.github.com/users/ArduPilot","html_url":"https://github.com/ArduPilot","followers_url":"https://api.github.com/users/ArduPilot/followers","following_url":"https://api.github.com/users/ArduPilot/following{/other_user}","gists_url":"https://api.github.com/users/ArduPilot/gists{/gist_id}","starred_url":"https://api.github.com/users/ArduPilot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ArduPilot/subscriptions","organizations_url":"https://api.github.com/users/ArduPilot/orgs","repos_url":"https://api.github.com/users/ArduPilot/repos","events_url":"https://api.github.com/users/ArduPilot/events{/privacy}","received_events_url":"https://api.github.com/users/ArduPilot/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ArduPilot/ardupilot","description":"ArduPlane, ArduCopter, ArduRover source","fork":false,"url":"https://api.github.com/repos/ArduPilot/ardupilot","forks_url":"https://api.github.com/repos/ArduPilot/ardupilot/forks","keys_url":"https://api.github.com/repos/ArduPilot/ardupilot/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ArduPilot/ardupilot/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ArduPilot/ardupilot/teams","hooks_url":"https://api.github.com/repos/ArduPilot/ardupilot/hooks","issue_events_url":"https://api.github.com/repos/ArduPilot/ardupilot/issues/events{/number}","events_url":"https://api.github.com/repos/ArduPilot/ardupilot/events","assignees_url":"https://api.github.com/repos/ArduPilot/ardupilot/assignees{/user}","branches_url":"https://api.github.com/repos/ArduPilot/ardupilot/branches{/branch}","tags_url":"https://api.github.com/repos/ArduPilot/ardupilot/tags","blobs_url":"https://api.github.com/repos/ArduPilot/ardupilot/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ArduPilot/ardupilot/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ArduPilot/ardupilot/git/refs{/sha}","trees_url":"https://api.github.com/repos/ArduPilot/ardupilot/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ArduPilot/ardupilot/statuses/{sha}","languages_url":"https://api.github.com/repos/ArduPilot/ardupilot/languages","stargazers_url":"https://api.github.com/repos/ArduPilot/ardupilot/stargazers","contributors_url":"https://api.github.com/repos/ArduPilot/ardupilot/contributors","subscribers_url":"https://api.github.com/repos/ArduPilot/ardupilot/subscribers","subscription_url":"https://api.github.com/repos/ArduPilot/ardupilot/subscription","commits_url":"https://api.github.com/repos/ArduPilot/ardupilot/commits{/sha}","git_commits_url":"https://api.github.com/repos/ArduPilot/ardupilot/git/commits{/sha}","comments_url":"https://api.github.com/repos/ArduPilot/ardupilot/comments{/number}","issue_comment_url":"https://api.github.com/repos/ArduPilot/ardupilot/issues/comments{/number}","contents_url":"https://api.github.com/repos/ArduPilot/ardupilot/contents/{+path}","compare_url":"https://api.github.com/repos/ArduPilot/ardupilot/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ArduPilot/ardupilot/merges","archive_url":"https://api.github.com/repos/ArduPilot/ardupilot/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ArduPilot/ardupilot/downloads","issues_url":"https://api.github.com/repos/ArduPilot/ardupilot/issues{/number}","pulls_url":"https://api.github.com/repos/ArduPilot/ardupilot/pulls{/number}","milestones_url":"https://api.github.com/repos/ArduPilot/ardupilot/milestones{/number}","notifications_url":"https://api.github.com/repos/ArduPilot/ardupilot/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ArduPilot/ardupilot/labels{/name}","releases_url":"https://api.github.com/repos/ArduPilot/ardupilot/releases{/id}","deployments_url":"https://api.github.com/repos/ArduPilot/ardupilot/deployments","created_at":"2013-01-09T00:58:52Z","updated_at":"2016-05-17T04:22:45Z","pushed_at":"2016-05-17T01:36:07Z","git_url":"git://github.com/ArduPilot/ardupilot.git","ssh_url":"git@github.com:ArduPilot/ardupilot.git","clone_url":"https://github.com/ArduPilot/ardupilot.git","svn_url":"https://github.com/ArduPilot/ardupilot","homepage":"http://ardupilot.org/","size":133230,"stargazers_count":1849,"watchers_count":1849,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3897,"mirror_url":null,"open_issues_count":734,"forks":3897,"open_issues":734,"watchers":1849,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ArduPilot/ardupilot/pulls/4024"},"html":{"href":"https://github.com/ArduPilot/ardupilot/pull/4024"},"issue":{"href":"https://api.github.com/repos/ArduPilot/ardupilot/issues/4024"},"comments":{"href":"https://api.github.com/repos/ArduPilot/ardupilot/issues/4024/comments"},"review_comments":{"href":"https://api.github.com/repos/ArduPilot/ardupilot/pulls/4024/comments"},"review_comment":{"href":"https://api.github.com/repos/ArduPilot/ardupilot/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ArduPilot/ardupilot/pulls/4024/commits"},"statuses":{"href":"https://api.github.com/repos/ArduPilot/ardupilot/statuses/b47387dc9b1b86f2c2f82470a84396b10f1917c7"}}}}