Blame sysdeps/pthread/lio_listio.c

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