File: e-flag.c

package info (click to toggle)
evolution-data-server 3.4.4-3%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 47,788 kB
  • sloc: ansic: 248,622; sh: 12,008; makefile: 2,874; xml: 428; perl: 204; python: 30
file content (186 lines) | stat: -rw-r--r-- 3,786 bytes parent folder | download
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
/* -*- 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 version 2 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program 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
 * 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, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "e-flag.h"

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

/**
 * e_flag_new:
 *
 * 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);
	flag->cond = g_cond_new ();
	flag->mutex = g_mutex_new ();
	flag->is_set = FALSE;

	return flag;
}

/**
 * e_flag_is_set:
 * @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:
 * @flag: an #EFlag
 *
 * Sets @flag.  All threads waiting on @flag are woken up.  Threads that
 * call e_flag_wait() or e_flag_timed_wait() 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:
 * @flag: an #EFlag
 *
 * Unsets @flag.  Subsequent calls to e_flag_wait() or e_flag_timed_wait()
 * 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:
 * @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:
 * @flag: an #EFlag
 * @abs_time: 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
 **/
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;
}

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

	g_cond_free (flag->cond);
	g_mutex_free (flag->mutex);
	g_slice_free (EFlag, flag);
}