https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92836
Tobias Burnus <burnus at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |burnus at gcc dot gnu.org
--- Comment #6 from Tobias Burnus <burnus at gcc dot gnu.org> ---
(In reply to Andrew Benson from comment #1)
> Running with -fsanitize=thread (suggested by Tobias) gives:
> WARNING: ThreadSanitizer: lock-order-inversion (potential deadlock)
> (pid=9982)
> Mutex M84 acquired here while holding mutex M26 in thread T63:
> #2 insert_unit ../../../gcc-trunk/libgfortran/io/unit.c:244
>
> Mutex M26 previously acquired by the same thread here:
> #2 get_gfc_unit ../../../gcc-trunk/libgfortran/io/unit.c:332
I think this part is fine:
1. get_gfc_unit: Get global lock ("unit_lock").
2. Call insert_unit: Get lock in u->lock which is for a particular unit number.
3. get_gfc_unit: unlock global lock
(4. u->lock is unlocked at some later point.)
> Mutex M26 acquired here while holding mutex M84 in thread T63:
> #2 close_unit_1 ../../../gcc-trunk/libgfortran/io/unit.c:735
close_unit_1:
* Obtain global lock ("unit_lock") – that's line 735
* Unlock the unit-number-specific lock u->lock
* Unlock the global lock.
> Mutex M84 previously acquired by the same thread here:
> #2 get_gfc_unit ../../../gcc-trunk/libgfortran/io/unit.c:380
That's again in get_gfc_unit. Here, I am a bit unsure about the code – but I
have not spend a lot of time on the code:
1. Get global lock (unit_lock)
2. Unit has been found
3. Call the following, i.e. if the outer condition is true,
-> if the lock could not been obtained, unlock 'unit_lock' (to avoid a
deadlock) ? But still return 'p'
if (p != NULL && (p->child_dtio == 0))
{
/* Fast path. */
if (! TRYLOCK (&p->lock))
{
/* assert (p->closed == 0); */
UNLOCK (&unit_lock);
return p;
}
The TRYLOCK is the one in line 380.
This part looks a bit fishy. Shouldn't this be "if (TRYLOCK (...))"? Without
"!" – or do I miss something crucial?
https://www.kernel.org/doc/htmldocs/kernel-locking/API-mutex-trylock.html