1 /* Unit tests for GThread
2 * Copyright (C) 2011 Red Hat, Inc
3 * Author: Matthias Clasen
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
26 thread1_func (gpointer data)
28 g_thread_exit (GINT_TO_POINTER (1));
30 g_assert_not_reached ();
35 /* test that g_thread_exit() works */
42 thread = g_thread_new ("test", thread1_func, NULL, TRUE, NULL);
44 result = g_thread_join (thread);
46 g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
50 thread2_func (gpointer data)
52 return g_thread_self ();
55 /* test that g_thread_self() works */
62 thread = g_thread_new ("test", thread2_func, NULL, TRUE, NULL);
64 g_assert (g_thread_self () != thread);
66 result = g_thread_join (thread);
68 g_assert (result == thread);
72 thread3_func (gpointer data)
83 result = g_thread_join (peer);
85 retval += GPOINTER_TO_INT (result);
88 return GINT_TO_POINTER (retval);
91 /* test that g_thread_join() works across peers */
96 GThread *thread1, *thread2, *thread3;
98 thread1 = g_thread_new ("a", thread3_func, NULL, TRUE, NULL);
99 thread2 = g_thread_new ("b", thread3_func, thread1, TRUE, NULL);
100 thread3 = g_thread_new ("c", thread3_func, thread2, TRUE, NULL);
102 result = g_thread_join (thread3);
104 g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9);
108 main (int argc, char *argv[])
110 g_test_init (&argc, &argv, NULL);
112 g_assert (g_thread_get_initialized ());
114 g_test_add_func ("/thread/thread1", test_thread1);
115 g_test_add_func ("/thread/thread2", test_thread2);
116 g_test_add_func ("/thread/thread3", test_thread3);
118 return g_test_run ();