1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Novell, Inc.
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.
31 * Creates a new #EFlag object. It is initially unset.
33 * Returns: a new #EFlag
40 flag = g_slice_new (EFlag);
41 flag->cond = g_cond_new ();
42 flag->mutex = g_mutex_new ();
52 * Returns the state of @flag.
54 * Returns: %TRUE if @flag is set
57 e_flag_is_set (EFlag *flag)
61 g_return_val_if_fail (flag != NULL, FALSE);
63 g_mutex_lock (flag->mutex);
64 is_set = flag->is_set;
65 g_mutex_unlock (flag->mutex);
74 * Sets @flag. All threads waiting on @flag are woken up. Threads that
75 * call e_flag_wait() or e_flag_timed_wait() once @flag is set will not
79 e_flag_set (EFlag *flag)
81 g_return_if_fail (flag != NULL);
83 g_mutex_lock (flag->mutex);
85 g_cond_broadcast (flag->cond);
86 g_mutex_unlock (flag->mutex);
93 * Unsets @flag. Subsequent calls to e_flag_wait() or e_flag_timed_wait()
94 * will block until @flag is set.
97 e_flag_clear (EFlag *flag)
99 g_return_if_fail (flag != NULL);
101 g_mutex_lock (flag->mutex);
102 flag->is_set = FALSE;
103 g_mutex_unlock (flag->mutex);
110 * Blocks until @flag is set. If @flag is already set, the function returns
114 e_flag_wait (EFlag *flag)
116 g_return_if_fail (flag != NULL);
118 g_mutex_lock (flag->mutex);
119 while (!flag->is_set)
120 g_cond_wait (flag->cond, flag->mutex);
121 g_mutex_unlock (flag->mutex);
127 * @abs_time: a #GTimeVal, determining the final time
129 * Blocks until @flag is set, or until the time specified by @abs_time.
130 * If @flag is already set, the function returns immediately. The return
131 * value indicates the state of @flag after waiting.
133 * If @abs_time is %NULL, e_flag_timed_wait() acts like e_flag_wait().
135 * To easily calculate @abs_time, a combination of g_get_current_time() and
136 * g_time_val_add() can be used.
138 * Returns: %TRUE if @flag is now set
141 e_flag_timed_wait (EFlag *flag, GTimeVal *abs_time)
145 g_return_val_if_fail (flag != NULL, FALSE);
147 g_mutex_lock (flag->mutex);
148 while (!flag->is_set)
149 if (!g_cond_timed_wait (flag->cond, flag->mutex, abs_time))
151 is_set = flag->is_set;
152 g_mutex_unlock (flag->mutex);
164 e_flag_free (EFlag *flag)
166 g_return_if_fail (flag != NULL);
168 g_cond_free (flag->cond);
169 g_mutex_free (flag->mutex);
170 g_slice_free (EFlag, flag);