File: check_mutex.c

package info (click to toggle)
libgda4 4.0.12-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 34,928 kB
  • ctags: 18,543
  • sloc: ansic: 187,884; sh: 10,317; xml: 7,903; yacc: 3,454; makefile: 1,974; java: 1,253; python: 896; sql: 321
file content (184 lines) | stat: -rw-r--r-- 3,436 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
#include <libgda/libgda.h>
#include <string.h>
#include <unistd.h>

#define NTHREADS 3
#define DEBUG_PRINT
#undef DEBUG_PRINT


/* 
 * tests
 */
typedef gboolean (*TestFunc) (GError **);
static gboolean test1 (GError **error);
static gboolean test2 (GError **error);
static gboolean test3 (GError **error);

TestFunc tests[] = {
	test1,
	test2,
	test3
};

int 
main (int argc, char** argv)
{
	g_type_init ();
	gda_init ();

	gint failures = 0;
	gint j, ntests = 0;;
	for (j = 0; j < 10; j++) {
		gint i;
		
		for (i = 0; i < sizeof (tests) / sizeof (TestFunc); i++) {
			GError *error = NULL;
			if (! tests[i] (&error)) {
				g_print ("Test %d failed: %s\n", i+1, 
					 error && error->message ? error->message : "No detail");
				if (error)
					g_error_free (error);
				failures ++;
			}
			ntests ++;
		}
	}

	g_print ("TESTS COUNT: %d\n", ntests);
	g_print ("FAILURES: %d\n", failures);

	return failures != 0 ? 1 : 0;
}

typedef struct {
	GThread  *thread;
	gint      th_id;
	gboolean  finished;
	GdaMutex *mutex;
} ThData;

static gboolean
test_multiple_threads (GThreadFunc func, GError **error)
{
	GdaMutex *mutex;
	ThData data[NTHREADS];
	gint i;

	mutex = gda_mutex_new ();

	for (i = 0; i < NTHREADS; i++) {
		ThData *d = &(data[i]);
		d->th_id = i;
		d->mutex = mutex;
		d->finished = FALSE;
	}

	for (i = 0; i < NTHREADS; i++) {
		ThData *d = &(data[i]);
#ifdef DEBUG_PRINT
		g_print ("Running thread %d\n", d->th_id);
#endif
		d->thread = g_thread_create (func, d, TRUE, NULL);
	}

	/* wait a bit */
	usleep (100000);

	for (i = 0; i < NTHREADS; i++) {
		ThData *d = &(data[i]);
		if (!d->finished) {
			/* there are some threads locked somethere */
			exit (EXIT_FAILURE);
		}
		else
			g_thread_join (d->thread);
	}
	
	gda_mutex_free (mutex);

	return TRUE;
}

/*
 * Creation and destruction test
 */
static gboolean
test1 (GError **error)
{
	GdaMutex *mutex;

	mutex = gda_mutex_new ();
	gda_mutex_free (mutex);

	return TRUE;
}

/*
 * Twice locking
 */
gpointer
test2_start_thread (ThData *data)
{
	gda_mutex_lock (data->mutex);
#ifdef DEBUG_PRINT
	g_print ("Mutex locked for %d\n", data->th_id);
#endif
	g_thread_yield ();
	gda_mutex_lock (data->mutex);
#ifdef DEBUG_PRINT
	g_print ("Mutex re-locked for %d\n", data->th_id);
#endif
	gda_mutex_unlock (data->mutex);
#ifdef DEBUG_PRINT
	g_print ("Mutex unlocked for %d\n", data->th_id);
	g_print ("Mutex unlocked for %d\n", data->th_id);
#endif
	gda_mutex_unlock (data->mutex);
	data->finished = TRUE;
	return NULL;
}

static gboolean
test2 (GError **error)
{
	return test_multiple_threads ((GThreadFunc) test2_start_thread, error);
}

/*
 * Try lock
 */
gpointer
test3_start_thread (ThData *data)
{
	gboolean tried = FALSE;
	while (!tried) {
		tried = gda_mutex_trylock (data->mutex);
#ifdef DEBUG_PRINT
		g_print ("TRY for %d: %s\n", data->th_id, tried ? "got it" : "no luck");
#endif
		g_thread_yield ();
	}
#ifdef DEBUG_PRINT
	g_print ("Mutex locked for %d\n", data->th_id);
#endif
	g_thread_yield ();
	gda_mutex_lock (data->mutex);
#ifdef DEBUG_PRINT
	g_print ("Mutex re-locked for %d\n", data->th_id);
#endif
	gda_mutex_unlock (data->mutex);
#ifdef DEBUG_PRINT
	g_print ("Mutex unlocked for %d\n", data->th_id);
	g_print ("Mutex unlocked for %d\n", data->th_id);
#endif
	gda_mutex_unlock (data->mutex);
	data->finished = TRUE;
	return NULL;
}

static gboolean
test3 (GError **error)
{
	return test_multiple_threads ((GThreadFunc) test3_start_thread, error);
}