summaryrefslogtreecommitdiff
path: root/libedataserver/e-cancellable-locks.c
blob: ca4ddd5426b6a890218c6e309b44c714d3fb341b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-cancellable-locks.h"

/**
 * SECTION:e-cancellable-locks
 * @title: Cancellable Locks
 * @short_description: locks, which can listen for a #GCancellable during lock call
 *
 * An #ECancellableMutex and an #ECancellableRecMutex are similar to
 * GLib's #GMutex and #GRecMutex, with one exception, their <i>lock</i>
 * function takes also a @GCancellable instance, thus the waiting for a lock
 * can be cancelled any time.
 **/

static void
cancellable_locks_cancelled_cb (GCancellable *cancellable,
                                struct _ECancellableLocksBase *base)
{
	g_return_if_fail (base != NULL);

	/* wake-up any waiting threads */
	g_mutex_lock (&base->cond_mutex);
	g_cond_broadcast (&base->cond);
	g_mutex_unlock (&base->cond_mutex);
}

/**
 * e_cancellable_mutex_init:
 * @mutex: an #ECancellableMutex instance
 *
 * Initializes @mutex structure.
 *
 * Since: 3.8
 *
 * Deprecated: 3.12: If you think you need this, you're using mutexes wrong.
 **/
void
e_cancellable_mutex_init (ECancellableMutex *mutex)
{
	g_return_if_fail (mutex != NULL);

	g_mutex_init (&mutex->mutex);
	g_mutex_init (&mutex->base.cond_mutex);
	g_cond_init (&mutex->base.cond);
}

/**
 * e_cancellable_mutex_clear:
 * @mutex: an #ECancellableMutex instance
 *
 * Frees memory allocated by e_cancellable_mutex_init().
 *
 * Since: 3.8
 *
 * Deprecated: 3.12: If you think you need this, you're using mutexes wrong.
 **/
void
e_cancellable_mutex_clear (ECancellableMutex *mutex)
{
	g_return_if_fail (mutex != NULL);

	g_mutex_clear (&mutex->mutex);
	g_mutex_clear (&mutex->base.cond_mutex);
	g_cond_clear (&mutex->base.cond);
}

/**
 * e_cancellable_mutex_lock:
 * @mutex: an #ECancellableMutex instance
 * @cancellable: (allow-none): a #GCancellable, or %NULL
 *
 * Acquires lock on @mutex. The returned value indicates whether
 * the lock was acquired, while %FALSE is returned only either or
 * invalid arguments or the passed in @cancellable had been cancelled.
 * In case of %NULL @cancellable the function blocks like g_mutex_lock().
 *
 * Returns: %TRUE, if lock had been acquired, %FALSE otherwise
 *
 * Since: 3.8
 *
 * Deprecated: 3.12: If you think you need this, you're using mutexes wrong.
 **/
gboolean
e_cancellable_mutex_lock (ECancellableMutex *mutex,
                          GCancellable *cancellable)
{
	gulong handler_id;
	gboolean res = TRUE;

	g_return_val_if_fail (mutex != NULL, FALSE);

	g_mutex_lock (&mutex->base.cond_mutex);
	if (!cancellable) {
		g_mutex_unlock (&mutex->base.cond_mutex);
		g_mutex_lock (&mutex->mutex);
		return TRUE;
	}

	if (g_cancellable_is_cancelled (cancellable)) {
		g_mutex_unlock (&mutex->base.cond_mutex);
		return FALSE;
	}

	handler_id = g_signal_connect (
		cancellable, "cancelled",
		G_CALLBACK (cancellable_locks_cancelled_cb), &mutex->base);

	while (!g_mutex_trylock (&mutex->mutex)) {
		/* recheck once per 10 seconds, just in case */
		g_cond_wait_until (
			&mutex->base.cond, &mutex->base.cond_mutex,
			g_get_monotonic_time () + (10 * G_TIME_SPAN_SECOND));

		if (g_cancellable_is_cancelled (cancellable)) {
			res = FALSE;
			break;
		}
	}

	g_signal_handler_disconnect (cancellable, handler_id);

	g_mutex_unlock (&mutex->base.cond_mutex);

	return res;
}

/**
 * e_cancellable_mutex_unlock:
 * @mutex: an #ECancellableMutex instance
 *
 * Releases lock previously acquired by e_cancellable_mutex_lock().
 * Behaviour is undefined if this is called on a @mutex which returned
 * %FALSE in e_cancellable_mutex_lock().
 *
 * Since: 3.8
 *
 * Deprecated: 3.12: If you think you need this, you're using mutexes wrong.
 **/
void
e_cancellable_mutex_unlock (ECancellableMutex *mutex)
{
	g_return_if_fail (mutex != NULL);

	g_mutex_unlock (&mutex->mutex);

	g_mutex_lock (&mutex->base.cond_mutex);
	/* also wake-up any waiting threads */
	g_cond_broadcast (&mutex->base.cond);
	g_mutex_unlock (&mutex->base.cond_mutex);
}

/**
 * e_cancellable_mutex_get_internal_mutex:
 * @mutex: an #ECancellableMutex instance
 *
 * To get internal #GMutex. This is meant for cases when a lock is already
 * acquired, and the caller needs to wait for a #GCond, in which case
 * the returned #GMutex can be used to g_cond_wait() or g_cond_wait_until().
 *
 * Returns: Internal #GMutex, used in @mutex
 *
 * Since: 3.8
 *
 * Deprecated: 3.12: If you think you need this, you're using mutexes wrong.
 **/
GMutex *
e_cancellable_mutex_get_internal_mutex (ECancellableMutex *mutex)
{
	g_return_val_if_fail (mutex != NULL, NULL);

	return &mutex->mutex;
}

/**
 * e_cancellable_rec_mutex_init:
 * @rec_mutex: an #ECancellableRecMutex instance
 *
 * Initializes @rec_mutex structure.
 *
 * Since: 3.8
 *
 * Deprecated: 3.12: If you think you need this, you're using mutexes wrong.
 **/
void
e_cancellable_rec_mutex_init (ECancellableRecMutex *rec_mutex)
{
	g_return_if_fail (rec_mutex != NULL);

	g_rec_mutex_init (&rec_mutex->rec_mutex);
	g_mutex_init (&rec_mutex->base.cond_mutex);
	g_cond_init (&rec_mutex->base.cond);
}

/**
 * e_cancellable_rec_mutex_clear:
 * @rec_mutex: an #ECancellableRecMutex instance
 *
 * Frees memory allocated by e_cancellable_rec_mutex_init().
 *
 * Since: 3.8
 *
 * Deprecated: 3.12: If you think you need this, you're using mutexes wrong.
 **/
void
e_cancellable_rec_mutex_clear (ECancellableRecMutex *rec_mutex)
{
	g_return_if_fail (rec_mutex != NULL);

	g_rec_mutex_clear (&rec_mutex->rec_mutex);
	g_mutex_clear (&rec_mutex->base.cond_mutex);
	g_cond_clear (&rec_mutex->base.cond);
}

/**
 * e_cancellable_rec_mutex_lock:
 * @rec_mutex: an #ECancellableRecMutex instance
 * @cancellable: (allow-none): a #GCancellable, or %NULL
 *
 * Acquires lock on @rec_mutex. The returned value indicates whether
 * the lock was acquired, while %FALSE is returned only either or
 * invalid arguments or the passed in @cancellable had been cancelled.
 * In case of %NULL @cancellable the function blocks like g_rec_mutex_lock().
 *
 * Returns: %TRUE, if lock had been acquired, %FALSE otherwise
 *
 * Since: 3.8
 *
 * Deprecated: 3.12: If you think you need this, you're using mutexes wrong.
 **/
gboolean
e_cancellable_rec_mutex_lock (ECancellableRecMutex *rec_mutex,
                              GCancellable *cancellable)
{
	gulong handler_id;
	gboolean res = TRUE;

	g_return_val_if_fail (rec_mutex != NULL, FALSE);

	g_mutex_lock (&rec_mutex->base.cond_mutex);
	if (!cancellable) {
		g_mutex_unlock (&rec_mutex->base.cond_mutex);
		g_rec_mutex_lock (&rec_mutex->rec_mutex);
		return TRUE;
	}

	if (g_cancellable_is_cancelled (cancellable)) {
		g_mutex_unlock (&rec_mutex->base.cond_mutex);
		return FALSE;
	}

	handler_id = g_signal_connect (
		cancellable, "cancelled",
		G_CALLBACK (cancellable_locks_cancelled_cb), &rec_mutex->base);

	while (!g_rec_mutex_trylock (&rec_mutex->rec_mutex)) {
		/* recheck once per 10 seconds, just in case */
		g_cond_wait_until (
			&rec_mutex->base.cond, &rec_mutex->base.cond_mutex,
			g_get_monotonic_time () + (10 * G_TIME_SPAN_SECOND));

		if (g_cancellable_is_cancelled (cancellable)) {
			res = FALSE;
			break;
		}
	}

	g_signal_handler_disconnect (cancellable, handler_id);

	g_mutex_unlock (&rec_mutex->base.cond_mutex);

	return res;
}

/**
 * e_cancellable_rec_mutex_unlock:
 * @rec_mutex: an #ECancellableRecMutex instance
 *
 * Releases lock previously acquired by e_cancellable_rec_mutex_lock().
 * Behaviour is undefined if this is called on a @rec_mutex which returned
 * %FALSE in e_cancellable_rec_mutex_lock().
 *
 * Since: 3.8
 *
 * Deprecated: 3.12: If you think you need this, you're using mutexes wrong.
 **/
void
e_cancellable_rec_mutex_unlock (ECancellableRecMutex *rec_mutex)
{
	g_return_if_fail (rec_mutex != NULL);

	g_rec_mutex_unlock (&rec_mutex->rec_mutex);

	g_mutex_lock (&rec_mutex->base.cond_mutex);
	/* also wake-up any waiting threads */
	g_cond_broadcast (&rec_mutex->base.cond);
	g_mutex_unlock (&rec_mutex->base.cond_mutex);
}