Add a missing since tag. (#464259, Mark Doliner)
[glib.git] / tests / errorcheck-mutex-test.c
blob5b300572c3825a4d0c5629819bb1b952263d0b91
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3 #define G_ERRORCHECK_MUTEXES
5 #include <glib.h>
6 #include <stdio.h>
7 #include <string.h>
9 static gpointer
10 locking_thread (gpointer mutex)
12 g_mutex_lock ((GMutex*)mutex);
14 return NULL;
17 static void
18 lock_locked_mutex (void)
20 GMutex* mutex = g_mutex_new ();
21 g_mutex_lock (mutex);
22 g_mutex_lock (mutex);
25 static void
26 trylock_locked_mutex (void)
28 GMutex* mutex = g_mutex_new ();
29 g_mutex_lock (mutex);
30 g_mutex_trylock (mutex);
33 static void
34 unlock_unlocked_mutex (void)
36 GMutex* mutex = g_mutex_new ();
37 g_mutex_lock (mutex);
38 g_mutex_unlock (mutex);
39 g_mutex_unlock (mutex);
42 static void
43 free_locked_mutex (void)
45 GMutex* mutex = g_mutex_new ();
46 g_mutex_lock (mutex);
47 g_mutex_free (mutex);
50 static void
51 wait_on_unlocked_mutex (void)
53 GMutex* mutex = g_mutex_new ();
54 GCond* cond = g_cond_new ();
55 g_cond_wait (cond, mutex);
58 static void
59 wait_on_otherwise_locked_mutex (void)
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);
69 static void
70 timed_wait_on_unlocked_mutex (void)
72 GMutex* mutex = g_mutex_new ();
73 GCond* cond = g_cond_new ();
74 g_cond_timed_wait (cond, mutex, NULL);
77 static void
78 timed_wait_on_otherwise_locked_mutex (void)
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);
88 struct
90 char *name;
91 void (*func)();
92 } func_table[] =
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}
106 main (int argc, char* argv[])
108 int i;
110 if (argc == 2)
112 for (i = 0; i < G_N_ELEMENTS (func_table); i++)
114 if (strcmp (func_table[i].name, argv[1]) == 0)
116 g_thread_init (NULL);
117 func_table[i].func ();
118 g_assert_not_reached ();
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++)
127 fprintf (stderr, " %s\n", func_table[i].name);
130 return 0;