Blame nptl/pthread_cond_destroy.c

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