blob: 56129aa1f2509faf04d1a8739f695223dd0fe2b5 [file] [log] [blame]
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00001/*
2 * Power debug tool (powerdebug)
Daniel Lezcano8be52602011-06-21 00:57:08 +02003 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00004 * Copyright (C) 2016, Linaro Limited.
Daniel Lezcano8be52602011-06-21 00:57:08 +02005 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00006 * 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 Lezcano8be52602011-06-21 00:57:08 +020010 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +000011 * 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 Lezcano8be52602011-06-21 00:57:08 +020015 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +000016 * 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 Lezcano8be52602011-06-21 00:57:08 +020021
22#include <stdlib.h>
23#include <errno.h>
24#include <unistd.h>
25#include <sys/epoll.h>
26#include "mainloop.h"
27
28static int epfd = -1;
29static unsigned short nrhandler;
30
31struct mainloop_data {
32 mainloop_callback_t cb;
33 void *data;
34 int fd;
35};
36
37struct mainloop_data **mds;
38
39#define MAX_EVENTS 10
40
Daniel Lezcanodb145802011-06-21 00:57:08 +020041int mainloop(unsigned int timeout)
Daniel Lezcano8be52602011-06-21 00:57:08 +020042{
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
69int 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
103int 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
116int mainloop_init(void)
117{
118 epfd = epoll_create(2);
119 if (epfd < 0)
120 return -1;
121
122 return 0;
123}
124
125void mainloop_fini(void)
126{
127 close(epfd);
128}