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 #include <sys/resource.h>
27 #include <sys/types.h>
33 thread1_func (gpointer data)
35 g_thread_exit (GINT_TO_POINTER (1));
37 g_assert_not_reached ();
42 /* test that g_thread_exit() works */
50 thread = g_thread_new ("test", thread1_func, NULL, TRUE, &error);
51 g_assert_no_error (error);
53 result = g_thread_join (thread);
55 g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
59 thread2_func (gpointer data)
61 return g_thread_self ();
64 /* test that g_thread_self() works */
71 thread = g_thread_new ("test", thread2_func, NULL, TRUE, NULL);
73 g_assert (g_thread_self () != thread);
75 result = g_thread_join (thread);
77 g_assert (result == thread);
81 thread3_func (gpointer data)
92 result = g_thread_join (peer);
94 retval += GPOINTER_TO_INT (result);
97 return GINT_TO_POINTER (retval);
100 /* test that g_thread_join() works across peers */
105 GThread *thread1, *thread2, *thread3;
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);
111 result = g_thread_join (thread3);
113 g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9);
116 /* test that thread creation fails as expected,
117 * by setting RLIMIT_NPROC ridiculously low
123 struct rlimit ol, nl;
131 if ((ret = prlimit (getpid(), RLIMIT_NPROC, &nl, &ol)) != 0)
132 g_error ("prlimit failed: %s\n", g_strerror (ret));
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);
140 prlimit (getpid (), RLIMIT_NPROC, &ol, NULL);
145 main (int argc, char *argv[])
147 g_test_init (&argc, &argv, NULL);
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);
154 return g_test_run ();