1 /* Unit tests for GThread
2 * Copyright (C) 2011 Red Hat, Inc
3 * Author: Matthias Clasen
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
7 * This work is provided "as is"; redistribution and modification
8 * in whole or in part, in any medium, physical or electronic is
9 * permitted without restriction.
11 * This work is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * In no event shall the authors or contributors be liable for any
16 * direct, indirect, incidental, special, exemplary, or consequential
17 * damages (including, but not limited to, procurement of substitute
18 * goods or services; loss of use, data, or profits; or business
19 * interruption) however caused and on any theory of liability, whether
20 * in contract, strict liability, or tort (including negligence or
21 * otherwise) arising in any way out of the use of this software, even
22 * if advised of the possibility of such damage.
28 #ifdef HAVE_SYS_TIME_H
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
38 #include "glib/glib-private.h"
42 #include <sys/resource.h>
50 thread1_func (gpointer data)
52 g_thread_exit (GINT_TO_POINTER (1));
54 g_assert_not_reached ();
59 /* test that g_thread_exit() works */
67 thread = g_thread_try_new ("test", thread1_func, NULL, &error);
68 g_assert_no_error (error);
70 result = g_thread_join (thread);
72 g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
76 thread2_func (gpointer data)
78 return g_thread_self ();
81 /* test that g_thread_self() works */
88 thread = g_thread_new ("test", thread2_func, NULL);
90 g_assert (g_thread_self () != thread);
92 result = g_thread_join (thread);
94 g_assert (result == thread);
98 thread3_func (gpointer data)
100 GThread *peer = data;
109 result = g_thread_join (peer);
111 retval += GPOINTER_TO_INT (result);
114 return GINT_TO_POINTER (retval);
117 /* test that g_thread_join() works across peers */
122 GThread *thread1, *thread2, *thread3;
124 thread1 = g_thread_new ("a", thread3_func, NULL);
125 thread2 = g_thread_new ("b", thread3_func, thread1);
126 thread3 = g_thread_new ("c", thread3_func, thread2);
128 result = g_thread_join (thread3);
130 g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9);
133 /* test that thread creation fails as expected,
134 * by setting RLIMIT_NPROC ridiculously low
139 #ifdef _GLIB_ADDRESS_SANITIZER
140 g_test_incomplete ("FIXME: Leaks a GSystemThread's name, see glib#2308");
141 #elif defined(HAVE_PRLIMIT)
142 struct rlimit ol, nl;
146 getrlimit (RLIMIT_NPROC, &nl);
149 if (prlimit (getpid (), RLIMIT_NPROC, &nl, &ol) != 0)
150 g_error ("setting RLIMIT_NPROC to {cur=%ld,max=%ld} failed: %s",
151 (long) nl.rlim_cur, (long) nl.rlim_max, g_strerror (errno));
154 thread = g_thread_try_new ("a", thread1_func, NULL, &error);
160 /* Privileged processes might be able to create new threads even
161 * though the rlimit is too low. There isn't much we can do about
162 * this; we just can't test this failure mode in this situation. */
163 g_test_skip ("Unable to test g_thread_try_new() failing with EAGAIN "
164 "while privileged (CAP_SYS_RESOURCE, CAP_SYS_ADMIN or "
166 result = g_thread_join (thread);
167 g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
171 g_assert (thread == NULL);
172 g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN);
173 g_error_free (error);
176 if (prlimit (getpid (), RLIMIT_NPROC, &ol, NULL) != 0)
177 g_error ("resetting RLIMIT_NPROC failed: %s", g_strerror (errno));
186 thread = g_thread_new ("a", thread3_func, NULL);
187 g_thread_ref (thread);
188 g_thread_join (thread);
189 g_thread_unref (thread);
193 thread6_func (gpointer data)
195 #if defined (HAVE_PTHREAD_SETNAME_NP_WITH_TID) && defined (HAVE_PTHREAD_GETNAME_NP)
198 pthread_getname_np (pthread_self(), name, 16);
200 g_assert_cmpstr (name, ==, data);
211 thread = g_thread_new ("abc", thread6_func, "abc");
212 g_thread_join (thread);
215 #if defined(_SC_NPROCESSORS_ONLN) && defined(THREADS_POSIX) && defined(HAVE_PTHREAD_GETAFFINITY_NP)
217 thread7_func (gpointer data)
220 int ncores = sysconf (_SC_NPROCESSORS_ONLN);
222 cpu_set_t old_mask, new_mask;
224 err = pthread_getaffinity_np (pthread_self (), sizeof (old_mask), &old_mask);
225 CPU_ZERO (&new_mask);
226 g_assert_cmpint (err, ==, 0);
228 for (idx = 0; idx < ncores; ++idx)
229 if (CPU_ISSET (idx, &old_mask))
231 CPU_SET (idx, &new_mask);
235 err = pthread_setaffinity_np (pthread_self (), sizeof (new_mask), &new_mask);
236 g_assert_cmpint (err, ==, 0);
238 int af_count = g_get_num_processors ();
239 return GINT_TO_POINTER (af_count);
246 #if defined(_SC_NPROCESSORS_ONLN) && defined(THREADS_POSIX) && defined(HAVE_PTHREAD_GETAFFINITY_NP)
247 GThread *thread = g_thread_new ("mask", thread7_func, NULL);
248 gpointer result = g_thread_join (thread);
250 g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
252 g_test_skip ("Skipping because pthread_getaffinity_np() is not available");
257 main (int argc, char *argv[])
259 g_test_init (&argc, &argv, NULL);
261 g_test_add_func ("/thread/thread1", test_thread1);
262 g_test_add_func ("/thread/thread2", test_thread2);
263 g_test_add_func ("/thread/thread3", test_thread3);
264 g_test_add_func ("/thread/thread4", test_thread4);
265 g_test_add_func ("/thread/thread5", test_thread5);
266 g_test_add_func ("/thread/thread6", test_thread6);
267 g_test_add_func ("/thread/thread7", test_thread7);
269 return g_test_run ();