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 modify it
6 * under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library. If not, see <http://www.gnu.org/licenses/>.
27 /* This is to keep e_flag_timed_wait() building, since
28 * it relies on g_cond_timed_wait() which is deprecated. */
29 #ifdef G_DISABLE_DEPRECATED
30 gboolean
g_cond_timed_wait (GCond
*cond
,
33 #endif /* G_DISABLE_DEPRECATED */
38 * Creates a new #EFlag object. It is initially unset.
40 * Returns: a new #EFlag
49 flag
= g_slice_new (EFlag
);
50 g_cond_init (&flag
->cond
);
51 g_mutex_init (&flag
->mutex
);
58 * e_flag_is_set: (skip)
61 * Returns the state of @flag.
63 * Returns: %TRUE if @flag is set
68 e_flag_is_set (EFlag
*flag
)
72 g_return_val_if_fail (flag
!= NULL
, FALSE
);
74 g_mutex_lock (&flag
->mutex
);
75 is_set
= flag
->is_set
;
76 g_mutex_unlock (&flag
->mutex
);
85 * Sets @flag. All threads waiting on @flag are woken up. Threads that
86 * call e_flag_wait() or e_flag_wait_until() once @flag is set will not
92 e_flag_set (EFlag
*flag
)
94 g_return_if_fail (flag
!= NULL
);
96 g_mutex_lock (&flag
->mutex
);
98 g_cond_broadcast (&flag
->cond
);
99 g_mutex_unlock (&flag
->mutex
);
103 * e_flag_clear: (skip)
106 * Unsets @flag. Subsequent calls to e_flag_wait() or e_flag_wait_until()
107 * will block until @flag is set.
112 e_flag_clear (EFlag
*flag
)
114 g_return_if_fail (flag
!= NULL
);
116 g_mutex_lock (&flag
->mutex
);
117 flag
->is_set
= FALSE
;
118 g_mutex_unlock (&flag
->mutex
);
122 * e_flag_wait: (skip)
125 * Blocks until @flag is set. If @flag is already set, the function returns
131 e_flag_wait (EFlag
*flag
)
133 g_return_if_fail (flag
!= NULL
);
135 g_mutex_lock (&flag
->mutex
);
136 while (!flag
->is_set
)
137 g_cond_wait (&flag
->cond
, &flag
->mutex
);
138 g_mutex_unlock (&flag
->mutex
);
142 * e_flag_timed_wait: (skip)
144 * @abs_time: a #GTimeVal, determining the final time
146 * Blocks until @flag is set, or until the time specified by @abs_time.
147 * If @flag is already set, the function returns immediately. The return
148 * value indicates the state of @flag after waiting.
150 * If @abs_time is %NULL, e_flag_timed_wait() acts like e_flag_wait().
152 * To easily calculate @abs_time, a combination of g_get_current_time() and
153 * g_time_val_add() can be used.
155 * Returns: %TRUE if @flag is now set
159 * Deprecated: 3.8: Use e_flag_wait_until() instead.
161 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
163 e_flag_timed_wait (EFlag
*flag
,
168 g_return_val_if_fail (flag
!= NULL
, FALSE
);
170 g_mutex_lock (&flag
->mutex
);
171 while (!flag
->is_set
)
172 if (!g_cond_timed_wait (&flag
->cond
, &flag
->mutex
, abs_time
))
174 is_set
= flag
->is_set
;
175 g_mutex_unlock (&flag
->mutex
);
179 G_GNUC_END_IGNORE_DEPRECATIONS
182 * e_flag_wait_until: (skip)
184 * @end_time: the monotonic time to wait until
186 * Blocks until @flag is set, or until the time specified by @end_time.
187 * If @flag is already set, the function returns immediately. The return
188 * value indicates the state of @flag after waiting.
190 * To easily calculate @end_time, a combination of g_get_monotonic_time() and
191 * G_TIME_SPAN_SECOND macro.
193 * Returns: %TRUE if @flag is now set
198 e_flag_wait_until (EFlag
*flag
,
203 g_return_val_if_fail (flag
!= NULL
, FALSE
);
205 g_mutex_lock (&flag
->mutex
);
206 while (!flag
->is_set
)
207 if (!g_cond_wait_until (&flag
->cond
, &flag
->mutex
, end_time
))
209 is_set
= flag
->is_set
;
210 g_mutex_unlock (&flag
->mutex
);
216 * e_flag_free: (skip)
224 e_flag_free (EFlag
*flag
)
226 g_return_if_fail (flag
!= NULL
);
228 /* Just to make sure that other threads are not holding the lock. */
229 g_mutex_lock (&flag
->mutex
);
230 g_cond_clear (&flag
->cond
);
231 g_mutex_unlock (&flag
->mutex
);
232 g_mutex_clear (&flag
->mutex
);
233 g_slice_free (EFlag
, flag
);