hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame sysdeps/pthread/aio_notify.c

Packit 6c4009
/* Notify initiator of AIO request.
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
#include <errno.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <aio_misc.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
Packit 6c4009
#ifndef aio_start_notify_thread
Packit 6c4009
# define aio_start_notify_thread() do { } while (0)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
struct notify_func
Packit 6c4009
  {
Packit 6c4009
    void (*func) (sigval_t);
Packit 6c4009
    sigval_t value;
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
notify_func_wrapper (void *arg)
Packit 6c4009
{
Packit 6c4009
  aio_start_notify_thread ();
Packit 6c4009
  struct notify_func *const n = arg;
Packit 6c4009
  void (*func) (sigval_t) = n->func;
Packit 6c4009
  sigval_t value = n->value;
Packit 6c4009
  free (n);
Packit 6c4009
  (*func) (value);
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__aio_notify_only (struct sigevent *sigev)
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  /* Send the signal to notify about finished processing of the request.  */
Packit 6c4009
  if (__glibc_unlikely (sigev->sigev_notify == SIGEV_THREAD))
Packit 6c4009
    {
Packit 6c4009
      /* We have to start a thread.  */
Packit 6c4009
      pthread_t tid;
Packit 6c4009
      pthread_attr_t attr, *pattr;
Packit 6c4009
Packit 6c4009
      pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
Packit 6c4009
      if (pattr == NULL)
Packit 6c4009
	{
Packit 6c4009
	  pthread_attr_init (&attr);
Packit 6c4009
	  pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
Packit 6c4009
	  pattr = &att;;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* SIGEV may be freed as soon as we return, so we cannot let the
Packit 6c4009
	 notification thread use that pointer.  Even though a sigval_t is
Packit 6c4009
	 only one word and the same size as a void *, we cannot just pass
Packit 6c4009
	 the value through pthread_create as the argument and have the new
Packit 6c4009
	 thread run the user's function directly, because on some machines
Packit 6c4009
	 the calling convention for a union like sigval_t is different from
Packit 6c4009
	 that for a pointer type like void *.  */
Packit 6c4009
      struct notify_func *nf = malloc (sizeof *nf);
Packit 6c4009
      if (nf == NULL)
Packit 6c4009
	result = -1;
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  nf->func = sigev->sigev_notify_function;
Packit 6c4009
	  nf->value = sigev->sigev_value;
Packit 6c4009
	  if (pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0)
Packit 6c4009
	    {
Packit 6c4009
	      free (nf);
Packit 6c4009
	      result = -1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else if (sigev->sigev_notify == SIGEV_SIGNAL)
Packit 6c4009
    {
Packit 6c4009
      /* We have to send a signal.  */
Packit 6c4009
#if _POSIX_REALTIME_SIGNALS > 0
Packit 6c4009
      /* Note that the standard gives us the option of using a plain
Packit 6c4009
	 non-queuing signal here when SA_SIGINFO is not set for the signal.  */
Packit 6c4009
      if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value, getpid ())
Packit 6c4009
	  < 0)
Packit 6c4009
	result = -1;
Packit 6c4009
#else
Packit 6c4009
      /* There are no queued signals on this system at all.  */
Packit 6c4009
      result = raise (sigev->sigev_signo);
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__aio_notify (struct requestlist *req)
Packit 6c4009
{
Packit 6c4009
  struct waitlist *waitlist;
Packit 6c4009
  struct aiocb *aiocbp = &req->aiocbp->aiocb;
Packit 6c4009
Packit 6c4009
  if (__aio_notify_only (&aiocbp->aio_sigevent) != 0)
Packit 6c4009
    {
Packit 6c4009
      /* XXX What shall we do if already an error is set by
Packit 6c4009
	 read/write/fsync?  */
Packit 6c4009
      aiocbp->__error_code = errno;
Packit 6c4009
      aiocbp->__return_value = -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now also notify possibly waiting threads.  */
Packit 6c4009
  waitlist = req->waiting;
Packit 6c4009
  while (waitlist != NULL)
Packit 6c4009
    {
Packit 6c4009
      struct waitlist *next = waitlist->next;
Packit 6c4009
Packit 6c4009
      if (waitlist->sigevp == NULL)
Packit 6c4009
	{
Packit 6c4009
	  if (waitlist->result != NULL && aiocbp->__return_value == -1)
Packit 6c4009
	    *waitlist->result = -1;
Packit 6c4009
Packit 6c4009
#ifdef DONT_NEED_AIO_MISC_COND
Packit 6c4009
	  AIO_MISC_NOTIFY (waitlist);
Packit 6c4009
#else
Packit 6c4009
	  /* Decrement the counter.  */
Packit 6c4009
	  --*waitlist->counterp;
Packit 6c4009
Packit 6c4009
	  pthread_cond_signal (waitlist->cond);
Packit 6c4009
#endif
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	/* This is part of an asynchronous `lio_listio' operation.  If
Packit 6c4009
	   this request is the last one, send the signal.  */
Packit 6c4009
	if (--*waitlist->counterp == 0)
Packit 6c4009
	  {
Packit 6c4009
	    __aio_notify_only (waitlist->sigevp);
Packit 6c4009
	    /* This is tricky.  See lio_listio.c for the reason why
Packit 6c4009
	       this works.  */
Packit 6c4009
	    free ((void *) waitlist->counterp);
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
      waitlist = next;
Packit 6c4009
    }
Packit 6c4009
}