Simplify glib/glib/tests setup
[glib.git] / glib / tests / thread.c
blobf91fcae5b04ad7ef365264272d171add187a0029
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.
23 #include <config.h>
25 #ifdef HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
28 #include <sys/types.h>
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
34 #endif
37 #include <glib.h>
39 #ifndef G_OS_WIN32
40 #include <sys/resource.h>
41 #endif
43 static gpointer
44 thread1_func (gpointer data)
46 g_thread_exit (GINT_TO_POINTER (1));
48 g_assert_not_reached ();
50 return NULL;
53 /* test that g_thread_exit() works */
54 static void
55 test_thread1 (void)
57 gpointer result;
58 GThread *thread;
59 GError *error = NULL;
61 thread = g_thread_try_new ("test", thread1_func, NULL, &error);
62 g_assert_no_error (error);
64 result = g_thread_join (thread);
66 g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
69 static gpointer
70 thread2_func (gpointer data)
72 return g_thread_self ();
75 /* test that g_thread_self() works */
76 static void
77 test_thread2 (void)
79 gpointer result;
80 GThread *thread;
82 thread = g_thread_new ("test", thread2_func, NULL);
84 g_assert (g_thread_self () != thread);
86 result = g_thread_join (thread);
88 g_assert (result == thread);
91 static gpointer
92 thread3_func (gpointer data)
94 GThread *peer = data;
95 gint retval;
97 retval = 3;
99 if (peer)
101 gpointer result;
103 result = g_thread_join (peer);
105 retval += GPOINTER_TO_INT (result);
108 return GINT_TO_POINTER (retval);
111 /* test that g_thread_join() works across peers */
112 static void
113 test_thread3 (void)
115 gpointer result;
116 GThread *thread1, *thread2, *thread3;
118 thread1 = g_thread_new ("a", thread3_func, NULL);
119 thread2 = g_thread_new ("b", thread3_func, thread1);
120 thread3 = g_thread_new ("c", thread3_func, thread2);
122 result = g_thread_join (thread3);
124 g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9);
127 /* test that thread creation fails as expected,
128 * by setting RLIMIT_NPROC ridiculously low
130 static void
131 test_thread4 (void)
133 #ifdef HAVE_PRLIMIT
134 struct rlimit ol, nl;
135 GThread *thread;
136 GError *error;
137 gint ret;
139 getrlimit (RLIMIT_NPROC, &nl);
140 nl.rlim_cur = 1;
142 if ((ret = prlimit (getpid(), RLIMIT_NPROC, &nl, &ol)) != 0)
143 g_error ("prlimit failed: %s\n", g_strerror (ret));
145 error = NULL;
146 thread = g_thread_try_new ("a", thread1_func, NULL, &error);
147 g_assert (thread == NULL);
148 g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN);
149 g_error_free (error);
151 if ((ret = prlimit (getpid (), RLIMIT_NPROC, &ol, NULL)) != 0)
152 g_error ("resetting RLIMIT_NPROC failed: %s\n", g_strerror (ret));
153 #endif
156 static void
157 test_thread5 (void)
159 GThread *thread;
161 thread = g_thread_new ("a", thread3_func, NULL);
162 g_thread_ref (thread);
163 g_thread_join (thread);
164 g_thread_unref (thread);
167 static gpointer
168 thread6_func (gpointer data)
170 #ifdef HAVE_SYS_PRCTL_H
171 #ifdef PR_GET_NAME
172 const gchar name[16];
174 prctl (PR_GET_NAME, name, 0, 0, 0, 0);
176 g_assert_cmpstr (name, ==, (gchar*)data);
177 #endif
178 #endif
180 return NULL;
183 static void
184 test_thread6 (void)
186 GThread *thread;
188 thread = g_thread_new ("abc", thread6_func, "abc");
189 g_thread_join (thread);
193 main (int argc, char *argv[])
195 g_test_init (&argc, &argv, NULL);
197 g_test_add_func ("/thread/thread1", test_thread1);
198 g_test_add_func ("/thread/thread2", test_thread2);
199 g_test_add_func ("/thread/thread3", test_thread3);
200 g_test_add_func ("/thread/thread4", test_thread4);
201 g_test_add_func ("/thread/thread5", test_thread5);
202 g_test_add_func ("/thread/thread6", test_thread6);
204 return g_test_run ();