hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame sysdeps/unix/sysv/linux/timer_create.c

Packit 6c4009
/* Copyright (C) 2003-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 License as
Packit 6c4009
   published by the Free Software Foundation; either version 2.1 of the
Packit 6c4009
   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; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <internaltypes.h>
Packit 6c4009
#include <nptl/pthreadP.h>
Packit 6c4009
#include "kernel-posix-timers.h"
Packit 6c4009
#include "kernel-posix-cpu-timers.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifdef timer_create_alias
Packit 6c4009
# define timer_create timer_create_alias
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
Packit 6c4009
{
Packit 6c4009
#undef timer_create
Packit 6c4009
  {
Packit 6c4009
    clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
Packit 6c4009
				 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
Packit 6c4009
				 : clock_id == CLOCK_THREAD_CPUTIME_ID
Packit 6c4009
				 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
Packit 6c4009
				 : clock_id);
Packit 6c4009
Packit 6c4009
    /* If the user wants notification via a thread we need to handle
Packit 6c4009
       this special.  */
Packit 6c4009
    if (evp == NULL
Packit 6c4009
	|| __builtin_expect (evp->sigev_notify != SIGEV_THREAD, 1))
Packit 6c4009
      {
Packit 6c4009
	struct sigevent local_evp;
Packit 6c4009
Packit 6c4009
	/* We avoid allocating too much memory by basically
Packit 6c4009
	   using struct timer as a derived class with the
Packit 6c4009
	   first two elements being in the superclass.  We only
Packit 6c4009
	   need these two elements here.  */
Packit 6c4009
	struct timer *newp = (struct timer *) malloc (offsetof (struct timer,
Packit 6c4009
								thrfunc));
Packit 6c4009
	if (newp == NULL)
Packit 6c4009
	  /* No more memory.  */
Packit 6c4009
	  return -1;
Packit 6c4009
Packit 6c4009
	if (evp == NULL)
Packit 6c4009
	  {
Packit 6c4009
	    /* The kernel has to pass up the timer ID which is a
Packit 6c4009
	       userlevel object.  Therefore we cannot leave it up to
Packit 6c4009
	       the kernel to determine it.  */
Packit 6c4009
	    local_evp.sigev_notify = SIGEV_SIGNAL;
Packit 6c4009
	    local_evp.sigev_signo = SIGALRM;
Packit 6c4009
	    local_evp.sigev_value.sival_ptr = newp;
Packit 6c4009
Packit 6c4009
	    evp = &local_evp;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	kernel_timer_t ktimerid;
Packit 6c4009
	int retval = INLINE_SYSCALL (timer_create, 3, syscall_clockid, evp,
Packit 6c4009
				     &ktimerid);
Packit 6c4009
Packit 6c4009
	if (retval != -1)
Packit 6c4009
	  {
Packit 6c4009
	    newp->sigev_notify = (evp != NULL
Packit 6c4009
				  ? evp->sigev_notify : SIGEV_SIGNAL);
Packit 6c4009
	    newp->ktimerid = ktimerid;
Packit 6c4009
Packit 6c4009
	    *timerid = (timer_t) newp;
Packit 6c4009
	  }
Packit 6c4009
	else
Packit 6c4009
	  {
Packit 6c4009
	    /* Cannot allocate the timer, fail.  */
Packit 6c4009
	    free (newp);
Packit 6c4009
	    retval = -1;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	return retval;
Packit 6c4009
      }
Packit 6c4009
    else
Packit 6c4009
      {
Packit 6c4009
	/* Create the helper thread.  */
Packit 6c4009
	pthread_once (&__helper_once, __start_helper_thread);
Packit 6c4009
	if (__helper_tid == 0)
Packit 6c4009
	  {
Packit 6c4009
	    /* No resources to start the helper thread.  */
Packit 6c4009
	    __set_errno (EAGAIN);
Packit 6c4009
	    return -1;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	struct timer *newp;
Packit 6c4009
	newp = (struct timer *) malloc (sizeof (struct timer));
Packit 6c4009
	if (newp == NULL)
Packit 6c4009
	  return -1;
Packit 6c4009
Packit 6c4009
	/* Copy the thread parameters the user provided.  */
Packit 6c4009
	newp->sival = evp->sigev_value;
Packit 6c4009
	newp->thrfunc = evp->sigev_notify_function;
Packit 6c4009
	newp->sigev_notify = SIGEV_THREAD;
Packit 6c4009
Packit 6c4009
	/* We cannot simply copy the thread attributes since the
Packit 6c4009
	   implementation might keep internal information for
Packit 6c4009
	   each instance.  */
Packit 6c4009
	(void) pthread_attr_init (&newp->attr);
Packit 6c4009
	if (evp->sigev_notify_attributes != NULL)
Packit 6c4009
	  {
Packit 6c4009
	    struct pthread_attr *nattr;
Packit 6c4009
	    struct pthread_attr *oattr;
Packit 6c4009
Packit 6c4009
	    nattr = (struct pthread_attr *) &newp->attr;
Packit 6c4009
	    oattr = (struct pthread_attr *) evp->sigev_notify_attributes;
Packit 6c4009
Packit 6c4009
	    nattr->schedparam = oattr->schedparam;
Packit 6c4009
	    nattr->schedpolicy = oattr->schedpolicy;
Packit 6c4009
	    nattr->flags = oattr->flags;
Packit 6c4009
	    nattr->guardsize = oattr->guardsize;
Packit 6c4009
	    nattr->stackaddr = oattr->stackaddr;
Packit 6c4009
	    nattr->stacksize = oattr->stacksize;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	/* In any case set the detach flag.  */
Packit 6c4009
	(void) pthread_attr_setdetachstate (&newp->attr,
Packit 6c4009
					    PTHREAD_CREATE_DETACHED);
Packit 6c4009
Packit 6c4009
	/* Create the event structure for the kernel timer.  */
Packit 6c4009
	struct sigevent sev =
Packit 6c4009
	  { .sigev_value.sival_ptr = newp,
Packit 6c4009
	    .sigev_signo = SIGTIMER,
Packit 6c4009
	    .sigev_notify = SIGEV_SIGNAL | SIGEV_THREAD_ID,
Packit 6c4009
	    ._sigev_un = { ._pad = { [0] = __helper_tid } } };
Packit 6c4009
Packit 6c4009
	/* Create the timer.  */
Packit 6c4009
	INTERNAL_SYSCALL_DECL (err);
Packit 6c4009
	int res;
Packit 6c4009
	res = INTERNAL_SYSCALL (timer_create, err, 3,
Packit 6c4009
				syscall_clockid, &sev, &newp->ktimerid);
Packit 6c4009
	if (! INTERNAL_SYSCALL_ERROR_P (res, err))
Packit 6c4009
	  {
Packit 6c4009
	    /* Add to the queue of active timers with thread
Packit 6c4009
	       delivery.  */
Packit 6c4009
	    pthread_mutex_lock (&__active_timer_sigev_thread_lock);
Packit 6c4009
	    newp->next = __active_timer_sigev_thread;
Packit 6c4009
	    __active_timer_sigev_thread = newp;
Packit 6c4009
	    pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
Packit 6c4009
Packit 6c4009
	    *timerid = (timer_t) newp;
Packit 6c4009
	    return 0;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	/* Free the resources.  */
Packit 6c4009
	free (newp);
Packit 6c4009
Packit 6c4009
	__set_errno (INTERNAL_SYSCALL_ERRNO (res, err));
Packit 6c4009
Packit 6c4009
	return -1;
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
}