Skip to content

Commit

Permalink
fallback to usleep
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jan 31, 2025
1 parent 03d22f3 commit 9179c6a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 42 deletions.
12 changes: 5 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.15...3.29)
cmake_minimum_required(VERSION 3.15...3.31)

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(FATAL_ERROR "use out-of-source build
Expand All @@ -11,20 +11,17 @@ LANGUAGES C CXX Fortran

enable_testing()

set(CMAKE_CXX_STANDARD 11)
include(CheckSymbolExists)

include(cmake/compilers.cmake)

include(CheckPIESupported)
check_pie_supported()
set(CMAKE_POSITION_INDEPENDENT_CODE true)
check_symbol_exists(nanosleep "time.h" HAVE_NANOSLEEP)

# --- RECOMMENDED: sleep_ms() using C++ stdlib

add_library(sleep_std OBJECT src/sleep_std.f90)
set_property(TARGET sleep_std PROPERTY Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include)

add_library(sleep_cpp src/sleep.cpp $<TARGET_OBJECTS:sleep_std>)
target_compile_features(sleep_cpp PUBLIC cxx_std_11)
target_include_directories(sleep_cpp PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include;${CMAKE_CURRENT_SOURCE_DIR}/src>"
$<INSTALL_INTERFACE:include>
Expand All @@ -38,6 +35,7 @@ add_test(NAME CPPsleep COMMAND main_cpp 150)
# --- sleep_ms() using time.h C -- more code but also robust/standard

add_library(sleep_c src/sleep.c $<TARGET_OBJECTS:sleep_std>)
target_compile_definitions(sleep_c PRIVATE $<$<BOOL:${HAVE_NANOSLEEP}>:HAVE_NANOSLEEP>)
target_include_directories(sleep_c PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include;${CMAKE_CURRENT_SOURCE_DIR}/src>"
$<INSTALL_INTERFACE:include>
Expand Down
27 changes: 0 additions & 27 deletions cmake/compilers.cmake

This file was deleted.

29 changes: 21 additions & 8 deletions src/sleep.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#if defined(__linux__) && !defined(_DEFAULT_SOURCE)
#define _DEFAULT_SOURCE
#endif

#include <stdio.h>

#include "csleep.h"
Expand All @@ -15,8 +19,13 @@ void c_sleep(int milliseconds){

#else

#include <stdlib.h>
#if defined(HAVE_NANOSLEEP)
#include <time.h>
#else
#include <unistd.h>
#endif

#include <stdlib.h>
#include <errno.h>
// https://linux.die.net/man/3/usleep
void c_sleep(int milliseconds)
Expand All @@ -27,31 +36,35 @@ void c_sleep(int milliseconds)
exit(EINVAL);
}

//int ierr = usleep(*milliseconds * 1000);
int ierr = 0;

#if defined(HAVE_NANOSLEEP)
struct timespec t;

t.tv_sec = milliseconds / 1000;
t.tv_nsec = (milliseconds % 1000) * 1000000;

int ierr = nanosleep(&t, NULL);
ierr = nanosleep(&t, NULL);
#else
ierr = usleep(milliseconds * 1000);
#endif

if (ierr != 0){
switch(errno){
case EINTR:
fprintf(stderr, "nanosleep() interrupted\n");
fprintf(stderr, "c_sleep() interrupted\n");
break;
case EINVAL:
fprintf(stderr, "nanosleep() bad milliseconds value\n");
fprintf(stderr, "c_sleep() bad milliseconds value\n");
exit(EINVAL);
case EFAULT:
fprintf(stderr, "nanosleep() bad milliseconds value\n");
fprintf(stderr, "c_sleep() bad milliseconds value\n");
exit(EFAULT);
case ENOSYS:
fprintf(stderr, "nanosleep() not supported on this system\n");
fprintf(stderr, "c_sleep() not supported on this system\n");
exit(ENOSYS);
default:
fprintf(stderr, "nanosleep() error\n");
fprintf(stderr, "c_sleep() error\n");
exit(1);
}
}
Expand Down

0 comments on commit 9179c6a

Please sign in to comment.