Blame gnulib/tests/usleep.c

Packit Service a2ae7a
/* Pausing execution of the current thread.
Packit Service a2ae7a
   Copyright (C) 2009-2019 Free Software Foundation, Inc.
Packit Service a2ae7a
   Written by Eric Blake <ebb9@byu.net>, 2009.
Packit Service a2ae7a
Packit Service a2ae7a
   This program is free software: you can redistribute it and/or modify
Packit Service a2ae7a
   it under the terms of the GNU General Public License as published by
Packit Service a2ae7a
   the Free Software Foundation; either version 3 of the License, or
Packit Service a2ae7a
   (at your option) any later version.
Packit Service a2ae7a
Packit Service a2ae7a
   This program is distributed in the hope that it will be useful,
Packit Service a2ae7a
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a2ae7a
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a2ae7a
   GNU General Public License for more details.
Packit Service a2ae7a
Packit Service a2ae7a
   You should have received a copy of the GNU General Public License
Packit Service a2ae7a
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service a2ae7a
Packit Service a2ae7a
/* This file is _intentionally_ light-weight.  Rather than using
Packit Service a2ae7a
   select or nanosleep, both of which drag in external libraries on
Packit Service a2ae7a
   some platforms, this merely rounds up to the nearest second if
Packit Service a2ae7a
   usleep() does not exist.  If sub-second resolution is important,
Packit Service a2ae7a
   then use a more powerful interface to begin with.  */
Packit Service a2ae7a
Packit Service a2ae7a
#include <config.h>
Packit Service a2ae7a
Packit Service a2ae7a
/* Specification.  */
Packit Service a2ae7a
#include <unistd.h>
Packit Service a2ae7a
Packit Service a2ae7a
#include <errno.h>
Packit Service a2ae7a
Packit Service a2ae7a
#ifndef HAVE_USLEEP
Packit Service a2ae7a
# define HAVE_USLEEP 0
Packit Service a2ae7a
#endif
Packit Service a2ae7a
Packit Service a2ae7a
/* Sleep for MICRO microseconds, which can be greater than 1 second.
Packit Service a2ae7a
   Return -1 and set errno to EINVAL on range error (about 4295
Packit Service a2ae7a
   seconds), or 0 on success.  Interaction with SIGALARM is
Packit Service a2ae7a
   unspecified.  */
Packit Service a2ae7a
Packit Service a2ae7a
int
Packit Service a2ae7a
usleep (useconds_t micro)
Packit Service a2ae7a
{
Packit Service a2ae7a
  unsigned int seconds = micro / 1000000;
Packit Service a2ae7a
  if (sizeof seconds < sizeof micro && micro / 1000000 != seconds)
Packit Service a2ae7a
    {
Packit Service a2ae7a
      errno = EINVAL;
Packit Service a2ae7a
      return -1;
Packit Service a2ae7a
    }
Packit Service a2ae7a
  if (!HAVE_USLEEP && micro % 1000000)
Packit Service a2ae7a
    seconds++;
Packit Service a2ae7a
  while ((seconds = sleep (seconds)) != 0);
Packit Service a2ae7a
Packit Service a2ae7a
#undef usleep
Packit Service a2ae7a
#if !HAVE_USLEEP
Packit Service a2ae7a
# define usleep(x) 0
Packit Service a2ae7a
#endif
Packit Service a2ae7a
  return usleep (micro % 1000000);
Packit Service a2ae7a
}