[Xfce4-commits] <xfce4-panel:master> Use realtime POSIX timers for the clock.

Nick Schermer noreply at xfce.org
Sun Dec 11 23:16:01 CET 2011


Updating branch refs/heads/master
         to 07fe0a629b7b6092f2dfcaf4ef2dd2910f161a7c (commit)
       from dfda220bd03d4c67a8e0bc94395d1eb79dc6338e (commit)

commit 07fe0a629b7b6092f2dfcaf4ef2dd2910f161a7c
Author: Nick Schermer <nick at xfce.org>
Date:   Sat Dec 3 18:13:20 2011 +0100

    Use realtime POSIX timers for the clock.
    
    Use timer functions that work properly after
    suspending the system.

 plugins/clock/clock.c |  151 +++++++++++++++++++-----------------------------
 1 files changed, 60 insertions(+), 91 deletions(-)

diff --git a/plugins/clock/clock.c b/plugins/clock/clock.c
index 77a1d1c..135b45a 100644
--- a/plugins/clock/clock.c
+++ b/plugins/clock/clock.c
@@ -23,6 +23,9 @@
 #ifdef HAVE_TIME_H
 #include <time.h>
 #endif
+#ifdef HAVE_SIGNAL_H
+#include <signal.h>
+#endif
 #ifdef HAVE_MATH_H
 #include <math.h>
 #endif
@@ -71,9 +74,6 @@ static void     clock_plugin_orientation_changed       (XfcePanelPlugin       *p
 static void     clock_plugin_configure_plugin          (XfcePanelPlugin       *panel_plugin);
 static void     clock_plugin_set_mode                  (ClockPlugin           *plugin);
 static gboolean clock_plugin_tooltip                   (gpointer               user_data);
-static gboolean clock_plugin_timeout_running           (gpointer               user_data);
-static void     clock_plugin_timeout_destroyed         (gpointer               user_data);
-static gboolean clock_plugin_timeout_sync              (gpointer               user_data);
 
 
 
@@ -125,11 +125,11 @@ struct _ClockPlugin
 
 struct _ClockPluginTimeout
 {
-  guint       interval;
   GSourceFunc function;
   gpointer    data;
-  guint       timeout_id;
-  guint       restart : 1;
+  guint       interval;
+  guint       timer_idle;
+  timer_t     timer_id;
 };
 
 typedef struct
@@ -887,62 +887,35 @@ clock_plugin_tooltip (gpointer user_data)
 
 
 static gboolean
-clock_plugin_timeout_running (gpointer user_data)
+clock_plugin_timeout_expired_idle (gpointer data)
 {
-  ClockPluginTimeout *timeout = user_data;
-  gboolean            result;
-  struct tm           tm;
+  ClockPluginTimeout *timeout = data;
+
+  timeout->timer_idle = 0;
 
   GDK_THREADS_ENTER ();
-  result = (timeout->function) (timeout->data);
+  (timeout->function) (timeout->data);
   GDK_THREADS_LEAVE ();
 
-  /* check if the timeout still runs in time if updating once a minute */
-  if (result && timeout->interval == CLOCK_INTERVAL_MINUTE)
-    {
-      /* sync again when we don't run on time */
-      clock_plugin_get_localtime (&tm);
-      timeout->restart = tm.tm_sec != 0;
-    }
-
-  return result && !timeout->restart;
+  return FALSE;
 }
 
 
 
 static void
-clock_plugin_timeout_destroyed (gpointer user_data)
+clock_plugin_timeout_expired (sigval_t info)
 {
-  ClockPluginTimeout *timeout = user_data;
-
-  timeout->timeout_id = 0;
-
-  if (G_UNLIKELY (timeout->restart))
-    clock_plugin_timeout_set_interval (timeout, timeout->interval);
-}
-
+  ClockPluginTimeout *timeout = info.sival_ptr;
 
+  panel_return_if_fail (GTK_IS_WIDGET (timeout->data));
 
-static gboolean
-clock_plugin_timeout_sync (gpointer user_data)
-{
-  ClockPluginTimeout *timeout = user_data;
+  /* drop pending timeout */
+  if (timeout->timer_idle != 0)
+    g_source_remove (timeout->timer_idle);
 
-  /* run the user function */
-  if ((timeout->function) (timeout->data))
-    {
-      /* start the real timeout */
-      timeout->timeout_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, timeout->interval,
-                                                        clock_plugin_timeout_running, timeout,
-                                                        clock_plugin_timeout_destroyed);
-    }
-  else
-    {
-      timeout->timeout_id = 0;
-    }
-
-  /* stop the sync timeout */
-  return FALSE;
+  /* this function is called in a new thread, schedule
+   * an update in the mainloop */
+  timeout->timer_idle = g_idle_add (clock_plugin_timeout_expired_idle, timeout);
 }
 
 
@@ -953,18 +926,25 @@ clock_plugin_timeout_new (guint       interval,
                           gpointer    data)
 {
   ClockPluginTimeout *timeout;
+  struct sigevent     event;
 
   panel_return_val_if_fail (interval > 0, NULL);
   panel_return_val_if_fail (function != NULL, NULL);
 
   timeout = g_slice_new0 (ClockPluginTimeout);
-  timeout->interval = 0;
   timeout->function = function;
   timeout->data = data;
-  timeout->timeout_id = 0;
-  timeout->restart = FALSE;
 
-  clock_plugin_timeout_set_interval (timeout, interval);
+  event.sigev_notify = SIGEV_THREAD;
+  event.sigev_notify_function = clock_plugin_timeout_expired;
+  event.sigev_notify_attributes = NULL;
+  event.sigev_value.sival_ptr = timeout;
+
+  /* create the realtime timer */
+  if (timer_create (CLOCK_REALTIME, &event, &timeout->timer_id) == 0)
+    clock_plugin_timeout_set_interval (timeout, interval);
+  else
+    g_critical ("Failed to start realtime clock timeout");
 
   return timeout;
 }
@@ -975,53 +955,42 @@ void
 clock_plugin_timeout_set_interval (ClockPluginTimeout *timeout,
                                    guint               interval)
 {
-  struct tm tm;
-  guint     next_interval;
-  gboolean  restart = timeout->restart;
+  struct itimerspec itimer;
+  GTimeVal          current;
+  gint              flags = 0;
 
-  panel_return_if_fail (timeout != NULL);
-  panel_return_if_fail (interval > 0);
-
-  /* leave if nothing changed and we're not restarting */
-  if (!restart && timeout->interval == interval)
+  /* leave if the intervals are equal */
+  if (timeout->interval == interval)
     return;
+
+  /* set the interval timeout */
   timeout->interval = interval;
-  timeout->restart = FALSE;
+  itimer.it_interval.tv_sec = interval;
+  itimer.it_interval.tv_nsec = 0;
 
-  /* stop running timeout */
-  if (G_LIKELY (timeout->timeout_id != 0))
-    g_source_remove (timeout->timeout_id);
-  timeout->timeout_id = 0;
+  if (interval >= CLOCK_INTERVAL_MINUTE)
+    {
+      g_get_current_time (&current);
 
-  /* run function when not restarting, leave if it returns false */
-  if (!restart && !(timeout->function) (timeout->data))
-    return;
+      /* sync to the next absolute time */
+      itimer.it_value.tv_nsec = 0;
+      itimer.it_value.tv_sec = current.tv_sec + 60 - (current.tv_sec % 60) + 1;
 
-  /* get the seconds to the next internal */
-  if (interval == CLOCK_INTERVAL_MINUTE)
-    {
-      clock_plugin_get_localtime (&tm);
-      next_interval = 60 - tm.tm_sec;
+      flags = TIMER_ABSTIME;
     }
   else
     {
-      next_interval = 0;
+      /* for the seconds timer */
+      itimer.it_value.tv_nsec = 0;
+      itimer.it_value.tv_sec = interval;
     }
 
-  if (next_interval > 0)
-    {
-      /* start the sync timeout */
-      timeout->timeout_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, next_interval,
-                                                        clock_plugin_timeout_sync,
-                                                        timeout, NULL);
-    }
-  else
-    {
-      /* directly start running the normal timeout */
-      timeout->timeout_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval,
-                                                        clock_plugin_timeout_running, timeout,
-                                                        clock_plugin_timeout_destroyed);
-    }
+  if (timer_settime (timeout->timer_id, flags, &itimer, NULL) != 0)
+    g_critical ("Failed to set timer timeout");
+
+  /* schedule the first update */
+  if (timeout->timer_idle == 0)
+    timeout->timer_idle = g_idle_add (clock_plugin_timeout_expired_idle, timeout);
 }
 
 
@@ -1031,9 +1000,9 @@ clock_plugin_timeout_free (ClockPluginTimeout *timeout)
 {
   panel_return_if_fail (timeout != NULL);
 
-  timeout->restart = FALSE;
-  if (G_LIKELY (timeout->timeout_id != 0))
-    g_source_remove (timeout->timeout_id);
+  timer_delete (timeout->timer_id);
+  if (timeout->timer_idle != 0)
+    g_source_remove (timeout->timer_idle);
   g_slice_free (ClockPluginTimeout, timeout);
 }
 


More information about the Xfce4-commits mailing list