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>
45 thread1_func (gpointer data
)
47 g_thread_exit (GINT_TO_POINTER (1));
49 g_assert_not_reached ();
54 /* test that g_thread_exit() works */
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);
71 thread2_func (gpointer data
)
73 return g_thread_self ();
76 /* test that g_thread_self() works */
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
);
93 thread3_func (gpointer data
)
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 */
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
135 struct rlimit ol
, nl
;
140 /* Linux CAP_SYS_RESOURCE overrides RLIMIT_NPROC, and probably similar
141 * things are true on other systems.
143 if (getuid () == 0 || geteuid () == 0)
146 getrlimit (RLIMIT_NPROC
, &nl
);
149 if ((ret
= prlimit (getpid(), RLIMIT_NPROC
, &nl
, &ol
)) != 0)
150 g_error ("prlimit failed: %s\n", g_strerror (ret
));
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
));
168 thread
= g_thread_new ("a", thread3_func
, NULL
);
169 g_thread_ref (thread
);
170 g_thread_join (thread
);
171 g_thread_unref (thread
);
175 thread6_func (gpointer data
)
177 #ifdef HAVE_PTHREAD_SETNAME_NP_WITH_TID
180 pthread_getname_np (pthread_self(), name
, 16);
182 g_assert_cmpstr (name
, ==, data
);
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 ();