Debian Codegraph @

pkcs11/gkm/gkm-timer.c

raw
1 /*
2 * gnome-keyring
3 *
4 * Copyright (C) 2009 Stefan Walter
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include "gkm-timer.h"
24
25 #include "egg/egg-error.h"
26
27 #include <glib.h>
28
29 struct _GkmTimer {
30 gint64 when;
31 GMutex *mutex;
32 gpointer identifier;
33 GkmTimerFunc callback;
34 gpointer user_data;
35 };
36
37 static GMutex timer_mutex = { 0, };
38 static GQueue *timer_queue = NULL;
39 static GThread *timer_thread = NULL;
40 static GCond timer_condition;
41 static GCond *timer_cond = NULL;
42 static gboolean timer_run = FALSE;
43 static gint timer_refs = 0;
44
45 static gint
46 compare_timers (gconstpointer a, gconstpointer b, gpointer user_data)
47 {
48 const GkmTimer *ta = a;
49 const GkmTimer *tb = b;
50 if (ta->when < tb->when)
51 return -1;
52 return ta->when > tb->when;
53 }
54
55 static gpointer
56 timer_thread_func (gpointer unused)
57 {
58 GkmTimer *timer;
59
60 g_mutex_lock (&timer_mutex);
61
62 while (timer_run) {
63 timer = g_queue_peek_head (timer_queue);
64
65 /* Nothing in the queue, wait until we have action */
66 if (!timer) {
67 g_cond_wait (timer_cond, &timer_mutex);
68 continue;
69 }
70
71 if (timer->when) {
72 gint64 offset = timer->when - g_get_monotonic_time ();
73 if (offset > 0) {
74 g_cond_wait_until (timer_cond, &timer_mutex, g_get_monotonic_time () + offset);
75 continue;
76 }
77 }
78
79 /* Leave our thread mutex, and enter the module */
80 g_mutex_unlock (&timer_mutex);
81 g_mutex_lock (timer->mutex);
82
83 if (timer->callback)
84 (timer->callback) (timer, timer->user_data);
85
86 /* Leave the module, and go back into our thread mutex */
87 g_mutex_unlock (timer->mutex);
88 g_mutex_lock (&timer_mutex);
89
90 /* There's a chance that the timer may no longer be at head of queue */
91 g_queue_remove (timer_queue, timer);
92 g_slice_free (GkmTimer, timer);
93 }
94
95 g_mutex_unlock (&timer_mutex);
96 return NULL;
97 }
98
99 void
100 gkm_timer_initialize (void)
101 {
102 GError *error = NULL;
103 g_mutex_lock (&timer_mutex);
104
105 g_atomic_int_inc (&timer_refs);
106 if (!timer_thread) {
107 timer_run = TRUE;
108 timer_thread = g_thread_new ("timer", timer_thread_func, NULL);
109 if (timer_thread) {
110 g_assert (timer_queue == NULL);
111 timer_queue = g_queue_new ();
112
113 g_assert (timer_cond == NULL);
114 timer_cond = &timer_condition;
115 g_cond_init (timer_cond);
116 } else {
117 g_warning ("could not create timer thread: %s",
118 egg_error_message (error));
119 }
120 }
121
122 g_mutex_unlock (&timer_mutex);
123 }
124
125 void
126 gkm_timer_shutdown (void)
127 {
128 GkmTimer *timer;
129
130 if (g_atomic_int_dec_and_test (&timer_refs)) {
131
132 g_mutex_lock (&timer_mutex);
133
134 timer_run = FALSE;
135 g_assert (timer_cond);
136 g_cond_broadcast (timer_cond);
137
138 g_mutex_unlock (&timer_mutex);
139
140 g_assert (timer_thread);
141 g_thread_join (timer_thread);
142 timer_thread = NULL;
143
144 g_assert (timer_queue);
145
146 /* Cleanup any outstanding timers */
147 while (!g_queue_is_empty (timer_queue)) {
148 timer = g_queue_pop_head (timer_queue);
149 g_slice_free (GkmTimer, timer);
150 }
151
152 g_queue_free (timer_queue);
153 timer_queue = NULL;
154
155 g_cond_clear (timer_cond);
156 timer_cond = NULL;
157 }
158 }
159
160 /* We're the only caller of this function */
161 GMutex* _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *self);
162
163 GkmTimer*
164 gkm_timer_start (GkmModule *module, glong seconds, GkmTimerFunc callback, gpointer user_data)
165 {
166 GkmTimer *timer;
167
168 g_return_val_if_fail (callback, NULL);
169 g_return_val_if_fail (timer_queue, NULL);
170
171 timer = g_slice_new (GkmTimer);
172 timer->when = g_get_monotonic_time () + seconds * G_TIME_SPAN_SECOND;
173 timer->callback = callback;
174 timer->user_data = user_data;
175
176 timer->mutex = _gkm_module_get_scary_mutex_that_you_should_not_touch (module);
177 g_return_val_if_fail (timer->mutex, NULL);
178
179 g_mutex_lock (&timer_mutex);
180
181 g_assert (timer_queue);
182 g_queue_insert_sorted (timer_queue, timer, compare_timers, NULL);
183 g_assert (timer_cond);
184 g_cond_broadcast (timer_cond);
185
186 g_mutex_unlock (&timer_mutex);
187
188 /*
189 * Note that the timer thread could not already completed this timer.
190 * This is because we're in the module, and in order to complete a timer
191 * the timer thread must enter the module mutex.
192 */
193
194 return timer;
195 }
196
197 void
198 gkm_timer_cancel (GkmTimer *timer)
199 {
200 GList *link;
201
202 g_return_if_fail (timer_queue);
203
204 g_mutex_lock (&timer_mutex);
205
206 g_assert (timer_queue);
207
208 link = g_queue_find (timer_queue, timer);
209 if (link) {
210
211 /*
212 * For thread safety the timer struct must be freed
213 * from the timer thread. So to cancel, what we do
214 * is move the timer to the front of the queue,
215 * and reset the callback and when.
216 */
217
218 timer->when = 0;
219 timer->callback = NULL;
220
221 g_queue_delete_link (timer_queue, link);
222 g_queue_push_head (timer_queue, timer);
223
224 g_assert (timer_cond);
225 g_cond_broadcast (timer_cond);
226 }
227
228 g_mutex_unlock (&timer_mutex);
229 }