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.
25 #ifdef HAVE_SYS_TIME_H
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_PRCTL_H
30 #include <sys/prctl.h>
37 #include <sys/resource.h>
41 thread1_func (gpointer data)
43 g_thread_exit (GINT_TO_POINTER (1));
45 g_assert_not_reached ();
50 /* test that g_thread_exit() works */
58 thread = g_thread_try_new ("test", thread1_func, NULL, &error);
59 g_assert_no_error (error);
61 result = g_thread_join (thread);
63 g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
67 thread2_func (gpointer data)
69 return g_thread_self ();
72 /* test that g_thread_self() works */
79 thread = g_thread_new ("test", thread2_func, NULL);
81 g_assert (g_thread_self () != thread);
83 result = g_thread_join (thread);
85 g_assert (result == thread);
89 thread3_func (gpointer data)
100 result = g_thread_join (peer);
102 retval += GPOINTER_TO_INT (result);
105 return GINT_TO_POINTER (retval);
108 /* test that g_thread_join() works across peers */
113 GThread *thread1, *thread2, *thread3;
115 thread1 = g_thread_new ("a", thread3_func, NULL);
116 thread2 = g_thread_new ("b", thread3_func, thread1);
117 thread3 = g_thread_new ("c", thread3_func, thread2);
119 result = g_thread_join (thread3);
121 g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9);
124 /* test that thread creation fails as expected,
125 * by setting RLIMIT_NPROC ridiculously low
131 struct rlimit ol, nl;
136 /* Linux CAP_SYS_RESOURCE overrides RLIMIT_NPROC, and probably similar
137 * things are true on other systems.
139 if (getuid () == 0 || geteuid () == 0)
142 getrlimit (RLIMIT_NPROC, &nl);
145 if ((ret = prlimit (getpid(), RLIMIT_NPROC, &nl, &ol)) != 0)
146 g_error ("prlimit failed: %s\n", g_strerror (ret));
149 thread = g_thread_try_new ("a", thread1_func, NULL, &error);
150 g_assert (thread == NULL);
151 g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN);
152 g_error_free (error);
154 if ((ret = prlimit (getpid (), RLIMIT_NPROC, &ol, NULL)) != 0)
155 g_error ("resetting RLIMIT_NPROC failed: %s\n", g_strerror (ret));
164 thread = g_thread_new ("a", thread3_func, NULL);
165 g_thread_ref (thread);
166 g_thread_join (thread);
167 g_thread_unref (thread);
171 thread6_func (gpointer data)
173 #ifdef HAVE_SYS_PRCTL_H
175 const gchar name[16];
177 prctl (PR_GET_NAME, name, 0, 0, 0, 0);
179 g_assert_cmpstr (name, ==, (gchar*)data);
191 thread = g_thread_new ("abc", thread6_func, "abc");
192 g_thread_join (thread);
196 main (int argc, char *argv[])
198 g_test_init (&argc, &argv, NULL);
200 g_test_add_func ("/thread/thread1", test_thread1);
201 g_test_add_func ("/thread/thread2", test_thread2);
202 g_test_add_func ("/thread/thread3", test_thread3);
203 g_test_add_func ("/thread/thread4", test_thread4);
204 g_test_add_func ("/thread/thread5", test_thread5);
205 g_test_add_func ("/thread/thread6", test_thread6);
207 return g_test_run ();