From patchwork Mon Jun 20 22:58:09 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 2090 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 7B6C923E52 for ; Mon, 20 Jun 2011 22:58:52 +0000 (UTC) Received: from mail-vw0-f52.google.com (mail-vw0-f52.google.com [209.85.212.52]) by fiordland.canonical.com (Postfix) with ESMTP id 4BF28A18303 for ; Mon, 20 Jun 2011 22:58:52 +0000 (UTC) Received: by mail-vw0-f52.google.com with SMTP id 16so4124077vws.11 for ; Mon, 20 Jun 2011 15:58:52 -0700 (PDT) Received: by 10.52.100.72 with SMTP id ew8mr814859vdb.247.1308610731985; Mon, 20 Jun 2011 15:58:51 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.52.183.130 with SMTP id em2cs32797vdc; Mon, 20 Jun 2011 15:58:51 -0700 (PDT) Received: by 10.216.61.3 with SMTP id v3mr2021336wec.103.1308610731067; Mon, 20 Jun 2011 15:58:51 -0700 (PDT) Received: from smtp.smtpout.orange.fr (smtp03.smtpout.orange.fr [80.12.242.125]) by mx.google.com with ESMTP id i58si7095027wed.16.2011.06.20.15.58.50; Mon, 20 Jun 2011 15:58:50 -0700 (PDT) Received-SPF: neutral (google.com: 80.12.242.125 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) client-ip=80.12.242.125; Authentication-Results: mx.google.com; spf=neutral (google.com: 80.12.242.125 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) smtp.mail=daniel.lezcano@linaro.org Received: from monster.dhcp.lxc ([92.134.95.191]) by mwinf5d06 with ME id yNyp1g00347kPVY03NypCd; Tue, 21 Jun 2011 00:58:50 +0200 From: Daniel Lezcano To: linaro-dev@lists.linaro.org Subject: [powerdebug 01/17] add a real mainloop Date: Tue, 21 Jun 2011 00:58:09 +0200 Message-Id: <1308610705-23281-1-git-send-email-daniel.lezcano@linaro.org> X-Mailer: git-send-email 1.7.1 Add a mainloop code with registering of the callbacks and co. Signed-off-by: Daniel Lezcano --- Makefile | 3 +- mainloop.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mainloop.h | 22 +++++++++++ 3 files changed, 146 insertions(+), 1 deletions(-) create mode 100644 mainloop.c create mode 100644 mainloop.h diff --git a/Makefile b/Makefile index 1a53121..8d41b24 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,8 @@ MANDIR=/usr/share/man/man8 CFLAGS?=-O1 -g -Wall -Wshadow CC?=gcc -OBJS = powerdebug.o sensor.o clocks.o regulator.o display.o tree.o utils.o +OBJS = powerdebug.o sensor.o clocks.o regulator.o \ + display.o tree.o utils.o mainloop.o default: powerdebug diff --git a/mainloop.c b/mainloop.c new file mode 100644 index 0000000..d63d703 --- /dev/null +++ b/mainloop.c @@ -0,0 +1,122 @@ +/******************************************************************************* + * Copyright (C) 2010, Linaro Limited. + * + * This file is part of PowerDebug. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Author: + * Daniel Lezcano + * + *******************************************************************************/ + +#include +#include +#include +#include +#include "mainloop.h" + +static int epfd = -1; +static unsigned short nrhandler; + +struct mainloop_data { + mainloop_callback_t cb; + void *data; + int fd; +}; + +struct mainloop_data **mds; + +#define MAX_EVENTS 10 + +int _mainloop(unsigned int timeout) +{ + int i, nfds; + struct epoll_event events[MAX_EVENTS]; + struct mainloop_data *md; + + if (epfd < 0) + return -1; + + for (;;) { + + nfds = epoll_wait(epfd, events, MAX_EVENTS, timeout); + if (nfds < 0) { + if (errno == EINTR) + continue; + return -1; + } + + for (i = 0; i < nfds; i++) { + md = events[i].data.ptr; + + if (md->cb(md->fd, md->data) > 0) + return 0; + } + + } +} + +int mainloop_add(int fd, mainloop_callback_t cb, void *data) +{ + struct epoll_event ev = { + .events = EPOLLIN, + }; + + struct mainloop_data *md; + + if (fd >= nrhandler) { + mds = realloc(mds, sizeof(*mds) * (fd + 1)); + if (!mds) + return -1; + nrhandler = fd + 1; + } + + md = malloc(sizeof(*md)); + if (!md) + return -1; + + md->data = data; + md->cb = cb; + md->fd = fd; + + mds[fd] = md; + ev.data.ptr = md; + + if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0) { + free(md); + return -1; + } + + return 0; +} + +int mainloop_del(int fd) +{ + if (fd >= nrhandler) + return -1; + + if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) < 0) + return -1; + + free(mds[fd]); + + return 0; +} + +int mainloop_init(void) +{ + epfd = epoll_create(2); + if (epfd < 0) + return -1; + + return 0; +} + +void mainloop_fini(void) +{ + close(epfd); +} diff --git a/mainloop.h b/mainloop.h new file mode 100644 index 0000000..3b4c253 --- /dev/null +++ b/mainloop.h @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (C) 2010, Linaro Limited. + * + * This file is part of PowerDebug. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Author: + * Daniel Lezcano + * + *******************************************************************************/ + +typedef int (*mainloop_callback_t)(int fd, void *data); + +extern int _mainloop(unsigned int timeout); +extern int mainloop_add(int fd, mainloop_callback_t cb, void *data); +extern int mainloop_del(int fd); +extern int mainloop_init(void); +extern void mainloop_fini(void);