1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU Lesser General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
28 /* This is to keep e_flag_timed_wait() building, since
29 * it relies on g_cond_timed_wait() which is deprecated. */
30 #if GLIB_CHECK_VERSION(2,35,6)
31 #ifdef G_DISABLE_DEPRECATED
32 gboolean g_cond_timed_wait (GCond *cond,
35 #endif /* G_DISABLE_DEPRECATED */
36 #endif /* GLIB_CHECK_VERSION */
41 * Creates a new #EFlag object. It is initially unset.
43 * Returns: a new #EFlag
52 flag = g_slice_new (EFlag);
53 g_cond_init (&flag->cond);
54 g_mutex_init (&flag->mutex);
64 * Returns the state of @flag.
66 * Returns: %TRUE if @flag is set
71 e_flag_is_set (EFlag *flag)
75 g_return_val_if_fail (flag != NULL, FALSE);
77 g_mutex_lock (&flag->mutex);
78 is_set = flag->is_set;
79 g_mutex_unlock (&flag->mutex);
88 * Sets @flag. All threads waiting on @flag are woken up. Threads that
89 * call e_flag_wait() or e_flag_wait_until() once @flag is set will not
95 e_flag_set (EFlag *flag)
97 g_return_if_fail (flag != NULL);
99 g_mutex_lock (&flag->mutex);
101 g_cond_broadcast (&flag->cond);
102 g_mutex_unlock (&flag->mutex);
109 * Unsets @flag. Subsequent calls to e_flag_wait() or e_flag_wait_until()
110 * will block until @flag is set.
115 e_flag_clear (EFlag *flag)
117 g_return_if_fail (flag != NULL);
119 g_mutex_lock (&flag->mutex);
120 flag->is_set = FALSE;
121 g_mutex_unlock (&flag->mutex);
128 * Blocks until @flag is set. If @flag is already set, the function returns
134 e_flag_wait (EFlag *flag)
136 g_return_if_fail (flag != NULL);
138 g_mutex_lock (&flag->mutex);
139 while (!flag->is_set)
140 g_cond_wait (&flag->cond, &flag->mutex);
141 g_mutex_unlock (&flag->mutex);
147 * @abs_time: a #GTimeVal, determining the final time
149 * Blocks until @flag is set, or until the time specified by @abs_time.
150 * If @flag is already set, the function returns immediately. The return
151 * value indicates the state of @flag after waiting.
153 * If @abs_time is %NULL, e_flag_timed_wait() acts like e_flag_wait().
155 * To easily calculate @abs_time, a combination of g_get_current_time() and
156 * g_time_val_add() can be used.
158 * Returns: %TRUE if @flag is now set
162 * Deprecated: 3.8: Use e_flag_wait_until() instead.
164 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
166 e_flag_timed_wait (EFlag *flag,
171 g_return_val_if_fail (flag != NULL, FALSE);
173 g_mutex_lock (&flag->mutex);
174 while (!flag->is_set)
175 if (!g_cond_timed_wait (&flag->cond, &flag->mutex, abs_time))
177 is_set = flag->is_set;
178 g_mutex_unlock (&flag->mutex);
182 G_GNUC_END_IGNORE_DEPRECATIONS
187 * @end_time: the monotonic time to wait until
189 * Blocks until @flag is set, or until the time specified by @end_time.
190 * If @flag is already set, the function returns immediately. The return
191 * value indicates the state of @flag after waiting.
193 * To easily calculate @end_time, a combination of g_get_monotonic_time() and
194 * G_TIME_SPAN_SECOND macro.
196 * Returns: %TRUE if @flag is now set
201 e_flag_wait_until (EFlag *flag,
206 g_return_val_if_fail (flag != NULL, FALSE);
208 g_mutex_lock (&flag->mutex);
209 while (!flag->is_set)
210 if (!g_cond_wait_until (&flag->cond, &flag->mutex, end_time))
212 is_set = flag->is_set;
213 g_mutex_unlock (&flag->mutex);
227 e_flag_free (EFlag *flag)
229 g_return_if_fail (flag != NULL);
231 g_cond_clear (&flag->cond);
232 g_mutex_clear (&flag->mutex);
233 g_slice_free (EFlag, flag);