mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
utilities: use nanosleep() instead of usleep() for upm_delay*() functions
usleep() has been deprecated, and can cause compile problems on never glibc versions (debian jesse). We now use nanosleep() on linux systems to implement delays. Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
parent
d2e0327c30
commit
90d856d128
@ -23,17 +23,29 @@
|
|||||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <upm_platform.h>
|
#include <upm_platform.h>
|
||||||
#include <upm_utilities.h>
|
#include <upm_utilities.h>
|
||||||
|
|
||||||
void upm_delay(int time)
|
void upm_delay(unsigned int time)
|
||||||
{
|
{
|
||||||
if (time <= 0)
|
if (time <= 0)
|
||||||
time = 1;
|
time = 1;
|
||||||
|
|
||||||
#if defined(UPM_PLATFORM_LINUX)
|
#if defined(UPM_PLATFORM_LINUX)
|
||||||
|
|
||||||
sleep(time);
|
struct timespec delay_time;
|
||||||
|
|
||||||
|
delay_time.tv_sec = time;
|
||||||
|
delay_time.tv_nsec = 0;
|
||||||
|
// The advantage over sleep(3) here is that it will not use
|
||||||
|
// an alarm signal or handler.
|
||||||
|
|
||||||
|
// here we spin until the delay is complete - detecting signals
|
||||||
|
// and continuing where we left off
|
||||||
|
while (nanosleep(&delay_time, &delay_time) && errno == EINTR)
|
||||||
|
; // loop
|
||||||
|
|
||||||
#elif defined(UPM_PLATFORM_ZEPHYR)
|
#elif defined(UPM_PLATFORM_ZEPHYR)
|
||||||
# if KERNEL_VERSION_MAJOR == 1 && KERNEL_VERSION_MINOR >= 6
|
# if KERNEL_VERSION_MAJOR == 1 && KERNEL_VERSION_MINOR >= 6
|
||||||
@ -56,14 +68,21 @@ void upm_delay(int time)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void upm_delay_ms(int time)
|
void upm_delay_ms(unsigned int time)
|
||||||
{
|
{
|
||||||
if (time <= 0)
|
if (time <= 0)
|
||||||
time = 1;
|
time = 1;
|
||||||
|
|
||||||
#if defined(UPM_PLATFORM_LINUX)
|
#if defined(UPM_PLATFORM_LINUX)
|
||||||
|
|
||||||
usleep(1000 * time);
|
struct timespec delay_time;
|
||||||
|
|
||||||
|
delay_time.tv_sec = time / 1000;
|
||||||
|
delay_time.tv_nsec = (time % 1000) * 1000000;
|
||||||
|
// here we spin until the delay is complete - detecting signals
|
||||||
|
// and continuing where we left off
|
||||||
|
while (nanosleep(&delay_time, &delay_time) && errno == EINTR)
|
||||||
|
; // loop
|
||||||
|
|
||||||
#elif defined(UPM_PLATFORM_ZEPHYR)
|
#elif defined(UPM_PLATFORM_ZEPHYR)
|
||||||
# if KERNEL_VERSION_MAJOR == 1 && KERNEL_VERSION_MINOR >= 6
|
# if KERNEL_VERSION_MAJOR == 1 && KERNEL_VERSION_MINOR >= 6
|
||||||
@ -85,14 +104,21 @@ void upm_delay_ms(int time)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void upm_delay_us(int time)
|
void upm_delay_us(unsigned int time)
|
||||||
{
|
{
|
||||||
if (time <= 0)
|
if (time <= 0)
|
||||||
time = 1;
|
time = 1;
|
||||||
|
|
||||||
#if defined(UPM_PLATFORM_LINUX)
|
#if defined(UPM_PLATFORM_LINUX)
|
||||||
|
|
||||||
usleep(time);
|
struct timespec delay_time;
|
||||||
|
|
||||||
|
delay_time.tv_sec = time / 1000000;
|
||||||
|
delay_time.tv_nsec = (time % 1000000) * 1000;
|
||||||
|
// here we spin until the delay is complete - detecting signals
|
||||||
|
// and continuing where we left off
|
||||||
|
while (nanosleep(&delay_time, &delay_time) && errno == EINTR)
|
||||||
|
; // loop
|
||||||
|
|
||||||
#elif defined(UPM_PLATFORM_ZEPHYR)
|
#elif defined(UPM_PLATFORM_ZEPHYR)
|
||||||
# if KERNEL_VERSION_MAJOR == 1 && KERNEL_VERSION_MINOR >= 6
|
# if KERNEL_VERSION_MAJOR == 1 && KERNEL_VERSION_MINOR >= 6
|
||||||
|
@ -69,21 +69,21 @@ typedef uint32_t upm_clock_t;
|
|||||||
*
|
*
|
||||||
* @param time The number of seconds to delay for
|
* @param time The number of seconds to delay for
|
||||||
*/
|
*/
|
||||||
void upm_delay(int time);
|
void upm_delay(unsigned int time);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delay for a number of milliseconds
|
* Delay for a number of milliseconds
|
||||||
*
|
*
|
||||||
* @param time The number of milliseconds to delay for
|
* @param time The number of milliseconds to delay for
|
||||||
*/
|
*/
|
||||||
void upm_delay_ms(int time);
|
void upm_delay_ms(unsigned int time);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delay for a number of microseconds
|
* Delay for a number of microseconds
|
||||||
*
|
*
|
||||||
* @param time The number of microseconds to delay for
|
* @param time The number of microseconds to delay for
|
||||||
*/
|
*/
|
||||||
void upm_delay_us(int time);
|
void upm_delay_us(unsigned int time);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize a clock. This can be used with upm_elapsed_ms() and
|
* Initialize a clock. This can be used with upm_elapsed_ms() and
|
||||||
|
Loading…
x
Reference in New Issue
Block a user