~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/thermal/lib/mainloop.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: LGPL-2.1+
  2 // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
  3 #include <stdlib.h>
  4 #include <errno.h>
  5 #include <unistd.h>
  6 #include <signal.h>
  7 #include <sys/epoll.h>
  8 #include "mainloop.h"
  9 #include "log.h"
 10 
 11 static int epfd = -1;
 12 static sig_atomic_t exit_mainloop;
 13 
 14 struct mainloop_data {
 15         mainloop_callback_t cb;
 16         void *data;
 17         int fd;
 18 };
 19 
 20 #define MAX_EVENTS 10
 21 
 22 int mainloop(unsigned int timeout)
 23 {
 24         int i, nfds;
 25         struct epoll_event events[MAX_EVENTS];
 26         struct mainloop_data *md;
 27 
 28         if (epfd < 0)
 29                 return -1;
 30 
 31         for (;;) {
 32 
 33                 nfds = epoll_wait(epfd, events, MAX_EVENTS, timeout);
 34 
 35                 if (exit_mainloop || !nfds)
 36                         return 0;
 37 
 38                 if (nfds < 0) {
 39                         if (errno == EINTR)
 40                                 continue;
 41                         return -1;
 42                 }
 43 
 44                 for (i = 0; i < nfds; i++) {
 45                         md = events[i].data.ptr;
 46 
 47                         if (md->cb(md->fd, md->data) > 0)
 48                                 return 0;
 49                 }
 50         }
 51 }
 52 
 53 int mainloop_add(int fd, mainloop_callback_t cb, void *data)
 54 {
 55         struct epoll_event ev = {
 56                 .events = EPOLLIN,
 57         };
 58 
 59         struct mainloop_data *md;
 60 
 61         md = malloc(sizeof(*md));
 62         if (!md)
 63                 return -1;
 64 
 65         md->data = data;
 66         md->cb = cb;
 67         md->fd = fd;
 68 
 69         ev.data.ptr = md;
 70 
 71         if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
 72                 free(md);
 73                 return -1;
 74         }
 75 
 76         return 0;
 77 }
 78 
 79 int mainloop_del(int fd)
 80 {
 81         if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) < 0)
 82                 return -1;
 83 
 84         return 0;
 85 }
 86 
 87 int mainloop_init(void)
 88 {
 89         epfd = epoll_create(2);
 90         if (epfd < 0)
 91                 return -1;
 92 
 93         return 0;
 94 }
 95 
 96 void mainloop_exit(void)
 97 {
 98         exit_mainloop = 1;
 99 }
100 
101 void mainloop_fini(void)
102 {
103         close(epfd);
104 }
105 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php