Add some more thread tests
[platform/upstream/glib.git] / glib / tests / thread.c
1 /* Unit tests for GThread
2  * Copyright (C) 2011 Red Hat, Inc
3  * Author: Matthias Clasen
4  *
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.
8  *
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.
12  *
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.
21  */
22
23 #include <glib.h>
24
25 static gpointer
26 thread1_func (gpointer data)
27 {
28   g_thread_exit (GINT_TO_POINTER (1));
29
30   g_assert_not_reached ();
31
32   return NULL;
33 }
34
35 /* test that g_thread_exit() works */
36 static void
37 test_thread1 (void)
38 {
39   gpointer result;
40   GThread *thread;
41
42   thread = g_thread_new ("test", thread1_func, NULL, TRUE, NULL);
43
44   result = g_thread_join (thread);
45
46   g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
47 }
48
49 static gpointer
50 thread2_func (gpointer data)
51 {
52   return g_thread_self ();
53 }
54
55 /* test that g_thread_self() works */
56 static void
57 test_thread2 (void)
58 {
59   gpointer result;
60   GThread *thread;
61
62   thread = g_thread_new ("test", thread2_func, NULL, TRUE, NULL);
63
64   g_assert (g_thread_self () != thread);
65
66   result = g_thread_join (thread);
67
68   g_assert (result == thread);
69 }
70
71 static gpointer
72 thread3_func (gpointer data)
73 {
74   GThread *peer = data;
75   gint retval;
76
77   retval = 3;
78
79   if (peer)
80     {
81       gpointer result;
82
83       result = g_thread_join (peer);
84
85       retval += GPOINTER_TO_INT (result);
86     }
87
88   return GINT_TO_POINTER (retval);
89 }
90
91 /* test that g_thread_join() works across peers */
92 static void
93 test_thread3 (void)
94 {
95   gpointer result;
96   GThread *thread1, *thread2, *thread3;
97
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);
101
102   result = g_thread_join (thread3);
103
104   g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9);
105 }
106
107 int
108 main (int argc, char *argv[])
109 {
110   g_test_init (&argc, &argv, NULL);
111
112   g_assert (g_thread_get_initialized ());
113
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);
117
118   return g_test_run ();
119 }