Skip to content

Commit c3051e6

Browse files
committed
sleep tests work on more systems, test more robustly
1 parent 4a725fa commit c3051e6

2 files changed

Lines changed: 24 additions & 13 deletions

File tree

src/sleep/sleep.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include <stdio.h>
22

3-
#ifdef _MSC_VER
4-
#define WIN32_LEAN_AND_MEAN
5-
#include <windows.h>
6-
#else
3+
#if defined(HAVE_NANOSLEEP)
74
#include <time.h>
85
#include <errno.h>
6+
#elif defined(_WIN32)
7+
#define WIN32_LEAN_AND_MEAN
8+
#include <windows.h>
99
#endif
1010

1111
#include "sleep.h"
@@ -20,9 +20,7 @@ void c_sleep(const int milliseconds)
2020
return;
2121
}
2222

23-
#ifdef _MSC_VER
24-
Sleep(milliseconds);
25-
#else
23+
#if defined(HAVE_NANOSLEEP)
2624
// https://linux.die.net/man/3/usleep
2725
//int ierr = usleep(*milliseconds * 1000);
2826

@@ -37,21 +35,25 @@ void c_sleep(const int milliseconds)
3735

3836
switch(errno){
3937
case EINTR:
40-
fprintf(stderr, "nanosleep() interrupted\n");
38+
fprintf(stderr, "nanosleep interrupted\n");
4139
break;
4240
case EINVAL:
43-
fprintf(stderr, "nanosleep() invalid timespec value (EINVAL)\n");
41+
fprintf(stderr, "nanosleep invalid timespec value (EINVAL)\n");
4442
break;
4543
case EFAULT:
46-
fprintf(stderr, "nanosleep() timespec points outside accessible address space (EFAULT)\n");
44+
fprintf(stderr, "nanosleep timespec points outside accessible address space (EFAULT)\n");
4745
break;
4846
case ENOSYS:
49-
fprintf(stderr, "nanosleep() not supported on this system\n");
47+
fprintf(stderr, "nanosleep not supported on this system\n");
5048
break;
5149
default:
52-
fprintf(stderr, "nanosleep() error\n");
50+
fprintf(stderr, "nanosleep error\n");
5351
break;
5452
}
53+
#elif defined(_WIN32)
54+
Sleep(milliseconds);
55+
#else
56+
#error "No nanosleep implementation available for this platform"
5557
#endif
5658

5759
}

test/sleep/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ set_property(DIRECTORY PROPERTY LABELS sleep)
1313

1414
# set_property(TEST Fortran_sleep PROPERTY DISABLED ${bad_ticks})
1515

16+
include(CheckSymbolExists)
17+
check_symbol_exists(nanosleep time.h HAVE_NANOSLEEP)
18+
19+
1620
add_library(f_sleep OBJECT ${PROJECT_SOURCE_DIR}/src/sleep/sleep_std.f90)
1721

1822
# not OBJECT so include propagates
1923
add_library(c_f_sleep ${PROJECT_SOURCE_DIR}/src/sleep/sleep.c $<TARGET_OBJECTS:f_sleep>)
2024
target_include_directories(c_f_sleep PUBLIC ${PROJECT_SOURCE_DIR}/src/sleep)
25+
target_compile_definitions(c_f_sleep PRIVATE $<$<BOOL:${HAVE_NANOSLEEP}>:HAVE_NANOSLEEP>)
2126

2227
# not OBJECT so include propagates
2328
add_library(cpp_f_sleep ${PROJECT_SOURCE_DIR}/src/sleep/sleep.cpp $<TARGET_OBJECTS:f_sleep>)
@@ -71,5 +76,9 @@ add_test(NAME Fortran_micro_sleep COMMAND fortran_micro_sleep)
7176
endif()
7277

7378
get_property(tests DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY TESTS)
74-
set_property(TEST ${tests} PROPERTY TIMEOUT 5)
79+
set_tests_properties(${tests} PROPERTIES
80+
TIMEOUT 5
81+
RUN_SERIAL true
82+
)
83+
# need RUN_SERIAL for first-time run of lots of tests, system might have artifact of running too slow one-time
7584
# allow extra time for valgrind. Have some timeout b/c macOS can hang for example.

0 commit comments

Comments
 (0)