summaryrefslogtreecommitdiff
path: root/src/libedataserver/e-flag.c
blob: fc857f719c83a63b47d9cd894fad8e0cf3fffcc3 (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
/* -*- 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/>.
 *
 */

#include "e-flag.h"

struct _EFlag {
	GCond cond;
	GMutex mutex;
	gboolean is_set;
};

/* This is to keep e_flag_timed_wait() building, since
 * it relies on g_cond_timed_wait() which is deprecated. */
#ifdef G_DISABLE_DEPRECATED
gboolean	g_cond_timed_wait		(GCond *cond,
						 GMutex *mutex,
						 GTimeVal *timeval);
#endif /* G_DISABLE_DEPRECATED */

/**
 * e_flag_new: (skip)
 *
 * Creates a new #EFlag object.  It is initially unset.
 *
 * Returns: a new #EFlag
 *
 * Since: 1.12
 **/
EFlag *
e_flag_new (void)
{
	EFlag *flag;

	flag = g_slice_new (EFlag);
	g_cond_init (&flag->cond);
	g_mutex_init (&flag->mutex);
	flag->is_set = FALSE;

	return flag;
}

/**
 * e_flag_is_set: (skip)
 * @flag: an #EFlag
 *
 * Returns the state of @flag.
 *
 * Returns: %TRUE if @flag is set
 *
 * Since: 1.12
 **/
gboolean
e_flag_is_set (EFlag *flag)
{
	gboolean is_set;

	g_return_val_if_fail (flag != NULL, FALSE);

	g_mutex_lock (&flag->mutex);
	is_set = flag->is_set;
	g_mutex_unlock (&flag->mutex);

	return is_set;
}

/**
 * e_flag_set: (skip)
 * @flag: an #EFlag
 *
 * Sets @flag.  All threads waiting on @flag are woken up.  Threads that
 * call e_flag_wait() or e_flag_wait_until() once @flag is set will not
 * block at all.
 *
 * Since: 1.12
 **/
void
e_flag_set (EFlag *flag)
{
	g_return_if_fail (flag != NULL);

	g_mutex_lock (&flag->mutex);
	flag->is_set = TRUE;
	g_cond_broadcast (&flag->cond);
	g_mutex_unlock (&flag->mutex);
}

/**
 * e_flag_clear: (skip)
 * @flag: an #EFlag
 *
 * Unsets @flag.  Subsequent calls to e_flag_wait() or e_flag_wait_until()
 * will block until @flag is set.
 *
 * Since: 1.12
 **/
void
e_flag_clear (EFlag *flag)
{
	g_return_if_fail (flag != NULL);

	g_mutex_lock (&flag->mutex);
	flag->is_set = FALSE;
	g_mutex_unlock (&flag->mutex);
}

/**
 * e_flag_wait: (skip)
 * @flag: an #EFlag
 *
 * Blocks until @flag is set.  If @flag is already set, the function returns
 * immediately.
 *
 * Since: 1.12
 **/
void
e_flag_wait (EFlag *flag)
{
	g_return_if_fail (flag != NULL);

	g_mutex_lock (&flag->mutex);
	while (!flag->is_set)
		g_cond_wait (&flag->cond, &flag->mutex);
	g_mutex_unlock (&flag->mutex);
}

/**
 * e_flag_timed_wait: (skip)
 * @flag: an #EFlag
 * @abs_time: (nullable): a #GTimeVal, determining the final time
 *
 * Blocks until @flag is set, or until the time specified by @abs_time.
 * If @flag is already set, the function returns immediately.  The return
 * value indicates the state of @flag after waiting.
 *
 * If @abs_time is %NULL, e_flag_timed_wait() acts like e_flag_wait().
 *
 * To easily calculate @abs_time, a combination of g_get_current_time() and
 * g_time_val_add() can be used.
 *
 * Returns: %TRUE if @flag is now set
 *
 * Since: 1.12
 *
 * Deprecated: 3.8: Use e_flag_wait_until() instead.
 **/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
gboolean
e_flag_timed_wait (EFlag *flag,
                   GTimeVal *abs_time)
{
	gboolean is_set;

	g_return_val_if_fail (flag != NULL, FALSE);

	g_mutex_lock (&flag->mutex);
	while (!flag->is_set)
		if (!g_cond_timed_wait (&flag->cond, &flag->mutex, abs_time))
			break;
	is_set = flag->is_set;
	g_mutex_unlock (&flag->mutex);

	return is_set;
}
G_GNUC_END_IGNORE_DEPRECATIONS

/**
 * e_flag_wait_until: (skip)
 * @flag: an #EFlag
 * @end_time: the monotonic time to wait until
 *
 * Blocks until @flag is set, or until the time specified by @end_time.
 * If @flag is already set, the function returns immediately.  The return
 * value indicates the state of @flag after waiting.
 *
 * To easily calculate @end_time, a combination of g_get_monotonic_time() and
 * G_TIME_SPAN_SECOND macro.
 *
 * Returns: %TRUE if @flag is now set
 *
 * Since: 3.8
 **/
gboolean
e_flag_wait_until (EFlag *flag,
                   gint64 end_time)
{
	gboolean is_set;

	g_return_val_if_fail (flag != NULL, FALSE);

	g_mutex_lock (&flag->mutex);
	while (!flag->is_set)
		if (!g_cond_wait_until (&flag->cond, &flag->mutex, end_time))
			break;
	is_set = flag->is_set;
	g_mutex_unlock (&flag->mutex);

	return is_set;
}

/**
 * e_flag_free: (skip)
 * @flag: an #EFlag
 *
 * Destroys @flag.
 *
 * Since: 1.12
 **/
void
e_flag_free (EFlag *flag)
{
	g_return_if_fail (flag != NULL);

	/* Just to make sure that other threads are not holding the lock. */
	g_mutex_lock (&flag->mutex);
	g_cond_clear (&flag->cond);
	g_mutex_unlock (&flag->mutex);
	g_mutex_clear (&flag->mutex);
	g_slice_free (EFlag, flag);
}