Disable deprecations where appropriate in 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 <config.h>
24
25 #include <sys/time.h>
26 #include <sys/resource.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 #include <glib.h>
31
32 static gpointer
33 thread1_func (gpointer data)
34 {
35   g_thread_exit (GINT_TO_POINTER (1));
36
37   g_assert_not_reached ();
38
39   return NULL;
40 }
41
42 /* test that g_thread_exit() works */
43 static void
44 test_thread1 (void)
45 {
46   gpointer result;
47   GThread *thread;
48   GError *error = NULL;
49
50   thread = g_thread_new ("test", thread1_func, NULL, TRUE, &error);
51   g_assert_no_error (error);
52
53   result = g_thread_join (thread);
54
55   g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
56 }
57
58 static gpointer
59 thread2_func (gpointer data)
60 {
61   return g_thread_self ();
62 }
63
64 /* test that g_thread_self() works */
65 static void
66 test_thread2 (void)
67 {
68   gpointer result;
69   GThread *thread;
70
71   thread = g_thread_new ("test", thread2_func, NULL, TRUE, NULL);
72
73   g_assert (g_thread_self () != thread);
74
75   result = g_thread_join (thread);
76
77   g_assert (result == thread);
78 }
79
80 static gpointer
81 thread3_func (gpointer data)
82 {
83   GThread *peer = data;
84   gint retval;
85
86   retval = 3;
87
88   if (peer)
89     {
90       gpointer result;
91
92       result = g_thread_join (peer);
93
94       retval += GPOINTER_TO_INT (result);
95     }
96
97   return GINT_TO_POINTER (retval);
98 }
99
100 /* test that g_thread_join() works across peers */
101 static void
102 test_thread3 (void)
103 {
104   gpointer result;
105   GThread *thread1, *thread2, *thread3;
106
107   thread1 = g_thread_new_full ("a", thread3_func, NULL, TRUE, 0, NULL);
108   thread2 = g_thread_new_full ("b", thread3_func, thread1, TRUE, 100, NULL);
109   thread3 = g_thread_new_full ("c", thread3_func, thread2, TRUE, 100000, NULL);
110
111   result = g_thread_join (thread3);
112
113   g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9);
114 }
115
116 /* test that thread creation fails as expected,
117  * by setting RLIMIT_NPROC ridiculously low
118  */
119 static void
120 test_thread4 (void)
121 {
122 #ifdef HAVE_PRLIMIT
123   struct rlimit ol, nl;
124   GThread *thread;
125   GError *error;
126   gint ret;
127
128   nl.rlim_cur = 1;
129   nl.rlim_max = 1;
130
131   if ((ret = prlimit (getpid(), RLIMIT_NPROC, &nl, &ol)) != 0)
132     g_error ("prlimit failed: %s\n", g_strerror (ret));
133
134   error = NULL;
135   thread = g_thread_new ("a", thread1_func, NULL, FALSE, &error);
136   g_assert (thread == NULL);
137   g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN);
138   g_error_free (error);
139
140   prlimit (getpid (), RLIMIT_NPROC, &ol, NULL);
141 #endif
142 }
143
144 int
145 main (int argc, char *argv[])
146 {
147   g_test_init (&argc, &argv, NULL);
148
149   g_test_add_func ("/thread/thread1", test_thread1);
150   g_test_add_func ("/thread/thread2", test_thread2);
151   g_test_add_func ("/thread/thread3", test_thread3);
152   g_test_add_func ("/thread/thread4", test_thread4);
153
154   return g_test_run ();
155 }