Meson: Group all glib tests into a single dict
[glib.git] / glib / tests / thread.c
blobb9f87967b5987227bdedabfd621dbdfc2e3f8f1a
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>
24 #include <errno.h>
26 #ifdef HAVE_SYS_TIME_H
27 #include <sys/time.h>
28 #endif
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_PRCTL_H
31 #include <sys/prctl.h>
32 #endif
34 #include <glib.h>
36 #ifdef G_OS_UNIX
37 #include <unistd.h>
38 #include <sys/resource.h>
39 #endif
41 #ifdef THREADS_POSIX
42 #include <pthread.h>
43 #endif
45 static gpointer
46 thread1_func (gpointer data)
48 g_thread_exit (GINT_TO_POINTER (1));
50 g_assert_not_reached ();
52 return NULL;
55 /* test that g_thread_exit() works */
56 static void
57 test_thread1 (void)
59 gpointer result;
60 GThread *thread;
61 GError *error = NULL;
63 thread = g_thread_try_new ("test", thread1_func, NULL, &error);
64 g_assert_no_error (error);
66 result = g_thread_join (thread);
68 g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
71 static gpointer
72 thread2_func (gpointer data)
74 return g_thread_self ();
77 /* test that g_thread_self() works */
78 static void
79 test_thread2 (void)
81 gpointer result;
82 GThread *thread;
84 thread = g_thread_new ("test", thread2_func, NULL);
86 g_assert (g_thread_self () != thread);
88 result = g_thread_join (thread);
90 g_assert (result == thread);
93 static gpointer
94 thread3_func (gpointer data)
96 GThread *peer = data;
97 gint retval;
99 retval = 3;
101 if (peer)
103 gpointer result;
105 result = g_thread_join (peer);
107 retval += GPOINTER_TO_INT (result);
110 return GINT_TO_POINTER (retval);
113 /* test that g_thread_join() works across peers */
114 static void
115 test_thread3 (void)
117 gpointer result;
118 GThread *thread1, *thread2, *thread3;
120 thread1 = g_thread_new ("a", thread3_func, NULL);
121 thread2 = g_thread_new ("b", thread3_func, thread1);
122 thread3 = g_thread_new ("c", thread3_func, thread2);
124 result = g_thread_join (thread3);
126 g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9);
129 /* test that thread creation fails as expected,
130 * by setting RLIMIT_NPROC ridiculously low
132 static void
133 test_thread4 (void)
135 #ifdef HAVE_PRLIMIT
136 struct rlimit ol, nl;
137 GThread *thread;
138 GError *error;
139 gint ret;
141 /* Linux CAP_SYS_RESOURCE overrides RLIMIT_NPROC, and probably similar
142 * things are true on other systems.
144 if (getuid () == 0 || geteuid () == 0)
145 return;
147 getrlimit (RLIMIT_NPROC, &nl);
148 nl.rlim_cur = 1;
150 if ((ret = prlimit (getpid (), RLIMIT_NPROC, &nl, &ol)) != 0)
151 g_error ("prlimit failed: %s", g_strerror (errno));
153 error = NULL;
154 thread = g_thread_try_new ("a", thread1_func, NULL, &error);
155 g_assert (thread == NULL);
156 g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN);
157 g_error_free (error);
159 if ((ret = prlimit (getpid (), RLIMIT_NPROC, &ol, NULL)) != 0)
160 g_error ("resetting RLIMIT_NPROC failed: %s", g_strerror (errno));
161 #endif
164 static void
165 test_thread5 (void)
167 GThread *thread;
169 thread = g_thread_new ("a", thread3_func, NULL);
170 g_thread_ref (thread);
171 g_thread_join (thread);
172 g_thread_unref (thread);
175 static gpointer
176 thread6_func (gpointer data)
178 #if defined (HAVE_PTHREAD_SETNAME_NP_WITH_TID) && defined (HAVE_PTHREAD_GETNAME_NP)
179 char name[16];
181 pthread_getname_np (pthread_self(), name, 16);
183 g_assert_cmpstr (name, ==, data);
184 #endif
186 return NULL;
189 static void
190 test_thread6 (void)
192 GThread *thread;
194 thread = g_thread_new ("abc", thread6_func, "abc");
195 g_thread_join (thread);
199 main (int argc, char *argv[])
201 g_test_init (&argc, &argv, NULL);
203 g_test_add_func ("/thread/thread1", test_thread1);
204 g_test_add_func ("/thread/thread2", test_thread2);
205 g_test_add_func ("/thread/thread3", test_thread3);
206 g_test_add_func ("/thread/thread4", test_thread4);
207 g_test_add_func ("/thread/thread5", test_thread5);
208 g_test_add_func ("/thread/thread6", test_thread6);
210 return g_test_run ();