]>
Dogcows Code - chaz/openbox/blob - openbox/timer.c
8 static GTimeVal ret_wait
;
9 static GSList
*timers
; /* nearest timer is at the top */
11 #define NEAREST_TIMEOUT (((Timer*)timers->data)->timeout)
13 static void insert_timer(Timer
*self
)
16 for (it
= timers
; it
!= NULL
; it
= it
->next
) {
18 if (!timercmp(&self
->timeout
, &t
->timeout
, >)) {
19 timers
= g_slist_insert_before(timers
, it
, self
);
23 if (it
== NULL
) /* didnt fit anywhere in the list */
24 timers
= g_slist_append(timers
, self
);
29 g_get_current_time(&now
);
36 for (it
= timers
; it
!= NULL
; it
= it
->next
) {
43 Timer
*timer_start(long delay
, TimeoutHandler cb
, void *data
)
45 Timer
*self
= g_new(Timer
, 1);
50 self
->last
= self
->timeout
= now
;
51 g_time_val_add(&self
->timeout
, delay
);
58 void timer_stop(Timer
*self
)
63 /* find the time to wait for the nearest timeout */
64 static gboolean
nearest_timeout_wait(GTimeVal
*tm
)
69 tm
->tv_sec
= NEAREST_TIMEOUT
.tv_sec
- now
.tv_sec
;
70 tm
->tv_usec
= NEAREST_TIMEOUT
.tv_usec
- now
.tv_usec
;
72 while (tm
->tv_usec
< 0) {
73 tm
->tv_usec
+= G_USEC_PER_SEC
;
76 tm
->tv_sec
+= tm
->tv_usec
/ G_USEC_PER_SEC
;
77 tm
->tv_usec
%= G_USEC_PER_SEC
;
85 void timer_dispatch(GTimeVal
**wait
)
87 g_get_current_time(&now
);
89 while (timers
!= NULL
) {
90 Timer
*curr
= timers
->data
; /* get the top element */
91 /* since timer_stop doesn't actually free the timer, we have to do our
95 timers
= g_slist_delete_link(timers
, timers
); /* delete the top */
100 /* the queue is sorted, so if this timer shouldn't fire, none are
102 if (!timercmp(&now
, &NEAREST_TIMEOUT
, >))
105 /* we set the last fired time to delay msec after the previous firing,
106 then re-insert. timers maintain their order and may trigger more
107 than once if they've waited more than one delay's worth of time.
109 timers
= g_slist_delete_link(timers
, timers
);
110 g_time_val_add(&curr
->last
, curr
->delay
);
111 curr
->action(curr
->data
);
112 g_time_val_add(&curr
->timeout
, curr
->delay
);
115 /* if at least one timer fires, then don't wait on X events, as there
116 may already be some in the queue from the timer callbacks.
118 ret_wait
.tv_sec
= ret_wait
.tv_usec
= 0;
123 if (nearest_timeout_wait(&ret_wait
))
This page took 0.04373 seconds and 4 git commands to generate.