ui: implement timers with waitsets
authorJeremy Kerr <jk@ozlabs.org>
Tue, 21 May 2013 04:43:36 +0000 (12:43 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 24 Jun 2013 04:52:50 +0000 (12:52 +0800)
A temporary change to the timers; we'll eventually remove these from the
ui code.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
ui/common/timer.c
ui/common/timer.h
ui/ncurses/generic-main.c
ui/ncurses/nc-cui.c
ui/twin/main-generic.c

index 954a18ad62b8a764d2c38c113457c70cfc402ee0..3c1e8331233b45e8016dd9faa2b8d8a0aaa976f1 100644 (file)
  * @seconds: The final timeout value in seconds.
  */
 
-void ui_timer_init(struct ui_timer *timer, unsigned int seconds)
+void ui_timer_init(struct waitset *waitset, struct ui_timer *timer,
+               unsigned int seconds)
 {
        pb_log("%s: %u\n", __func__, seconds);
-       assert(!timer->disabled);
        timer->timeout = seconds;
+       timer->waitset = waitset;
 }
 
 /**
- * ui_timer_next - Calculate the next timer interval.
+ * ui_timer_kick - Kickstart the next timer interval.
  */
 
-static unsigned int ui_timer_next(unsigned int seconds)
+static int timer_cb(void *arg)
 {
-       unsigned int next;
-
-       if (seconds == 0) {
-               next = 0;
-               goto done;
-       }
-
-       if (seconds <= 10) {
-               next = 1;
-               goto done;
-       }
-
-       if (seconds <= 60) {
-               next = seconds % 5;
-               next = next ? next : 5;
-               goto done;
-       }
+       struct ui_timer *timer = arg;
 
-       next = seconds % 10;
-       next = next ? next : 10;
-
-done:
-       pb_log("%s: %u = %u\n", __func__, seconds, next);
-       return next;
+       timer->handle_timeout(timer);
+       timer->waiter = 0;
+       return 0;
 }
 
-/**
- * ui_timer_kick - Kickstart the next timer interval.
- */
-
 void ui_timer_kick(struct ui_timer *timer)
 {
-       unsigned int next;
-
-       if(timer->disabled)
-               return;
-
        if (timer->update_display)
                timer->update_display(timer, timer->timeout);
 
-       next = ui_timer_next(timer->timeout);
-       timer->timeout -= next;
-
-       if (next) {
-               alarm(next);
-               return;
-       }
-
-       pb_log("%s: timed out\n", __func__);
+       if (timer->waiter)
+               waiter_remove(timer->waiter);
 
-       ui_timer_disable(timer);
-       timer->handle_timeout(timer);
+       timer->waiter = waiter_register_timeout(timer->waitset,
+                       timer->timeout * 1000, timer_cb, timer);
 }
 
 /**
@@ -106,34 +72,10 @@ void ui_timer_kick(struct ui_timer *timer)
 
 void ui_timer_disable(struct ui_timer *timer)
 {
-       if (timer->disabled)
+       if (!timer->waiter)
                return;
 
        pb_log("%s\n", __func__);
-       timer->disabled = 1;
-       timer->timeout = UINT_MAX;
-       alarm(0);
-}
-
-/**
- * ui_timer_sigalrm
- *
- * Called at SIGALRM.
- */
-
-void ui_timer_sigalrm(struct ui_timer *timer)
-{
-       timer->signaled = 1;
-}
-
-/**
- * ui_timer_process_sig - Process a timer signal
- */
-
-void ui_timer_process_sig(struct ui_timer *timer)
-{
-       while (timer->signaled) {
-               timer->signaled = 0;
-               ui_timer_kick(timer);
-       }
+       waiter_remove(timer->waiter);
+       timer->waiter = NULL;
 }
index 781b442448334d203a3d8ae74db192ee920794dd..7b3651250cfa7c422d79db2ded306b0d345e9510 100644 (file)
@@ -19,7 +19,7 @@
 #if !defined(_PB_UI_TIMER_H)
 #define _PB_UI_TIMER_H
 
-#include <signal.h>
+#include <waiter/waiter.h>
 
 /**
  * struct ui_timer - UI timeout.
 
 struct ui_timer {
        unsigned int timeout;
-       unsigned int disabled;
-       sig_atomic_t signaled;
+       struct waiter *waiter;
+       struct waitset *waitset;
        void (*update_display)(struct ui_timer *timer, unsigned int timeout);
        void (*handle_timeout)(struct ui_timer *timer);
 };
 
-void ui_timer_init(struct ui_timer *timer, unsigned int seconds);
+void ui_timer_init(struct waitset *set, struct ui_timer *timer,
+               unsigned int seconds);
 void ui_timer_kick(struct ui_timer *timer);
 void ui_timer_disable(struct ui_timer *timer);
-void ui_timer_sigalrm(struct ui_timer *timer);
-void ui_timer_process_sig(struct ui_timer *timer);
 
 #endif
index 365034ceea3c402d1870e33fa627d80c9fcb4539..560e3e5683e9b32b9ee9c965a4df01e32580a458 100644 (file)
@@ -180,10 +180,6 @@ static void sig_handler(int signum)
        DBGS("%d\n", signum);
 
        switch (signum) {
-       case SIGALRM:
-               if (pb.cui)
-                       ui_timer_sigalrm(&pb.cui->timer);
-               break;
        case SIGWINCH:
                if (pb.cui)
                        cui_resize(pb.cui);
index ef3fd23b8474711a65d1b78d77845d1cf22c7acb..bd4f6b8ae84a175699d9f312555bdd23f2c11205 100644 (file)
@@ -623,8 +623,6 @@ int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
                if (cui->abort)
                        break;
 
-               ui_timer_process_sig(&cui->timer);
-
                while (cui->resize) {
                        cui->resize = 0;
                        cui_handle_resize(cui);
index c9cb4923863c06b96807514e1ab46eb709ae406d..fcda70970bdce1e5b8e4ba0f54f06a65e5790a7f 100644 (file)
@@ -212,8 +212,6 @@ static int run(struct pbt_client *client)
                if (client->signal_data.abort)
                        break;
 
-               ui_timer_process_sig(&client->signal_data.timer);
-
                while (client->signal_data.resize) {
                        client->signal_data.resize = 0;
                        pbt_client_resize(client);
@@ -245,9 +243,6 @@ static void sig_handler(int signum)
                return;
 
        switch (signum) {
-       case SIGALRM:
-               ui_timer_sigalrm(&sd->timer);
-               break;
        case SIGWINCH:
                sd->resize = 1;
                break;