2020-01-05 21:37:33 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup rig
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file src/sleep.c
|
|
|
|
* \brief Replacements for sleep and usleep
|
|
|
|
* \author Michael Black W9MDB
|
|
|
|
* \date 2020
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Hamlib Interface - Replacements for sleep and usleep
|
|
|
|
* Copyright (c) 2020 by Michael Black W9MDB
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief provide sleep and usleep replacements
|
2020-04-05 03:49:27 +00:00
|
|
|
* \note parameters are same as man page for each
|
2020-01-05 21:37:33 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2020-01-06 13:25:38 +00:00
|
|
|
#include <time.h>
|
2020-01-09 04:50:37 +00:00
|
|
|
#include <pthread.h>
|
2020-01-10 21:58:16 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "sleep.h"
|
2020-01-09 04:50:37 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2020-01-05 21:37:33 +00:00
|
|
|
|
2020-01-08 05:18:56 +00:00
|
|
|
// In order to stop the usleep warnings in cppcheck we provide our own interface
|
|
|
|
// So this will use system usleep or our usleep depending on availability of nanosleep
|
|
|
|
// This version of usleep can handle > 1000000 usec values
|
2020-06-13 22:40:47 +00:00
|
|
|
int hl_usleep(rig_useconds_t usec)
|
2020-01-08 05:18:56 +00:00
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
while (usec > 1000000)
|
|
|
|
{
|
|
|
|
if (retval != 0) { return retval; }
|
|
|
|
|
|
|
|
retval = usleep(1000000);
|
|
|
|
usec -= 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return usleep(usec);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_NANOSLEEP
|
2020-07-04 16:33:11 +00:00
|
|
|
#ifndef HAVE_SLEEP
|
2020-04-09 22:56:19 +00:00
|
|
|
/**
|
|
|
|
* \brief sleep
|
|
|
|
* \param secs is seconds to sleep
|
|
|
|
*/
|
2020-01-05 21:37:33 +00:00
|
|
|
unsigned int sleep(unsigned int secs)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
struct timespec t;
|
|
|
|
struct timespec tleft;
|
|
|
|
t.tv_sec = secs;
|
|
|
|
t.tv_nsec = 0;
|
|
|
|
retval = nanosleep(&t, &tleft);
|
|
|
|
|
|
|
|
if (retval == -1) { return tleft.tv_sec; }
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-04 16:33:11 +00:00
|
|
|
#endif
|
2020-01-05 21:37:33 +00:00
|
|
|
|
2020-01-08 05:18:56 +00:00
|
|
|
|
2020-06-13 22:40:47 +00:00
|
|
|
#if 0
|
2020-04-09 22:56:19 +00:00
|
|
|
/**
|
|
|
|
* \brief microsecond sleep
|
|
|
|
* \param usec is microseconds to sleep
|
|
|
|
* This does not have the same 1000000 limit as POSIX usleep
|
|
|
|
*/
|
2020-06-13 22:40:47 +00:00
|
|
|
int usleep(rig_useconds_t usec)
|
2020-01-05 21:37:33 +00:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
unsigned long sec = usec / 1000000ul;
|
|
|
|
unsigned long nsec = usec * 1000ul - (sec * 1000000000ul);
|
|
|
|
struct timespec t;
|
|
|
|
t.tv_sec = sec;
|
|
|
|
t.tv_nsec = nsec;
|
|
|
|
retval = nanosleep(&t, NULL);
|
|
|
|
|
|
|
|
// EINTR is the only error return usleep should need
|
|
|
|
// since EINVAL should not be a problem
|
|
|
|
if (retval == -1) { return EINTR; }
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-06-13 22:40:47 +00:00
|
|
|
#endif
|
2020-04-10 04:35:22 +00:00
|
|
|
|
2020-01-07 23:33:37 +00:00
|
|
|
#endif // HAVE_NANOSLEEP
|
2020-01-09 04:50:37 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2020-04-10 04:35:22 +00:00
|
|
|
/** @} */
|