hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame sysdeps/nptl/gai_misc.h

Packit 6c4009
/* Copyright (C) 2006-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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
/* We define a special synchronization primitive for AIO.  POSIX
Packit 6c4009
   conditional variables would be ideal but the pthread_cond_*wait
Packit 6c4009
   operations do not return on EINTR.  This is a requirement for
Packit 6c4009
   correct aio_suspend and lio_listio implementations.  */
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <nptl/pthreadP.h>
Packit 6c4009
#include <futex-internal.h>
Packit 6c4009
Packit 6c4009
#define DONT_NEED_GAI_MISC_COND	1
Packit 6c4009
Packit 6c4009
#define GAI_MISC_NOTIFY(waitlist) \
Packit 6c4009
  do {									      \
Packit 6c4009
    if (*waitlist->counterp > 0 && --*waitlist->counterp == 0)		      \
Packit 6c4009
      futex_wake ((unsigned int *) waitlist->counterp, 1, FUTEX_PRIVATE);     \
Packit 6c4009
  } while (0)
Packit 6c4009
Packit 6c4009
#define GAI_MISC_WAIT(result, futex, timeout, cancel) \
Packit 6c4009
  do {									      \
Packit 6c4009
    volatile unsigned int *futexaddr = &futex;				      \
Packit 6c4009
    unsigned int oldval = futex;					      \
Packit 6c4009
									      \
Packit 6c4009
    if (oldval != 0)							      \
Packit 6c4009
      {									      \
Packit 6c4009
	pthread_mutex_unlock (&__gai_requests_mutex);			      \
Packit 6c4009
									      \
Packit 6c4009
	int oldtype;							      \
Packit 6c4009
	if (cancel)							      \
Packit 6c4009
	  oldtype = LIBC_CANCEL_ASYNC ();				      \
Packit 6c4009
									      \
Packit 6c4009
	int status;							      \
Packit 6c4009
	do								      \
Packit 6c4009
	  {								      \
Packit 6c4009
	    status = futex_reltimed_wait ((unsigned int *) futexaddr, oldval, \
Packit 6c4009
					  timeout, FUTEX_PRIVATE);	      \
Packit 6c4009
	    if (status != EAGAIN)					      \
Packit 6c4009
	      break;							      \
Packit 6c4009
									      \
Packit 6c4009
	    oldval = *futexaddr;					      \
Packit 6c4009
	  }								      \
Packit 6c4009
	while (oldval != 0);						      \
Packit 6c4009
									      \
Packit 6c4009
	if (cancel)							      \
Packit 6c4009
	  LIBC_CANCEL_RESET (oldtype);					      \
Packit 6c4009
									      \
Packit 6c4009
	if (status == EINTR)						      \
Packit 6c4009
	  result = EINTR;						      \
Packit 6c4009
	else if (status == ETIMEDOUT)					      \
Packit 6c4009
	  result = EAGAIN;						      \
Packit 6c4009
	else								      \
Packit 6c4009
	  assert (status == 0 || status == EAGAIN);			      \
Packit 6c4009
									      \
Packit 6c4009
	pthread_mutex_lock (&__gai_requests_mutex);			      \
Packit 6c4009
      }									      \
Packit 6c4009
  } while (0)
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define gai_start_notify_thread __gai_start_notify_thread
Packit 6c4009
#define gai_create_helper_thread __gai_create_helper_thread
Packit 6c4009
Packit 6c4009
extern inline void
Packit 6c4009
__gai_start_notify_thread (void)
Packit 6c4009
{
Packit 6c4009
  sigset_t ss;
Packit 6c4009
  sigemptyset (&ss);
Packit 6c4009
  int sigerr __attribute__ ((unused));
Packit 6c4009
  sigerr = pthread_sigmask (SIG_SETMASK, &ss, NULL);
Packit 6c4009
  assert_perror (sigerr);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
extern inline int
Packit 6c4009
__gai_create_helper_thread (pthread_t *threadp, void *(*tf) (void *),
Packit 6c4009
			    void *arg)
Packit 6c4009
{
Packit 6c4009
  pthread_attr_t attr;
Packit 6c4009
Packit 6c4009
  /* Make sure the thread is created detached.  */
Packit 6c4009
  pthread_attr_init (&attr);
Packit 6c4009
  pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
Packit 6c4009
Packit 6c4009
  /* The helper thread needs only very little resources.  */
Packit 6c4009
  (void) pthread_attr_setstacksize (&attr,
Packit 6c4009
				    __pthread_get_minstack (&attr)
Packit 6c4009
				    + 4 * PTHREAD_STACK_MIN);
Packit 6c4009
Packit 6c4009
  /* Block all signals in the helper thread.  To do this thoroughly we
Packit 6c4009
     temporarily have to block all signals here.  */
Packit 6c4009
  sigset_t ss;
Packit 6c4009
  sigset_t oss;
Packit 6c4009
  sigfillset (&ss);
Packit 6c4009
  int sigerr __attribute__ ((unused));
Packit 6c4009
  sigerr = pthread_sigmask (SIG_SETMASK, &ss, &oss;;
Packit 6c4009
  assert_perror (sigerr);
Packit 6c4009
Packit 6c4009
  int ret = pthread_create (threadp, &attr, tf, arg);
Packit 6c4009
Packit 6c4009
  /* Restore the signal mask.  */
Packit 6c4009
  sigerr = pthread_sigmask (SIG_SETMASK, &oss, NULL);
Packit 6c4009
  assert_perror (sigerr);
Packit 6c4009
Packit 6c4009
  (void) pthread_attr_destroy (&attr);
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include_next <gai_misc.h>