Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Power debug tool (powerdebug) |
Daniel Lezcano | 8be5260 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 3 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 4 | * Copyright (C) 2016, Linaro Limited. |
Daniel Lezcano | 8be5260 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 5 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
Daniel Lezcano | 8be5260 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 10 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Daniel Lezcano | 8be5260 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 15 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | * |
| 20 | */ |
Daniel Lezcano | 8be5260 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <errno.h> |
| 24 | #include <unistd.h> |
| 25 | #include <sys/epoll.h> |
| 26 | #include "mainloop.h" |
| 27 | |
| 28 | static int epfd = -1; |
| 29 | static unsigned short nrhandler; |
| 30 | |
| 31 | struct mainloop_data { |
| 32 | mainloop_callback_t cb; |
| 33 | void *data; |
| 34 | int fd; |
| 35 | }; |
| 36 | |
| 37 | struct mainloop_data **mds; |
| 38 | |
| 39 | #define MAX_EVENTS 10 |
| 40 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 41 | int mainloop(unsigned int timeout) |
Daniel Lezcano | 8be5260 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 42 | { |
| 43 | int i, nfds; |
| 44 | struct epoll_event events[MAX_EVENTS]; |
| 45 | struct mainloop_data *md; |
| 46 | |
| 47 | if (epfd < 0) |
| 48 | return -1; |
| 49 | |
| 50 | for (;;) { |
| 51 | |
| 52 | nfds = epoll_wait(epfd, events, MAX_EVENTS, timeout); |
| 53 | if (nfds < 0) { |
| 54 | if (errno == EINTR) |
| 55 | continue; |
| 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | for (i = 0; i < nfds; i++) { |
| 60 | md = events[i].data.ptr; |
| 61 | |
| 62 | if (md->cb(md->fd, md->data) > 0) |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | int mainloop_add(int fd, mainloop_callback_t cb, void *data) |
| 70 | { |
| 71 | struct epoll_event ev = { |
| 72 | .events = EPOLLIN, |
| 73 | }; |
| 74 | |
| 75 | struct mainloop_data *md; |
| 76 | |
| 77 | if (fd >= nrhandler) { |
| 78 | mds = realloc(mds, sizeof(*mds) * (fd + 1)); |
| 79 | if (!mds) |
| 80 | return -1; |
| 81 | nrhandler = fd + 1; |
| 82 | } |
| 83 | |
| 84 | md = malloc(sizeof(*md)); |
| 85 | if (!md) |
| 86 | return -1; |
| 87 | |
| 88 | md->data = data; |
| 89 | md->cb = cb; |
| 90 | md->fd = fd; |
| 91 | |
| 92 | mds[fd] = md; |
| 93 | ev.data.ptr = md; |
| 94 | |
| 95 | if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0) { |
| 96 | free(md); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | int mainloop_del(int fd) |
| 104 | { |
| 105 | if (fd >= nrhandler) |
| 106 | return -1; |
| 107 | |
| 108 | if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) < 0) |
| 109 | return -1; |
| 110 | |
| 111 | free(mds[fd]); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int mainloop_init(void) |
| 117 | { |
| 118 | epfd = epoll_create(2); |
| 119 | if (epfd < 0) |
| 120 | return -1; |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | void mainloop_fini(void) |
| 126 | { |
| 127 | close(epfd); |
| 128 | } |