Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 1 | #include <gdk/gdk.h> |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 2 | #include <gtk/gtk.h> |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 3 | #include <sys/epoll.h> |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 4 | #include <condition_variable> |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 5 | #include <mutex> |
| 6 | #include <thread> |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 7 | |
| 8 | #include "aos/vision/events/epoll_events.h" |
| 9 | |
| 10 | namespace aos { |
| 11 | namespace events { |
| 12 | |
| 13 | void EpollLoop::RunWithGtkMain() { |
| 14 | int timeout; |
| 15 | static constexpr size_t kNumberOfEvents = 64; |
| 16 | epoll_event events[kNumberOfEvents]; |
| 17 | int number_events = 0; |
| 18 | |
| 19 | std::mutex m; |
| 20 | std::condition_variable cv; |
| 21 | bool all_events_handled = false; |
| 22 | auto handle_cb = [&]() { |
| 23 | { |
| 24 | std::unique_lock<std::mutex> lk(m); |
| 25 | |
| 26 | for (int i = 0; i < number_events; i++) { |
| 27 | EpollEvent *event = static_cast<EpollEvent *>(events[i].data.ptr); |
| 28 | if ((events[i].events & ~(EPOLLIN | EPOLLPRI)) != 0) { |
| 29 | LOG(FATAL, "unexpected epoll events set in %x on %d\n", |
| 30 | events[i].events, event->fd()); |
| 31 | } |
| 32 | event->ReadEvent(); |
| 33 | } |
| 34 | timeout = CalculateTimeout(); |
| 35 | |
| 36 | all_events_handled = true; |
| 37 | } |
| 38 | cv.notify_one(); |
| 39 | }; |
| 40 | handle_cb(); |
| 41 | using HandleCBType = decltype(handle_cb); |
| 42 | |
| 43 | std::thread t([&]() { |
| 44 | std::unique_lock<std::mutex> lk(m); |
| 45 | while (true) { |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 46 | cv.wait(lk, [&all_events_handled] { return all_events_handled; }); |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 47 | // Wait for handle_cb to be done. |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 48 | number_events = |
| 49 | PCHECK(epoll_wait(epoll_fd(), events, kNumberOfEvents, timeout)); |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 50 | all_events_handled = false; |
| 51 | // Trigger handle_cb on main_thread to avoid concurrency. |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame] | 52 | gdk_threads_add_idle( |
| 53 | +[](gpointer user_data) -> gboolean { |
| 54 | auto &handle_cb = *reinterpret_cast<HandleCBType *>(user_data); |
| 55 | handle_cb(); |
| 56 | return G_SOURCE_REMOVE; |
| 57 | }, |
| 58 | &handle_cb); |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 59 | } |
| 60 | }); |
| 61 | gtk_main(); |
| 62 | |
| 63 | // TODO(parker): Allow concurrent proxy onto the normal thread just like Gtk |
| 64 | // in order to allow event loop fusion, and make event addition thread-safe. |
| 65 | |
| 66 | // Avoid stack destructors (explicitly shutting down of the thread.) |
| 67 | exit(EXIT_SUCCESS); |
| 68 | } |
| 69 | |
| 70 | } // namespace events |
| 71 | } // namespace aos |