Imported Upstream version 2.73.3
[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 #include <errno.h>
25
26 #ifdef HAVE_SYS_TIME_H
27 #include <sys/time.h>
28 #endif
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_PRCTL_H
31 #include <sys/prctl.h>
32 #endif
33
34 #include <glib.h>
35
36 #include "glib/glib-private.h"
37
38 #ifdef G_OS_UNIX
39 #include <unistd.h>
40 #include <sys/resource.h>
41 #endif
42
43 #ifdef THREADS_POSIX
44 #include <pthread.h>
45 #endif
46
47 static gpointer
48 thread1_func (gpointer data)
49 {
50   g_thread_exit (GINT_TO_POINTER (1));
51
52   g_assert_not_reached ();
53
54   return NULL;
55 }
56
57 /* test that g_thread_exit() works */
58 static void
59 test_thread1 (void)
60 {
61   gpointer result;
62   GThread *thread;
63   GError *error = NULL;
64
65   thread = g_thread_try_new ("test", thread1_func, NULL, &error);
66   g_assert_no_error (error);
67
68   result = g_thread_join (thread);
69
70   g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
71 }
72
73 static gpointer
74 thread2_func (gpointer data)
75 {
76   return g_thread_self ();
77 }
78
79 /* test that g_thread_self() works */
80 static void
81 test_thread2 (void)
82 {
83   gpointer result;
84   GThread *thread;
85
86   thread = g_thread_new ("test", thread2_func, NULL);
87
88   g_assert (g_thread_self () != thread);
89
90   result = g_thread_join (thread);
91
92   g_assert (result == thread);
93 }
94
95 static gpointer
96 thread3_func (gpointer data)
97 {
98   GThread *peer = data;
99   gint retval;
100
101   retval = 3;
102
103   if (peer)
104     {
105       gpointer result;
106
107       result = g_thread_join (peer);
108
109       retval += GPOINTER_TO_INT (result);
110     }
111
112   return GINT_TO_POINTER (retval);
113 }
114
115 /* test that g_thread_join() works across peers */
116 static void
117 test_thread3 (void)
118 {
119   gpointer result;
120   GThread *thread1, *thread2, *thread3;
121
122   thread1 = g_thread_new ("a", thread3_func, NULL);
123   thread2 = g_thread_new ("b", thread3_func, thread1);
124   thread3 = g_thread_new ("c", thread3_func, thread2);
125
126   result = g_thread_join (thread3);
127
128   g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9);
129 }
130
131 /* test that thread creation fails as expected,
132  * by setting RLIMIT_NPROC ridiculously low
133  */
134 static void
135 test_thread4 (void)
136 {
137 #ifdef _GLIB_ADDRESS_SANITIZER
138   g_test_incomplete ("FIXME: Leaks a GSystemThread's name, see glib#2308");
139 #elif defined(HAVE_PRLIMIT)
140   struct rlimit ol, nl;
141   GThread *thread;
142   GError *error;
143
144   getrlimit (RLIMIT_NPROC, &nl);
145   nl.rlim_cur = 1;
146
147   if (prlimit (getpid (), RLIMIT_NPROC, &nl, &ol) != 0)
148     g_error ("prlimit failed: %s", g_strerror (errno));
149
150   error = NULL;
151   thread = g_thread_try_new ("a", thread1_func, NULL, &error);
152
153   if (thread != NULL)
154     {
155       gpointer result;
156
157       /* Privileged processes might be able to create new threads even
158        * though the rlimit is too low. There isn't much we can do about
159        * this; we just can't test this failure mode in this situation. */
160       g_test_skip ("Unable to test g_thread_try_new() failing with EAGAIN "
161                    "while privileged (CAP_SYS_RESOURCE, CAP_SYS_ADMIN or "
162                    "euid 0?)");
163       result = g_thread_join (thread);
164       g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
165     }
166   else
167     {
168       g_assert (thread == NULL);
169       g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN);
170       g_error_free (error);
171     }
172
173   if (prlimit (getpid (), RLIMIT_NPROC, &ol, NULL) != 0)
174     g_error ("resetting RLIMIT_NPROC failed: %s", g_strerror (errno));
175 #endif
176 }
177
178 static void
179 test_thread5 (void)
180 {
181   GThread *thread;
182
183   thread = g_thread_new ("a", thread3_func, NULL);
184   g_thread_ref (thread);
185   g_thread_join (thread);
186   g_thread_unref (thread);
187 }
188
189 static gpointer
190 thread6_func (gpointer data)
191 {
192 #if defined (HAVE_PTHREAD_SETNAME_NP_WITH_TID) && defined (HAVE_PTHREAD_GETNAME_NP)
193   char name[16];
194
195   pthread_getname_np (pthread_self(), name, 16);
196
197   g_assert_cmpstr (name, ==, data);
198 #endif
199
200   return NULL;
201 }
202
203 static void
204 test_thread6 (void)
205 {
206   GThread *thread;
207
208   thread = g_thread_new ("abc", thread6_func, "abc");
209   g_thread_join (thread);
210 }
211
212 int
213 main (int argc, char *argv[])
214 {
215   g_test_init (&argc, &argv, NULL);
216
217   g_test_add_func ("/thread/thread1", test_thread1);
218   g_test_add_func ("/thread/thread2", test_thread2);
219   g_test_add_func ("/thread/thread3", test_thread3);
220   g_test_add_func ("/thread/thread4", test_thread4);
221   g_test_add_func ("/thread/thread5", test_thread5);
222   g_test_add_func ("/thread/thread6", test_thread6);
223
224   return g_test_run ();
225 }