2011-10-03 22:22:55 -04:00
|
|
|
/* Unit tests for GThread
|
|
|
|
* Copyright (C) 2011 Red Hat, Inc
|
|
|
|
* Author: Matthias Clasen
|
|
|
|
*
|
|
|
|
* This work is provided "as is"; redistribution and modification
|
|
|
|
* in whole or in part, in any medium, physical or electronic is
|
|
|
|
* permitted without restriction.
|
|
|
|
*
|
|
|
|
* This work is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
* In no event shall the authors or contributors be liable for any
|
|
|
|
* direct, indirect, incidental, special, exemplary, or consequential
|
|
|
|
* damages (including, but not limited to, procurement of substitute
|
|
|
|
* goods or services; loss of use, data, or profits; or business
|
|
|
|
* interruption) however caused and on any theory of liability, whether
|
|
|
|
* in contract, strict liability, or tort (including negligence or
|
|
|
|
* otherwise) arising in any way out of the use of this software, even
|
|
|
|
* if advised of the possibility of such damage.
|
|
|
|
*/
|
|
|
|
|
2011-10-05 00:28:53 -04:00
|
|
|
#include <config.h>
|
2018-07-10 16:14:12 +02:00
|
|
|
#include <errno.h>
|
2011-10-05 00:28:53 -04:00
|
|
|
|
2011-10-17 17:18:20 +08:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2011-10-05 00:28:53 -04:00
|
|
|
#include <sys/time.h>
|
2011-10-17 17:18:20 +08:00
|
|
|
#endif
|
2011-10-05 00:28:53 -04:00
|
|
|
#include <sys/types.h>
|
2011-11-14 22:18:13 -05:00
|
|
|
#ifdef HAVE_SYS_PRCTL_H
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
|
2011-10-03 22:22:55 -04:00
|
|
|
#include <glib.h>
|
|
|
|
|
Replace #ifdef HAVE_UNISTD_H checks with #ifdef G_OS_UNIX
In Windows development environments that have it, <unistd.h> is mostly
just a wrapper around several other native headers (in particular,
<io.h>, which contains read(), close(), etc, and <process.h>, which
contains getpid()). But given that some Windows dev environments don't
have <unistd.h>, everything that uses those functions on Windows
already needed to include the correct Windows header as well, and so
there is never any point to including <unistd.h> on Windows.
Also, remove some <unistd.h> includes (and a few others) that were
unnecessary even on unix.
https://bugzilla.gnome.org/show_bug.cgi?id=710519
2013-10-19 13:04:00 -04:00
|
|
|
#ifdef G_OS_UNIX
|
2013-11-04 13:07:52 +08:00
|
|
|
#include <unistd.h>
|
2011-10-12 23:25:12 -04:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
|
|
|
|
2016-05-14 01:17:26 +00:00
|
|
|
#ifdef THREADS_POSIX
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2011-10-03 22:22:55 -04:00
|
|
|
static gpointer
|
|
|
|
thread1_func (gpointer data)
|
|
|
|
{
|
|
|
|
g_thread_exit (GINT_TO_POINTER (1));
|
|
|
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test that g_thread_exit() works */
|
|
|
|
static void
|
|
|
|
test_thread1 (void)
|
|
|
|
{
|
|
|
|
gpointer result;
|
|
|
|
GThread *thread;
|
2011-10-04 00:46:10 -04:00
|
|
|
GError *error = NULL;
|
2011-10-03 22:22:55 -04:00
|
|
|
|
2011-10-15 09:48:10 -04:00
|
|
|
thread = g_thread_try_new ("test", thread1_func, NULL, &error);
|
2011-10-04 00:46:10 -04:00
|
|
|
g_assert_no_error (error);
|
2011-10-03 22:22:55 -04:00
|
|
|
|
|
|
|
result = g_thread_join (thread);
|
|
|
|
|
|
|
|
g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
thread2_func (gpointer data)
|
|
|
|
{
|
|
|
|
return g_thread_self ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test that g_thread_self() works */
|
|
|
|
static void
|
|
|
|
test_thread2 (void)
|
|
|
|
{
|
|
|
|
gpointer result;
|
|
|
|
GThread *thread;
|
|
|
|
|
2011-10-13 01:00:57 -04:00
|
|
|
thread = g_thread_new ("test", thread2_func, NULL);
|
2011-10-03 22:22:55 -04:00
|
|
|
|
|
|
|
g_assert (g_thread_self () != thread);
|
|
|
|
|
|
|
|
result = g_thread_join (thread);
|
|
|
|
|
|
|
|
g_assert (result == thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
thread3_func (gpointer data)
|
|
|
|
{
|
|
|
|
GThread *peer = data;
|
|
|
|
gint retval;
|
|
|
|
|
|
|
|
retval = 3;
|
|
|
|
|
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
gpointer result;
|
|
|
|
|
|
|
|
result = g_thread_join (peer);
|
|
|
|
|
|
|
|
retval += GPOINTER_TO_INT (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GINT_TO_POINTER (retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test that g_thread_join() works across peers */
|
|
|
|
static void
|
|
|
|
test_thread3 (void)
|
|
|
|
{
|
|
|
|
gpointer result;
|
|
|
|
GThread *thread1, *thread2, *thread3;
|
|
|
|
|
2011-10-13 01:17:36 -04:00
|
|
|
thread1 = g_thread_new ("a", thread3_func, NULL);
|
|
|
|
thread2 = g_thread_new ("b", thread3_func, thread1);
|
|
|
|
thread3 = g_thread_new ("c", thread3_func, thread2);
|
2011-10-03 22:22:55 -04:00
|
|
|
|
|
|
|
result = g_thread_join (thread3);
|
|
|
|
|
|
|
|
g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9);
|
|
|
|
}
|
|
|
|
|
2011-10-05 00:28:53 -04:00
|
|
|
/* test that thread creation fails as expected,
|
|
|
|
* by setting RLIMIT_NPROC ridiculously low
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
test_thread4 (void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_PRLIMIT
|
|
|
|
struct rlimit ol, nl;
|
|
|
|
GThread *thread;
|
|
|
|
GError *error;
|
|
|
|
gint ret;
|
|
|
|
|
2013-02-21 10:19:27 +00:00
|
|
|
/* Linux CAP_SYS_RESOURCE overrides RLIMIT_NPROC, and probably similar
|
|
|
|
* things are true on other systems.
|
|
|
|
*/
|
|
|
|
if (getuid () == 0 || geteuid () == 0)
|
|
|
|
return;
|
|
|
|
|
2011-10-14 23:20:06 -04:00
|
|
|
getrlimit (RLIMIT_NPROC, &nl);
|
2011-10-05 00:28:53 -04:00
|
|
|
nl.rlim_cur = 1;
|
|
|
|
|
2018-07-10 16:14:12 +02:00
|
|
|
if ((ret = prlimit (getpid (), RLIMIT_NPROC, &nl, &ol)) != 0)
|
|
|
|
g_error ("prlimit failed: %s", g_strerror (errno));
|
2011-10-05 00:28:53 -04:00
|
|
|
|
|
|
|
error = NULL;
|
2011-10-15 09:48:10 -04:00
|
|
|
thread = g_thread_try_new ("a", thread1_func, NULL, &error);
|
2011-10-05 00:28:53 -04:00
|
|
|
g_assert (thread == NULL);
|
|
|
|
g_assert_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN);
|
|
|
|
g_error_free (error);
|
|
|
|
|
2011-10-14 23:20:06 -04:00
|
|
|
if ((ret = prlimit (getpid (), RLIMIT_NPROC, &ol, NULL)) != 0)
|
2018-07-10 16:14:12 +02:00
|
|
|
g_error ("resetting RLIMIT_NPROC failed: %s", g_strerror (errno));
|
2011-10-05 00:28:53 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-10-14 23:20:06 -04:00
|
|
|
static void
|
|
|
|
test_thread5 (void)
|
|
|
|
{
|
|
|
|
GThread *thread;
|
|
|
|
|
|
|
|
thread = g_thread_new ("a", thread3_func, NULL);
|
|
|
|
g_thread_ref (thread);
|
|
|
|
g_thread_join (thread);
|
|
|
|
g_thread_unref (thread);
|
|
|
|
}
|
|
|
|
|
2011-11-14 22:18:13 -05:00
|
|
|
static gpointer
|
|
|
|
thread6_func (gpointer data)
|
|
|
|
{
|
2018-04-23 10:33:44 -04:00
|
|
|
#if defined (HAVE_PTHREAD_SETNAME_NP_WITH_TID) && defined (HAVE_PTHREAD_GETNAME_NP)
|
2016-04-26 06:35:06 -04:00
|
|
|
char name[16];
|
2011-11-14 22:18:13 -05:00
|
|
|
|
2016-04-26 06:35:06 -04:00
|
|
|
pthread_getname_np (pthread_self(), name, 16);
|
2011-11-14 22:18:13 -05:00
|
|
|
|
2016-04-26 06:35:06 -04:00
|
|
|
g_assert_cmpstr (name, ==, data);
|
2011-11-14 22:18:13 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_thread6 (void)
|
|
|
|
{
|
|
|
|
GThread *thread;
|
|
|
|
|
|
|
|
thread = g_thread_new ("abc", thread6_func, "abc");
|
|
|
|
g_thread_join (thread);
|
|
|
|
}
|
|
|
|
|
2011-10-03 22:22:55 -04:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
|
|
|
|
g_test_add_func ("/thread/thread1", test_thread1);
|
|
|
|
g_test_add_func ("/thread/thread2", test_thread2);
|
|
|
|
g_test_add_func ("/thread/thread3", test_thread3);
|
2011-10-05 00:28:53 -04:00
|
|
|
g_test_add_func ("/thread/thread4", test_thread4);
|
2011-10-14 23:20:06 -04:00
|
|
|
g_test_add_func ("/thread/thread5", test_thread5);
|
2011-11-14 22:18:13 -05:00
|
|
|
g_test_add_func ("/thread/thread6", test_thread6);
|
2011-10-03 22:22:55 -04:00
|
|
|
|
|
|
|
return g_test_run ();
|
|
|
|
}
|