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 #ifdef HAVE_SYS_TIME_H
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_PRCTL_H
31 #include <sys/prctl.h>
36 #include "glib/glib-private.h"
40 #include <sys/resource.h>
48 thread1_func (gpointer data)
50 g_thread_exit (GINT_TO_POINTER (1));
52 g_assert_not_reached ();
57 /* test that g_thread_exit() works */
65 thread = g_thread_try_new ("test", thread1_func, NULL, &error);
66 g_assert_no_error (error);
68 result = g_thread_join (thread);
70 g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
74 thread2_func (gpointer data)
76 return g_thread_self ();
79 /* test that g_thread_self() works */
86 thread = g_thread_new ("test", thread2_func, NULL);
88 g_assert (g_thread_self () != thread);
90 result = g_thread_join (thread);
92 g_assert (result == thread);
96 thread3_func (gpointer data)
107 result = g_thread_join (peer);
109 retval += GPOINTER_TO_INT (result);
112 return GINT_TO_POINTER (retval);
115 /* test that g_thread_join() works across peers */
120 GThread *thread1, *thread2, *thread3;
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);
126 result = g_thread_join (thread3);
128 g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9);
131 /* test that thread creation fails as expected,
132 * by setting RLIMIT_NPROC ridiculously low
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;
144 getrlimit (RLIMIT_NPROC, &nl);
147 if (prlimit (getpid (), RLIMIT_NPROC, &nl, &ol) != 0)
148 g_error ("prlimit failed: %s", g_strerror (errno));
151 thread = g_thread_try_new ("a", thread1_func, NULL, &error);
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 "
163 result = g_thread_join (thread);
164 g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
168 g_assert (thread == NULL);
169 g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN);
170 g_error_free (error);
173 if (prlimit (getpid (), RLIMIT_NPROC, &ol, NULL) != 0)
174 g_error ("resetting RLIMIT_NPROC failed: %s", g_strerror (errno));
183 thread = g_thread_new ("a", thread3_func, NULL);
184 g_thread_ref (thread);
185 g_thread_join (thread);
186 g_thread_unref (thread);
190 thread6_func (gpointer data)
192 #if defined (HAVE_PTHREAD_SETNAME_NP_WITH_TID) && defined (HAVE_PTHREAD_GETNAME_NP)
195 pthread_getname_np (pthread_self(), name, 16);
197 g_assert_cmpstr (name, ==, data);
208 thread = g_thread_new ("abc", thread6_func, "abc");
209 g_thread_join (thread);
213 main (int argc, char *argv[])
215 g_test_init (&argc, &argv, NULL);
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);
224 return g_test_run ();