Blame nptl/pthread_cond_destroy.c

Packit Service 82fcde
/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <shlib-compat.h>
Packit Service 82fcde
#include "pthreadP.h"
Packit Service 82fcde
#include <stap-probe.h>
Packit Service 82fcde
#include <atomic.h>
Packit Service 82fcde
#include <futex-internal.h>
Packit Service 82fcde
Packit Service 82fcde
#include "pthread_cond_common.c"
Packit Service 82fcde
Packit Service 82fcde
/* See __pthread_cond_wait for a high-level description of the algorithm.
Packit Service 82fcde
Packit Service 82fcde
   A correct program must make sure that no waiters are blocked on the condvar
Packit Service 82fcde
   when it is destroyed, and that there are no concurrent signals or
Packit Service 82fcde
   broadcasts.  To wake waiters reliably, the program must signal or
Packit Service 82fcde
   broadcast while holding the mutex or after having held the mutex.  It must
Packit Service 82fcde
   also ensure that no signal or broadcast are still pending to unblock
Packit Service 82fcde
   waiters; IOW, because waiters can wake up spuriously, the program must
Packit Service 82fcde
   effectively ensure that destruction happens after the execution of those
Packit Service 82fcde
   signal or broadcast calls.
Packit Service 82fcde
   Thus, we can assume that all waiters that are still accessing the condvar
Packit Service 82fcde
   have been woken.  We wait until they have confirmed to have woken up by
Packit Service 82fcde
   decrementing __wrefs.  */
Packit Service 82fcde
int
Packit Service 82fcde
__pthread_cond_destroy (pthread_cond_t *cond)
Packit Service 82fcde
{
Packit Service 82fcde
  LIBC_PROBE (cond_destroy, 1, cond);
Packit Service 82fcde
Packit Service 82fcde
  /* Set the wake request flag.  We could also spin, but destruction that is
Packit Service 82fcde
     concurrent with still-active waiters is probably neither common nor
Packit Service 82fcde
     performance critical.  Acquire MO to synchronize with waiters confirming
Packit Service 82fcde
     that they finished.  */
Packit Service 82fcde
  unsigned int wrefs = atomic_fetch_or_acquire (&cond->__data.__wrefs, 4);
Packit Service 82fcde
  int private = __condvar_get_private (wrefs);
Packit Service 82fcde
  while (wrefs >> 3 != 0)
Packit Service 82fcde
    {
Packit Service 82fcde
      futex_wait_simple (&cond->__data.__wrefs, wrefs, private);
Packit Service 82fcde
      /* See above.  */
Packit Service 82fcde
      wrefs = atomic_load_acquire (&cond->__data.__wrefs);
Packit Service 82fcde
    }
Packit Service 82fcde
  /* The memory the condvar occupies can now be reused.  */
Packit Service 82fcde
  return 0;
Packit Service 82fcde
}
Packit Service 82fcde
versioned_symbol (libpthread, __pthread_cond_destroy,
Packit Service 82fcde
		  pthread_cond_destroy, GLIBC_2_3_2);