1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3 #define G_ERRORCHECK_MUTEXES
4
5 #include <glib.h>
6 #include <stdio.h>
7 #include <string.h>
8
9 static gpointer
locking_thread(gpointer mutex)10 locking_thread (gpointer mutex)
11 {
12 g_mutex_lock ((GMutex*)mutex);
13
14 return NULL;
15 }
16
17 static void
lock_locked_mutex(void)18 lock_locked_mutex (void)
19 {
20 GMutex* mutex = g_mutex_new ();
21 g_mutex_lock (mutex);
22 g_mutex_lock (mutex);
23 }
24
25 static void
trylock_locked_mutex(void)26 trylock_locked_mutex (void)
27 {
28 GMutex* mutex = g_mutex_new ();
29 g_mutex_lock (mutex);
30 g_mutex_trylock (mutex);
31 }
32
33 static void
unlock_unlocked_mutex(void)34 unlock_unlocked_mutex (void)
35 {
36 GMutex* mutex = g_mutex_new ();
37 g_mutex_lock (mutex);
38 g_mutex_unlock (mutex);
39 g_mutex_unlock (mutex);
40 }
41
42 static void
free_locked_mutex(void)43 free_locked_mutex (void)
44 {
45 GMutex* mutex = g_mutex_new ();
46 g_mutex_lock (mutex);
47 g_mutex_free (mutex);
48 }
49
50 static void
wait_on_unlocked_mutex(void)51 wait_on_unlocked_mutex (void)
52 {
53 GMutex* mutex = g_mutex_new ();
54 GCond* cond = g_cond_new ();
55 g_cond_wait (cond, mutex);
56 }
57
58 static void
wait_on_otherwise_locked_mutex(void)59 wait_on_otherwise_locked_mutex (void)
60 {
61 GMutex* mutex = g_mutex_new ();
62 GCond* cond = g_cond_new ();
63 GThread* thread = g_thread_create (locking_thread, mutex, TRUE, NULL);
64 g_assert (thread != NULL);
65 g_usleep (G_USEC_PER_SEC);
66 g_cond_wait (cond, mutex);
67 }
68
69 static void
timed_wait_on_unlocked_mutex(void)70 timed_wait_on_unlocked_mutex (void)
71 {
72 GMutex* mutex = g_mutex_new ();
73 GCond* cond = g_cond_new ();
74 g_cond_timed_wait (cond, mutex, NULL);
75 }
76
77 static void
timed_wait_on_otherwise_locked_mutex(void)78 timed_wait_on_otherwise_locked_mutex (void)
79 {
80 GMutex* mutex = g_mutex_new ();
81 GCond* cond = g_cond_new ();
82 GThread* thread = g_thread_create (locking_thread, mutex, TRUE, NULL);
83 g_assert (thread != NULL);
84 g_usleep (G_USEC_PER_SEC);
85 g_cond_timed_wait (cond, mutex, NULL);
86 }
87
88 struct
89 {
90 char *name;
91 void (*func)();
92 } func_table[] =
93 {
94 {"lock_locked_mutex", lock_locked_mutex},
95 {"trylock_locked_mutex", trylock_locked_mutex},
96 {"unlock_unlocked_mutex", unlock_unlocked_mutex},
97 {"free_locked_mutex", free_locked_mutex},
98 {"wait_on_unlocked_mutex", wait_on_unlocked_mutex},
99 {"wait_on_otherwise_locked_mutex", wait_on_otherwise_locked_mutex},
100 {"timed_wait_on_unlocked_mutex", timed_wait_on_unlocked_mutex},
101 {"timed_wait_on_otherwise_locked_mutex",
102 timed_wait_on_otherwise_locked_mutex}
103 };
104
105 int
main(int argc,char * argv[])106 main (int argc, char* argv[])
107 {
108 int i;
109
110 if (argc == 2)
111 {
112 for (i = 0; i < G_N_ELEMENTS (func_table); i++)
113 {
114 if (strcmp (func_table[i].name, argv[1]) == 0)
115 {
116 g_thread_init (NULL);
117 func_table[i].func ();
118 g_assert_not_reached ();
119 }
120 }
121 }
122
123 fprintf (stderr, "Usage: errorcheck-mutex-test [TEST]\n\n");
124 fprintf (stderr, " where TEST can be one of:\n\n");
125 for (i = 0; i < G_N_ELEMENTS (func_table); i++)
126 {
127 fprintf (stderr, " %s\n", func_table[i].name);
128 }
129
130 return 0;
131 }
132