Blame sysdeps/pthread/lio_listio.c

Packit Service 82fcde
/* Enqueue and list of read or write requests.
Packit Service 82fcde
   Copyright (C) 1997-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@cygnus.com>, 1997.
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
#ifndef lio_listio
Packit Service 82fcde
#include <aio.h>
Packit Service 82fcde
#include <assert.h>
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
Packit Service 82fcde
#include <aio_misc.h>
Packit Service 82fcde
Packit Service 82fcde
#define LIO_OPCODE_BASE 0
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
#include <shlib-compat.h>
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
/* We need this special structure to handle asynchronous I/O.  */
Packit Service 82fcde
struct async_waitlist
Packit Service 82fcde
  {
Packit Service 82fcde
    unsigned int counter;
Packit Service 82fcde
    struct sigevent sigev;
Packit Service 82fcde
    struct waitlist list[0];
Packit Service 82fcde
  };
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
/* The code in glibc 2.1 to glibc 2.4 issued only one event when all
Packit Service 82fcde
   requests submitted with lio_listio finished.  The existing practice
Packit Service 82fcde
   is to issue events for the individual requests as well.  This is
Packit Service 82fcde
   what the new code does.  */
Packit Service 82fcde
#if SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_4)
Packit Service 82fcde
# define LIO_MODE(mode) ((mode) & 127)
Packit Service 82fcde
# define NO_INDIVIDUAL_EVENT_P(mode) ((mode) & 128)
Packit Service 82fcde
#else
Packit Service 82fcde
# define LIO_MODE(mode) mode
Packit Service 82fcde
# define NO_INDIVIDUAL_EVENT_P(mode) 0
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
static int
Packit Service 82fcde
lio_listio_internal (int mode, struct aiocb *const list[], int nent,
Packit Service 82fcde
		     struct sigevent *sig)
Packit Service 82fcde
{
Packit Service 82fcde
  struct sigevent defsigev;
Packit Service 82fcde
  struct requestlist *requests[nent];
Packit Service 82fcde
  int cnt;
Packit Service 82fcde
  volatile unsigned int total = 0;
Packit Service 82fcde
  int result = 0;
Packit Service 82fcde
Packit Service 82fcde
  if (sig == NULL)
Packit Service 82fcde
    {
Packit Service 82fcde
      defsigev.sigev_notify = SIGEV_NONE;
Packit Service 82fcde
      sig = &defsigev;
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  /* Request the mutex.  */
Packit Service 82fcde
  pthread_mutex_lock (&__aio_requests_mutex);
Packit Service 82fcde
Packit Service 82fcde
  /* Now we can enqueue all requests.  Since we already acquired the
Packit Service 82fcde
     mutex the enqueue function need not do this.  */
Packit Service 82fcde
  for (cnt = 0; cnt < nent; ++cnt)
Packit Service 82fcde
    if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
Packit Service 82fcde
      {
Packit Service 82fcde
	if (NO_INDIVIDUAL_EVENT_P (mode))
Packit Service 82fcde
	  list[cnt]->aio_sigevent.sigev_notify = SIGEV_NONE;
Packit Service 82fcde
Packit Service 82fcde
	requests[cnt] = __aio_enqueue_request ((aiocb_union *) list[cnt],
Packit Service 82fcde
					       (list[cnt]->aio_lio_opcode
Packit Service 82fcde
						| LIO_OPCODE_BASE));
Packit Service 82fcde
Packit Service 82fcde
	if (requests[cnt] != NULL)
Packit Service 82fcde
	  /* Successfully enqueued.  */
Packit Service 82fcde
	  ++total;
Packit Service 82fcde
	else
Packit Service 82fcde
	  /* Signal that we've seen an error.  `errno' and the error code
Packit Service 82fcde
	     of the aiocb will tell more.  */
Packit Service 82fcde
	  result = -1;
Packit Service 82fcde
      }
Packit Service 82fcde
    else
Packit Service 82fcde
      requests[cnt] = NULL;
Packit Service 82fcde
Packit Service 82fcde
  if (total == 0)
Packit Service 82fcde
    {
Packit Service 82fcde
      /* We don't have anything to do except signalling if we work
Packit Service 82fcde
	 asynchronously.  */
Packit Service 82fcde
Packit Service 82fcde
      /* Release the mutex.  We do this before raising a signal since the
Packit Service 82fcde
	 signal handler might do a `siglongjmp' and then the mutex is
Packit Service 82fcde
	 locked forever.  */
Packit Service 82fcde
      pthread_mutex_unlock (&__aio_requests_mutex);
Packit Service 82fcde
Packit Service 82fcde
      if (LIO_MODE (mode) == LIO_NOWAIT)
Packit Service 82fcde
	__aio_notify_only (sig);
Packit Service 82fcde
Packit Service 82fcde
      return result;
Packit Service 82fcde
    }
Packit Service 82fcde
  else if (LIO_MODE (mode) == LIO_WAIT)
Packit Service 82fcde
    {
Packit Service 82fcde
#ifndef DONT_NEED_AIO_MISC_COND
Packit Service 82fcde
      pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Packit Service 82fcde
      int oldstate;
Packit Service 82fcde
#endif
Packit Service 82fcde
      struct waitlist waitlist[nent];
Packit Service 82fcde
Packit Service 82fcde
      total = 0;
Packit Service 82fcde
      for (cnt = 0; cnt < nent; ++cnt)
Packit Service 82fcde
	{
Packit Service 82fcde
	  assert (requests[cnt] == NULL || list[cnt] != NULL);
Packit Service 82fcde
Packit Service 82fcde
	  if (requests[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
Packit Service 82fcde
	    {
Packit Service 82fcde
#ifndef DONT_NEED_AIO_MISC_COND
Packit Service 82fcde
	      waitlist[cnt].cond = &cond;
Packit Service 82fcde
#endif
Packit Service 82fcde
	      waitlist[cnt].result = &result;
Packit Service 82fcde
	      waitlist[cnt].next = requests[cnt]->waiting;
Packit Service 82fcde
	      waitlist[cnt].counterp = &total;
Packit Service 82fcde
	      waitlist[cnt].sigevp = NULL;
Packit Service 82fcde
	      requests[cnt]->waiting = &waitlist[cnt];
Packit Service 82fcde
	      ++total;
Packit Service 82fcde
	    }
Packit Service 82fcde
	}
Packit Service 82fcde
Packit Service 82fcde
#ifdef DONT_NEED_AIO_MISC_COND
Packit Service 82fcde
      AIO_MISC_WAIT (result, total, NULL, 0);
Packit Service 82fcde
#else
Packit Service 82fcde
      /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancellation
Packit Service 82fcde
	 points we must be careful.  We added entries to the waiting lists
Packit Service 82fcde
	 which we must remove.  So defer cancellation for now.  */
Packit Service 82fcde
      pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
Packit Service 82fcde
Packit Service 82fcde
      while (total > 0)
Packit Service 82fcde
	pthread_cond_wait (&cond, &__aio_requests_mutex);
Packit Service 82fcde
Packit Service 82fcde
      /* Now it's time to restore the cancellation state.  */
Packit Service 82fcde
      pthread_setcancelstate (oldstate, NULL);
Packit Service 82fcde
Packit Service 82fcde
      /* Release the conditional variable.  */
Packit Service 82fcde
      if (pthread_cond_destroy (&cond) != 0)
Packit Service 82fcde
	/* This must never happen.  */
Packit Service 82fcde
	abort ();
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
      /* If any of the I/O requests failed, return -1 and set errno.  */
Packit Service 82fcde
      if (result != 0)
Packit Service 82fcde
	{
Packit Service 82fcde
	  __set_errno (result == EINTR ? EINTR : EIO);
Packit Service 82fcde
	  result = -1;
Packit Service 82fcde
	}
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    {
Packit Service 82fcde
      struct async_waitlist *waitlist;
Packit Service 82fcde
Packit Service 82fcde
      waitlist = (struct async_waitlist *)
Packit Service 82fcde
	malloc (sizeof (struct async_waitlist)
Packit Service 82fcde
		+ (nent * sizeof (struct waitlist)));
Packit Service 82fcde
Packit Service 82fcde
      if (waitlist == NULL)
Packit Service 82fcde
	{
Packit Service 82fcde
	  __set_errno (EAGAIN);
Packit Service 82fcde
	  result = -1;
Packit Service 82fcde
	}
Packit Service 82fcde
      else
Packit Service 82fcde
	{
Packit Service 82fcde
	  total = 0;
Packit Service 82fcde
Packit Service 82fcde
	  for (cnt = 0; cnt < nent; ++cnt)
Packit Service 82fcde
	    {
Packit Service 82fcde
	      assert (requests[cnt] == NULL || list[cnt] != NULL);
Packit Service 82fcde
Packit Service 82fcde
	      if (requests[cnt] != NULL
Packit Service 82fcde
		  && list[cnt]->aio_lio_opcode != LIO_NOP)
Packit Service 82fcde
		{
Packit Service 82fcde
#ifndef DONT_NEED_AIO_MISC_COND
Packit Service 82fcde
		  waitlist->list[cnt].cond = NULL;
Packit Service 82fcde
#endif
Packit Service 82fcde
		  waitlist->list[cnt].result = NULL;
Packit Service 82fcde
		  waitlist->list[cnt].next = requests[cnt]->waiting;
Packit Service 82fcde
		  waitlist->list[cnt].counterp = &waitlist->counter;
Packit Service 82fcde
		  waitlist->list[cnt].sigevp = &waitlist->sigev;
Packit Service 82fcde
		  requests[cnt]->waiting = &waitlist->list[cnt];
Packit Service 82fcde
		  ++total;
Packit Service 82fcde
		}
Packit Service 82fcde
	    }
Packit Service 82fcde
Packit Service 82fcde
	  waitlist->counter = total;
Packit Service 82fcde
	  waitlist->sigev = *sig;
Packit Service 82fcde
	}
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  /* Release the mutex.  */
Packit Service 82fcde
  pthread_mutex_unlock (&__aio_requests_mutex);
Packit Service 82fcde
Packit Service 82fcde
  return result;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
#if SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_4)
Packit Service 82fcde
int
Packit Service 82fcde
attribute_compat_text_section
Packit Service 82fcde
__lio_listio_21 (int mode, struct aiocb *const list[], int nent,
Packit Service 82fcde
		 struct sigevent *sig)
Packit Service 82fcde
{
Packit Service 82fcde
  /* Check arguments.  */
Packit Service 82fcde
  if (mode != LIO_WAIT && mode != LIO_NOWAIT)
Packit Service 82fcde
    {
Packit Service 82fcde
      __set_errno (EINVAL);
Packit Service 82fcde
      return -1;
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  return lio_listio_internal (mode | LIO_NO_INDIVIDUAL_EVENT, list, nent, sig);
Packit Service 82fcde
}
Packit Service 82fcde
compat_symbol (librt, __lio_listio_21, lio_listio, GLIBC_2_1);
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
int
Packit Service 82fcde
__lio_listio_item_notify (int mode, struct aiocb *const list[], int nent,
Packit Service 82fcde
			  struct sigevent *sig)
Packit Service 82fcde
{
Packit Service 82fcde
    /* Check arguments.  */
Packit Service 82fcde
  if (mode != LIO_WAIT && mode != LIO_NOWAIT)
Packit Service 82fcde
    {
Packit Service 82fcde
      __set_errno (EINVAL);
Packit Service 82fcde
      return -1;
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  return lio_listio_internal (mode, list, nent, sig);
Packit Service 82fcde
}
Packit Service 82fcde
versioned_symbol (librt, __lio_listio_item_notify, lio_listio, GLIBC_2_4);