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
|
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN
#define G_ERRORCHECK_MUTEXES
#include <glib.h>
#include <stdio.h>
#include <string.h>
static gpointer
locking_thread (gpointer mutex)
{
g_mutex_lock ((GMutex*)mutex);
return NULL;
}
static void
lock_locked_mutex (void)
{
GMutex* mutex = g_mutex_new ();
g_mutex_lock (mutex);
g_mutex_lock (mutex);
}
static void
trylock_locked_mutex (void)
{
GMutex* mutex = g_mutex_new ();
g_mutex_lock (mutex);
g_mutex_trylock (mutex);
}
static void
unlock_unlocked_mutex (void)
{
GMutex* mutex = g_mutex_new ();
g_mutex_lock (mutex);
g_mutex_unlock (mutex);
g_mutex_unlock (mutex);
}
static void
free_locked_mutex (void)
{
GMutex* mutex = g_mutex_new ();
g_mutex_lock (mutex);
g_mutex_free (mutex);
}
static void
wait_on_unlocked_mutex (void)
{
GMutex* mutex = g_mutex_new ();
GCond* cond = g_cond_new ();
g_cond_wait (cond, mutex);
}
static void
wait_on_otherwise_locked_mutex (void)
{
GMutex* mutex = g_mutex_new ();
GCond* cond = g_cond_new ();
GThread* thread = g_thread_create (locking_thread, mutex, TRUE, NULL);
g_assert (thread != NULL);
g_usleep (G_USEC_PER_SEC);
g_cond_wait (cond, mutex);
}
static void
timed_wait_on_unlocked_mutex (void)
{
GMutex* mutex = g_mutex_new ();
GCond* cond = g_cond_new ();
g_cond_timed_wait (cond, mutex, NULL);
}
static void
timed_wait_on_otherwise_locked_mutex (void)
{
GMutex* mutex = g_mutex_new ();
GCond* cond = g_cond_new ();
GThread* thread = g_thread_create (locking_thread, mutex, TRUE, NULL);
g_assert (thread != NULL);
g_usleep (G_USEC_PER_SEC);
g_cond_timed_wait (cond, mutex, NULL);
}
struct
{
char *name;
void (*func)();
} func_table[] =
{
{"lock_locked_mutex", lock_locked_mutex},
{"trylock_locked_mutex", trylock_locked_mutex},
{"unlock_unlocked_mutex", unlock_unlocked_mutex},
{"free_locked_mutex", free_locked_mutex},
{"wait_on_unlocked_mutex", wait_on_unlocked_mutex},
{"wait_on_otherwise_locked_mutex", wait_on_otherwise_locked_mutex},
{"timed_wait_on_unlocked_mutex", timed_wait_on_unlocked_mutex},
{"timed_wait_on_otherwise_locked_mutex",
timed_wait_on_otherwise_locked_mutex}
};
int
main (int argc, char* argv[])
{
int i;
if (argc == 2)
{
for (i = 0; i < G_N_ELEMENTS (func_table); i++)
{
if (strcmp (func_table[i].name, argv[1]) == 0)
{
g_thread_init (NULL);
func_table[i].func ();
g_assert_not_reached ();
}
}
}
fprintf (stderr, "Usage: errorcheck-mutex-test [TEST]\n\n");
fprintf (stderr, " where TEST can be one of:\n\n");
for (i = 0; i < G_N_ELEMENTS (func_table); i++)
{
fprintf (stderr, " %s\n", func_table[i].name);
}
return 0;
}
|