1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/* Verify that pthread_create does not deadlock when ctors take locks.
Copyright (C) 2021-2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <support/xdlfcn.h>
#include <support/xthread.h>
/*
Check if ctor and pthread_create deadlocks in
thread 1: dlopen -> ctor -> lock(user_lock)
thread 2: lock(user_lock) -> pthread_create
or in
thread 1: dlclose -> dtor -> lock(user_lock)
thread 2: lock(user_lock) -> pthread_create
*/
static pthread_barrier_t bar_ctor;
static pthread_barrier_t bar_ctor_finish;
static pthread_barrier_t bar_dtor;
static pthread_mutex_t user_lock = PTHREAD_MUTEX_INITIALIZER;
void
ctor (void)
{
xpthread_barrier_wait (&bar_ctor);
dprintf (1, "thread 1: in ctor: started.\n");
xpthread_mutex_lock (&user_lock);
dprintf (1, "thread 1: in ctor: locked user_lock.\n");
xpthread_mutex_unlock (&user_lock);
dprintf (1, "thread 1: in ctor: unlocked user_lock.\n");
dprintf (1, "thread 1: in ctor: done.\n");
xpthread_barrier_wait (&bar_ctor_finish);
}
void
dtor (void)
{
xpthread_barrier_wait (&bar_dtor);
dprintf (1, "thread 1: in dtor: started.\n");
xpthread_mutex_lock (&user_lock);
dprintf (1, "thread 1: in dtor: locked user_lock.\n");
xpthread_mutex_unlock (&user_lock);
dprintf (1, "thread 1: in dtor: unlocked user_lock.\n");
dprintf (1, "thread 1: in dtor: done.\n");
}
static void *
thread3 (void *a)
{
dprintf (1, "thread 3: started.\n");
dprintf (1, "thread 3: done.\n");
return 0;
}
static void *
thread2 (void *a)
{
pthread_t t3;
dprintf (1, "thread 2: started.\n");
xpthread_mutex_lock (&user_lock);
dprintf (1, "thread 2: locked user_lock.\n");
xpthread_barrier_wait (&bar_ctor);
t3 = xpthread_create (0, thread3, 0);
xpthread_mutex_unlock (&user_lock);
dprintf (1, "thread 2: unlocked user_lock.\n");
xpthread_join (t3);
xpthread_barrier_wait (&bar_ctor_finish);
xpthread_mutex_lock (&user_lock);
dprintf (1, "thread 2: locked user_lock.\n");
xpthread_barrier_wait (&bar_dtor);
t3 = xpthread_create (0, thread3, 0);
xpthread_mutex_unlock (&user_lock);
dprintf (1, "thread 2: unlocked user_lock.\n");
xpthread_join (t3);
dprintf (1, "thread 2: done.\n");
return 0;
}
static void
thread1 (void)
{
dprintf (1, "thread 1: started.\n");
xpthread_barrier_init (&bar_ctor, NULL, 2);
xpthread_barrier_init (&bar_ctor_finish, NULL, 2);
xpthread_barrier_init (&bar_dtor, NULL, 2);
pthread_t t2 = xpthread_create (0, thread2, 0);
void *p = xdlopen ("tst-create1mod.so", RTLD_NOW | RTLD_GLOBAL);
dprintf (1, "thread 1: dlopen done.\n");
xdlclose (p);
dprintf (1, "thread 1: dlclose done.\n");
xpthread_join (t2);
dprintf (1, "thread 1: done.\n");
}
static int
do_test (void)
{
thread1 ();
return 0;
}
#include <support/test-driver.c>
|