Add some more cases to the app-id unit tests
[glib.git] / glib / tests / thread.c
blob5447836928cef91fbd436f6bb7bdf82c7161be2d
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_SYS_PRCTL_H
30 #include <sys/prctl.h>
31 #endif
33 #include <glib.h>
35 #ifdef G_OS_UNIX
36 #include <unistd.h>
37 #include <sys/resource.h>
38 #endif
40 #ifdef THREADS_POSIX
41 #include <pthread.h>
42 #endif
44 static gpointer
45 thread1_func (gpointer data)
47 g_thread_exit (GINT_TO_POINTER (1));
49 g_assert_not_reached ();
51 return NULL;
54 /* test that g_thread_exit() works */
55 static void
56 test_thread1 (void)
58 gpointer result;
59 GThread *thread;
60 GError *error = NULL;
62 thread = g_thread_try_new ("test", thread1_func, NULL, &error);
63 g_assert_no_error (error);
65 result = g_thread_join (thread);
67 g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
70 static gpointer
71 thread2_func (gpointer data)
73 return g_thread_self ();
76 /* test that g_thread_self() works */
77 static void
78 test_thread2 (void)
80 gpointer result;
81 GThread *thread;
83 thread = g_thread_new ("test", thread2_func, NULL);
85 g_assert (g_thread_self () != thread);
87 result = g_thread_join (thread);
89 g_assert (result == thread);
92 static gpointer
93 thread3_func (gpointer data)
95 GThread *peer = data;
96 gint retval;
98 retval = 3;
100 if (peer)
102 gpointer result;
104 result = g_thread_join (peer);
106 retval += GPOINTER_TO_INT (result);
109 return GINT_TO_POINTER (retval);
112 /* test that g_thread_join() works across peers */
113 static void
114 test_thread3 (void)
116 gpointer result;
117 GThread *thread1, *thread2, *thread3;
119 thread1 = g_thread_new ("a", thread3_func, NULL);
120 thread2 = g_thread_new ("b", thread3_func, thread1);
121 thread3 = g_thread_new ("c", thread3_func, thread2);
123 result = g_thread_join (thread3);
125 g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9);
128 /* test that thread creation fails as expected,
129 * by setting RLIMIT_NPROC ridiculously low
131 static void
132 test_thread4 (void)
134 #ifdef HAVE_PRLIMIT
135 struct rlimit ol, nl;
136 GThread *thread;
137 GError *error;
138 gint ret;
140 /* Linux CAP_SYS_RESOURCE overrides RLIMIT_NPROC, and probably similar
141 * things are true on other systems.
143 if (getuid () == 0 || geteuid () == 0)
144 return;
146 getrlimit (RLIMIT_NPROC, &nl);
147 nl.rlim_cur = 1;
149 if ((ret = prlimit (getpid(), RLIMIT_NPROC, &nl, &ol)) != 0)
150 g_error ("prlimit failed: %s\n", g_strerror (ret));
152 error = NULL;
153 thread = g_thread_try_new ("a", thread1_func, NULL, &error);
154 g_assert (thread == NULL);
155 g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN);
156 g_error_free (error);
158 if ((ret = prlimit (getpid (), RLIMIT_NPROC, &ol, NULL)) != 0)
159 g_error ("resetting RLIMIT_NPROC failed: %s\n", g_strerror (ret));
160 #endif
163 static void
164 test_thread5 (void)
166 GThread *thread;
168 thread = g_thread_new ("a", thread3_func, NULL);
169 g_thread_ref (thread);
170 g_thread_join (thread);
171 g_thread_unref (thread);
174 static gpointer
175 thread6_func (gpointer data)
177 #ifdef HAVE_PTHREAD_SETNAME_NP_WITH_TID
178 char name[16];
180 pthread_getname_np (pthread_self(), name, 16);
182 g_assert_cmpstr (name, ==, data);
183 #endif
185 return NULL;
188 static void
189 test_thread6 (void)
191 GThread *thread;
193 thread = g_thread_new ("abc", thread6_func, "abc");
194 g_thread_join (thread);
198 main (int argc, char *argv[])
200 g_test_init (&argc, &argv, NULL);
202 g_test_add_func ("/thread/thread1", test_thread1);
203 g_test_add_func ("/thread/thread2", test_thread2);
204 g_test_add_func ("/thread/thread3", test_thread3);
205 g_test_add_func ("/thread/thread4", test_thread4);
206 g_test_add_func ("/thread/thread5", test_thread5);
207 g_test_add_func ("/thread/thread6", test_thread6);
209 return g_test_run ();