Blame sysdeps/pthread/aio_suspend.c

Packit 6c4009
/* Suspend until termination of a requests.
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
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
Packit 6c4009
/* We use an UGLY hack to prevent gcc from finding us cheating.  The
Packit 6c4009
   implementations of aio_suspend and aio_suspend64 are identical and so
Packit 6c4009
   we want to avoid code duplication by using aliases.  But gcc sees
Packit 6c4009
   the different parameter lists and prints a warning.  We define here
Packit 6c4009
   a function so that aio_suspend64 has no prototype.  */
Packit 6c4009
#define aio_suspend64 XXX
Packit 6c4009
#include <aio.h>
Packit 6c4009
/* And undo the hack.  */
Packit 6c4009
#undef aio_suspend64
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
#include <aio_misc.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
struct clparam
Packit 6c4009
{
Packit 6c4009
  const struct aiocb *const *list;
Packit 6c4009
  struct waitlist *waitlist;
Packit 6c4009
  struct requestlist **requestlist;
Packit 6c4009
#ifndef DONT_NEED_AIO_MISC_COND
Packit 6c4009
  pthread_cond_t *cond;
Packit 6c4009
#endif
Packit 6c4009
  int nent;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
cleanup (void *arg)
Packit 6c4009
{
Packit 6c4009
#ifdef DONT_NEED_AIO_MISC_COND
Packit 6c4009
  /* Acquire the mutex.  If pthread_cond_*wait is used this would
Packit 6c4009
     happen implicitly.  */
Packit 6c4009
  pthread_mutex_lock (&__aio_requests_mutex);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  const struct clparam *param = (const struct clparam *) arg;
Packit 6c4009
Packit 6c4009
  /* Now remove the entry in the waiting list for all requests
Packit 6c4009
     which didn't terminate.  */
Packit 6c4009
  int cnt = param->nent;
Packit 6c4009
  while (cnt-- > 0)
Packit 6c4009
    if (param->list[cnt] != NULL
Packit 6c4009
	&& param->list[cnt]->__error_code == EINPROGRESS)
Packit 6c4009
      {
Packit 6c4009
	struct waitlist **listp;
Packit 6c4009
Packit 6c4009
	assert (param->requestlist[cnt] != NULL);
Packit 6c4009
Packit 6c4009
	/* There is the chance that we cannot find our entry anymore. This
Packit 6c4009
	   could happen if the request terminated and restarted again.  */
Packit 6c4009
	listp = &param->requestlist[cnt]->waiting;
Packit 6c4009
	while (*listp != NULL && *listp != &param->waitlist[cnt])
Packit 6c4009
	  listp = &(*listp)->next;
Packit 6c4009
Packit 6c4009
	if (*listp != NULL)
Packit 6c4009
	  *listp = (*listp)->next;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
#ifndef DONT_NEED_AIO_MISC_COND
Packit 6c4009
  /* Release the conditional variable.  */
Packit 6c4009
  (void) pthread_cond_destroy (param->cond);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* Release the mutex.  */
Packit 6c4009
  pthread_mutex_unlock (&__aio_requests_mutex);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#ifdef DONT_NEED_AIO_MISC_COND
Packit 6c4009
static int
Packit 6c4009
__attribute__ ((noinline))
Packit 6c4009
do_aio_misc_wait (unsigned int *cntr, const struct timespec *timeout)
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  AIO_MISC_WAIT (result, *cntr, timeout, 1);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
aio_suspend (const struct aiocb *const list[], int nent,
Packit 6c4009
	     const struct timespec *timeout)
Packit 6c4009
{
Packit 6c4009
  if (__glibc_unlikely (nent < 0))
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  struct waitlist waitlist[nent];
Packit 6c4009
  struct requestlist *requestlist[nent];
Packit 6c4009
#ifndef DONT_NEED_AIO_MISC_COND
Packit 6c4009
  pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Packit 6c4009
#endif
Packit 6c4009
  int cnt;
Packit 6c4009
  bool any = false;
Packit 6c4009
  int result = 0;
Packit 6c4009
  unsigned int cntr = 1;
Packit 6c4009
Packit 6c4009
  /* Request the mutex.  */
Packit 6c4009
  pthread_mutex_lock (&__aio_requests_mutex);
Packit 6c4009
Packit 6c4009
  /* There is not yet a finished request.  Signal the request that
Packit 6c4009
     we are working for it.  */
Packit 6c4009
  for (cnt = 0; cnt < nent; ++cnt)
Packit 6c4009
    if (list[cnt] != NULL)
Packit 6c4009
      {
Packit 6c4009
	if (list[cnt]->__error_code == EINPROGRESS)
Packit 6c4009
	  {
Packit 6c4009
	    requestlist[cnt] = __aio_find_req ((aiocb_union *) list[cnt]);
Packit 6c4009
Packit 6c4009
	    if (requestlist[cnt] != NULL)
Packit 6c4009
	      {
Packit 6c4009
#ifndef DONT_NEED_AIO_MISC_COND
Packit 6c4009
		waitlist[cnt].cond = &cond;
Packit 6c4009
#endif
Packit 6c4009
		waitlist[cnt].result = NULL;
Packit 6c4009
		waitlist[cnt].next = requestlist[cnt]->waiting;
Packit 6c4009
		waitlist[cnt].counterp = &cntr;
Packit 6c4009
		waitlist[cnt].sigevp = NULL;
Packit 6c4009
		requestlist[cnt]->waiting = &waitlist[cnt];
Packit 6c4009
		any = true;
Packit 6c4009
	      }
Packit 6c4009
	    else
Packit 6c4009
	      /* We will never suspend.  */
Packit 6c4009
	      break;
Packit 6c4009
	  }
Packit 6c4009
	else
Packit 6c4009
	  /* We will never suspend.  */
Packit 6c4009
	  break;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
Packit 6c4009
  /* Only if none of the entries is NULL or finished to be wait.  */
Packit 6c4009
  if (cnt == nent && any)
Packit 6c4009
    {
Packit 6c4009
      struct clparam clparam =
Packit 6c4009
	{
Packit 6c4009
	  .list = list,
Packit 6c4009
	  .waitlist = waitlist,
Packit 6c4009
	  .requestlist = requestlist,
Packit 6c4009
#ifndef DONT_NEED_AIO_MISC_COND
Packit 6c4009
	  .cond = &cond,
Packit 6c4009
#endif
Packit 6c4009
	  .nent = nent
Packit 6c4009
	};
Packit 6c4009
Packit 6c4009
      pthread_cleanup_push (cleanup, &clparam);
Packit 6c4009
Packit 6c4009
#ifdef DONT_NEED_AIO_MISC_COND
Packit 6c4009
      result = do_aio_misc_wait (&cntr, timeout);
Packit 6c4009
#else
Packit 6c4009
      if (timeout == NULL)
Packit 6c4009
	result = pthread_cond_wait (&cond, &__aio_requests_mutex);
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* We have to convert the relative timeout value into an
Packit 6c4009
	     absolute time value with pthread_cond_timedwait expects.  */
Packit 6c4009
	  struct timeval now;
Packit 6c4009
	  struct timespec abstime;
Packit 6c4009
Packit 6c4009
	  __gettimeofday (&now, NULL);
Packit 6c4009
	  abstime.tv_nsec = timeout->tv_nsec + now.tv_usec * 1000;
Packit 6c4009
	  abstime.tv_sec = timeout->tv_sec + now.tv_sec;
Packit 6c4009
	  if (abstime.tv_nsec >= 1000000000)
Packit 6c4009
	    {
Packit 6c4009
	      abstime.tv_nsec -= 1000000000;
Packit 6c4009
	      abstime.tv_sec += 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  result = pthread_cond_timedwait (&cond, &__aio_requests_mutex,
Packit 6c4009
					   &abstime);
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
      pthread_cleanup_pop (0);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now remove the entry in the waiting list for all requests
Packit 6c4009
     which didn't terminate.  */
Packit 6c4009
  while (cnt-- > 0)
Packit 6c4009
    if (list[cnt] != NULL && list[cnt]->__error_code == EINPROGRESS)
Packit 6c4009
      {
Packit 6c4009
	struct waitlist **listp;
Packit 6c4009
Packit 6c4009
	assert (requestlist[cnt] != NULL);
Packit 6c4009
Packit 6c4009
	/* There is the chance that we cannot find our entry anymore. This
Packit 6c4009
	   could happen if the request terminated and restarted again.  */
Packit 6c4009
	listp = &requestlist[cnt]->waiting;
Packit 6c4009
	while (*listp != NULL && *listp != &waitlist[cnt])
Packit 6c4009
	  listp = &(*listp)->next;
Packit 6c4009
Packit 6c4009
	if (*listp != NULL)
Packit 6c4009
	  *listp = (*listp)->next;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
#ifndef DONT_NEED_AIO_MISC_COND
Packit 6c4009
  /* Release the conditional variable.  */
Packit 6c4009
  if (__glibc_unlikely (pthread_cond_destroy (&cond) != 0))
Packit 6c4009
    /* This must never happen.  */
Packit 6c4009
    abort ();
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (result != 0)
Packit 6c4009
    {
Packit 6c4009
#ifndef DONT_NEED_AIO_MISC_COND
Packit 6c4009
      /* An error occurred.  Possibly it's ETIMEDOUT.  We have to translate
Packit 6c4009
	 the timeout error report of `pthread_cond_timedwait' to the
Packit 6c4009
	 form expected from `aio_suspend'.  */
Packit 6c4009
      if (result == ETIMEDOUT)
Packit 6c4009
	__set_errno (EAGAIN);
Packit 6c4009
      else
Packit 6c4009
#endif
Packit 6c4009
	__set_errno (result);
Packit 6c4009
Packit 6c4009
      result = -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Release the mutex.  */
Packit 6c4009
  pthread_mutex_unlock (&__aio_requests_mutex);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (aio_suspend, aio_suspend64)