[Pommed-commits] r486 - trunk/pommed
jblache at alioth.debian.org
jblache at alioth.debian.org
Fri Jun 6 19:54:55 UTC 2008
Author: jblache
Date: 2008-06-06 19:54:54 +0000 (Fri, 06 Jun 2008)
New Revision: 486
Modified:
trunk/pommed/dbus.c
trunk/pommed/evloop.c
trunk/pommed/evloop.h
trunk/pommed/kbd_auto.c
trunk/pommed/power.c
Log:
Share timers with the same timeout to reduce wakeups.
Modified: trunk/pommed/dbus.c
===================================================================
--- trunk/pommed/dbus.c 2008-06-05 21:22:18 UTC (rev 485)
+++ trunk/pommed/dbus.c 2008-06-06 19:54:54 UTC (rev 486)
@@ -741,17 +741,16 @@
static void
-mbpdbus_reconnect(int fd, uint32_t events)
+mbpdbus_reconnect(int id, uint64_t ticks)
{
int ret;
- uint64_t dummy;
- /* Acknowledge timer */
- read(fd, &dummy, sizeof(dummy));
-
ret = mbpdbus_init();
if (ret == 0)
- evloop_remove_timer(fd);
+ {
+ evloop_remove_timer(id);
+ dbus_timer = -1;
+ }
}
static DBusHandlerResult
Modified: trunk/pommed/evloop.c
===================================================================
--- trunk/pommed/evloop.c 2008-06-05 21:22:18 UTC (rev 485)
+++ trunk/pommed/evloop.c 2008-06-06 19:54:54 UTC (rev 486)
@@ -45,6 +45,10 @@
/* event sources registered on the main loop */
static struct pommed_event *sources;
+/* timers */
+static struct pommed_timer *timers;
+static int timer_job_id;
+
static int running;
@@ -123,9 +127,39 @@
}
-int
-evloop_add_timer(int timeout, pommed_event_cb cb)
+static void
+evloop_timer_callback(int fd, uint32_t events)
{
+ uint64_t ticks;
+
+ struct pommed_timer *t;
+ struct pommed_timer_job *j;
+
+ /* Acknowledge timer */
+ read(fd, &ticks, sizeof(ticks));
+
+ j = NULL;
+ for (t = timers; t != NULL; t = t->next)
+ {
+ if (t->fd == fd)
+ {
+ j = t->jobs;
+
+ break;
+ }
+ }
+
+ while (j != NULL)
+ {
+ j->cb(j->id, ticks);
+
+ j = j->next;
+ }
+}
+
+static int
+evloop_create_timer(int timeout)
+{
int fd;
int ret;
@@ -168,7 +202,7 @@
return -1;
}
- ret = evloop_add(fd, EPOLLIN, cb);
+ ret = evloop_add(fd, EPOLLIN, evloop_timer_callback);
if (ret < 0)
{
close(fd);
@@ -179,16 +213,113 @@
}
int
-evloop_remove_timer(int fd)
+evloop_add_timer(int timeout, pommed_timer_cb cb)
{
+ int fd;
+
+ struct pommed_timer *t;
+ struct pommed_timer_job *j;
+
+ j = (struct pommed_timer_job *)malloc(sizeof(struct pommed_timer_job));
+ if (j == NULL)
+ {
+ logmsg(LOG_ERR, "Could not allocate memory for timer job");
+ return -1;
+ }
+
+ j->cb = cb;
+ j->id = timer_job_id;
+ timer_job_id++;
+
+ for (t = timers; t != NULL; t = t->next)
+ {
+ if (t->timeout == timeout)
+ break;
+ }
+
+ if (t == NULL)
+ {
+ t = (struct pommed_timer *)malloc(sizeof(struct pommed_timer));
+ if (t == NULL)
+ {
+ logmsg(LOG_ERR, "Could not allocate memory for timer");
+ return -1;
+ }
+
+ fd = evloop_create_timer(timeout);
+ if (fd < 0)
+ {
+ free(t);
+ return -1;
+ }
+
+ t->fd = fd;
+ t->timeout = timeout;
+ t->jobs = NULL;
+ t->next = timers;
+ timers = t;
+ }
+
+ j->next = t->jobs;
+ t->jobs = j;
+
+ return 0;
+}
+
+int
+evloop_remove_timer(int id)
+{
+ int found;
int ret;
- ret = evloop_remove(fd);
- if (ret < 0)
- return ret;
+ struct pommed_timer *t;
+ struct pommed_timer *pt;
+ struct pommed_timer_job *j;
+ struct pommed_timer_job *pj;
- close(fd);
+ found = 0;
+ for (pt = NULL, t = timers; t != NULL; pt = t, t = t->next)
+ {
+ for (pj = NULL, j = t->jobs; j != NULL; pj = j, j = j->next)
+ {
+ if (j->id == id)
+ {
+ if (pj != NULL)
+ pj->next = j->next;
+ else
+ t->jobs = j->next;
+ free(j);
+
+ found = 1;
+
+ break;
+ }
+ }
+
+ if (found)
+ break;
+ }
+
+ if (t == NULL)
+ return 0;
+
+ if (t->jobs == NULL)
+ {
+ ret = evloop_remove(t->fd);
+ if (ret < 0)
+ return ret;
+
+ close(t->fd);
+
+ if (pt != NULL)
+ pt->next = t->next;
+ else
+ timers = t->next;
+
+ free(t);
+ }
+
return 0;
}
@@ -239,6 +370,10 @@
evloop_init(void)
{
sources = NULL;
+
+ timers = NULL;
+ timer_job_id = 0;
+
running = 1;
epfd = epoll_create(MAX_EPOLL_EVENTS);
@@ -256,6 +391,9 @@
evloop_cleanup(void)
{
struct pommed_event *p;
+ struct pommed_timer *t;
+ struct pommed_timer_job *j;
+ struct pommed_timer_job *jobs;
close(epfd);
@@ -268,4 +406,21 @@
free(p);
}
+
+ while (timers != NULL)
+ {
+ t = timers;
+ timers = timers->next;
+
+ jobs = t->jobs;
+ while (jobs != NULL)
+ {
+ j = jobs;
+ jobs = jobs->next;
+
+ free(j);
+ }
+
+ free(t);
+ }
}
Modified: trunk/pommed/evloop.h
===================================================================
--- trunk/pommed/evloop.h 2008-06-05 21:22:18 UTC (rev 485)
+++ trunk/pommed/evloop.h 2008-06-06 19:54:54 UTC (rev 486)
@@ -17,7 +17,26 @@
struct pommed_event *next;
};
+typedef void(*pommed_timer_cb)(int id, uint64_t ticks);
+struct pommed_timer_job
+{
+ int id;
+ pommed_timer_cb cb;
+
+ struct pommed_timer_job *next;
+};
+
+struct pommed_timer
+{
+ int fd;
+ int timeout;
+ struct pommed_timer_job *jobs;
+
+ struct pommed_timer *next;
+};
+
+
int
evloop_add(int fd, uint32_t events, pommed_event_cb cb);
@@ -25,10 +44,10 @@
evloop_remove(int fd);
int
-evloop_add_timer(int timeout, pommed_event_cb cb);
+evloop_add_timer(int timeout, pommed_timer_cb cb);
int
-evloop_remove_timer(int fd);
+evloop_remove_timer(int id);
int
evloop_iteration(void);
Modified: trunk/pommed/kbd_auto.c
===================================================================
--- trunk/pommed/kbd_auto.c 2008-06-05 21:22:18 UTC (rev 485)
+++ trunk/pommed/kbd_auto.c 2008-06-06 19:54:54 UTC (rev 486)
@@ -142,13 +142,8 @@
static void
-kbd_auto_process(int fd, uint32_t events)
+kbd_auto_process(int id, uint64_t ticks)
{
- uint64_t dummy;
-
- /* Acknowledge timer */
- read(fd, &dummy, sizeof(dummy));
-
/* Increment keyboard backlight idle timer */
kbd_bck_info.idle++;
if ((kbd_cfg.idle > 0) && (kbd_bck_info.idle > kbd_cfg.idle))
Modified: trunk/pommed/power.c
===================================================================
--- trunk/pommed/power.c 2008-06-05 21:22:18 UTC (rev 485)
+++ trunk/pommed/power.c 2008-06-06 19:54:54 UTC (rev 486)
@@ -41,15 +41,10 @@
static void
-power_check_ac_state(int fd, uint32_t events)
+power_check_ac_state(int id, uint64_t ticks)
{
int ac_state;
- uint64_t dummy;
-
- /* Acknowledge timer */
- read(fd, &dummy, sizeof(dummy));
-
ac_state = check_ac_state();
if (ac_state == prev_state)
More information about the Pommed-commits
mailing list