Use a common sleep implementation

Implement a common sleep function to eliminate some conditional
compilation in the rest of the code.
This commit is contained in:
Jef Driesen 2021-03-23 21:12:18 +01:00
parent a4d771956a
commit 752a064bb3
8 changed files with 74 additions and 31 deletions

View File

@ -398,6 +398,10 @@
RelativePath="..\src\parser.c"
>
</File>
<File
RelativePath="..\src\platform.c"
>
</File>
<File
RelativePath="..\src\rbstream.c"
>

View File

@ -64,7 +64,7 @@ libdivecomputer_la_SOURCES = \
diverite_nitekq.h diverite_nitekq.c diverite_nitekq_parser.c \
citizen_aqualand.h citizen_aqualand.c citizen_aqualand_parser.c \
divesystem_idive.h divesystem_idive.c divesystem_idive_parser.c \
platform.h \
platform.h platform.c \
ringbuffer.h ringbuffer.c \
rbstream.h rbstream.c \
checksum.h checksum.c \

View File

@ -202,11 +202,7 @@ dc_irda_iterator_new (dc_iterator_t **out, dc_context_t *context, dc_descriptor_
// modified by the previous getsockopt call.
size = sizeof (data);
#ifdef _WIN32
Sleep (1000);
#else
sleep (1);
#endif
dc_platform_sleep (1000);
}
S_CLOSE (fd);

51
src/platform.c Normal file
View File

@ -0,0 +1,51 @@
/*
* libdivecomputer
*
* Copyright (C) 2021 Jef Driesen
*
* 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
*/
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOGDI
#include <windows.h>
#else
#include <time.h>
#include <errno.h>
#endif
#include "platform.h"
int
dc_platform_sleep (unsigned int milliseconds)
{
#ifdef _WIN32
Sleep (milliseconds);
#else
struct timespec ts;
ts.tv_sec = (milliseconds / 1000);
ts.tv_nsec = (milliseconds % 1000) * 1000000;
while (nanosleep (&ts, &ts) != 0) {
if (errno != EINTR ) {
return -1;
}
}
#endif
return 0;
}

View File

@ -45,6 +45,8 @@ extern "C" {
#endif
#endif
int dc_platform_sleep(unsigned int milliseconds);
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -30,7 +30,6 @@
#include <fcntl.h> // fcntl
#include <termios.h> // tcgetattr, tcsetattr, cfsetispeed, cfsetospeed, tcflush, tcsendbreak
#include <sys/ioctl.h> // ioctl
#include <time.h> // nanosleep
#ifdef HAVE_LINUX_SERIAL_H
#include <linux/serial.h>
#endif
@ -59,6 +58,7 @@
#include "iostream-private.h"
#include "iterator-private.h"
#include "descriptor-private.h"
#include "platform.h"
#include "timer.h"
#define DIRNAME "/dev"
@ -995,16 +995,10 @@ dc_serial_get_lines (dc_iostream_t *abstract, unsigned int *value)
static dc_status_t
dc_serial_sleep (dc_iostream_t *abstract, unsigned int timeout)
{
struct timespec ts;
ts.tv_sec = (timeout / 1000);
ts.tv_nsec = (timeout % 1000) * 1000000;
while (nanosleep (&ts, &ts) != 0) {
if (dc_platform_sleep (timeout) != 0) {
int errcode = errno;
if (errcode != EINTR ) {
SYSERROR (abstract->context, errcode);
return syserror (errcode);
}
SYSERROR (abstract->context, errcode);
return syserror (errcode);
}
return DC_STATUS_SUCCESS;

View File

@ -32,6 +32,7 @@
#include "iostream-private.h"
#include "iterator-private.h"
#include "descriptor-private.h"
#include "platform.h"
static dc_status_t dc_serial_iterator_next (dc_iterator_t *iterator, void *item);
static dc_status_t dc_serial_iterator_free (dc_iterator_t *iterator);
@ -836,7 +837,11 @@ dc_serial_get_lines (dc_iostream_t *abstract, unsigned int *value)
static dc_status_t
dc_serial_sleep (dc_iostream_t *abstract, unsigned int timeout)
{
Sleep (timeout);
if (dc_platform_sleep (timeout) != 0) {
DWORD errcode = GetLastError ();
SYSERROR (abstract->context, errcode);
return syserror (errcode);
}
return DC_STATUS_SUCCESS;
}

View File

@ -20,6 +20,7 @@
*/
#include "socket.h"
#include "platform.h"
#include "common-private.h"
#include "context-private.h"
@ -357,21 +358,11 @@ dc_socket_ioctl (dc_iostream_t *abstract, unsigned int request, void *data, size
dc_status_t
dc_socket_sleep (dc_iostream_t *abstract, unsigned int timeout)
{
#ifdef _WIN32
Sleep (timeout);
#else
struct timespec ts;
ts.tv_sec = (timeout / 1000);
ts.tv_nsec = (timeout % 1000) * 1000000;
while (nanosleep (&ts, &ts) != 0) {
int errcode = errno;
if (errcode != EINTR ) {
SYSERROR (abstract->context, errcode);
return dc_socket_syserror (errcode);
}
if (dc_platform_sleep (timeout) != 0) {
s_errcode_t errcode = S_ERRNO;
SYSERROR (abstract->context, errcode);
return dc_socket_syserror(errcode);
}
#endif
return DC_STATUS_SUCCESS;
}