Skip to content

Commit de51391

Browse files
committed
sleep: header, print fail reason
1 parent d70ef7d commit de51391

File tree

7 files changed

+34
-15
lines changed

7 files changed

+34
-15
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
timeout-minutes: 10
6464

6565
env:
66-
FC: gfortran-13
66+
FC: gfortran-12
6767

6868
steps:
6969
- name: install valgrind
@@ -73,7 +73,7 @@ jobs:
7373
7474
- uses: actions/checkout@v4
7575

76-
- run: ctest -S memcheck.cmake -V -E "Fortran_nano_sleep|string_array"
76+
- run: ctest -S memcheck.cmake -VV -E "Fortran_nano_sleep|string_array"
7777

7878

7979
linux-flang:

CMakePresets.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"noTestsAction": "error",
5050
"scheduleRandom": true,
5151
"stopOnFailure": false,
52-
"timeout": 60
52+
"timeout": 15
5353
}
5454
},
5555
{

src/sleep/sleep.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
#include <errno.h>
99
#endif
1010

11+
#include "sleep.h"
1112

12-
void c_sleep(int);
1313

1414
// https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep
15-
void c_sleep(int milliseconds)
15+
void c_sleep(const int milliseconds)
1616
{
1717

1818
#ifdef _MSC_VER

src/sleep/sleep.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#include <chrono>
22
#include <thread>
33

4-
extern "C" void c_sleep(int);
4+
#include "sleep.h"
55

6-
void c_sleep(int milliseconds)
7-
{
6+
void c_sleep(const int milliseconds) {
87
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
98
}

src/sleep/sleep.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
void c_sleep(const int);
6+
7+
#ifdef __cplusplus
8+
}
9+
#endif

test/sleep/CMakeLists.txt

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ set_property(DIRECTORY PROPERTY LABELS sleep)
22

33
add_library(f_sleep OBJECT ${PROJECT_SOURCE_DIR}/src/sleep/sleep_std.f90)
44

5-
add_library(c_f_sleep ${PROJECT_SOURCE_DIR}/src/sleep/sleep.c)
6-
target_link_libraries(c_f_sleep PRIVATE f_sleep)
7-
add_library(cpp_f_sleep ${PROJECT_SOURCE_DIR}/src/sleep/sleep.cpp)
8-
target_link_libraries(cpp_f_sleep PRIVATE f_sleep)
5+
# not OBJECT so include propagates
6+
add_library(c_f_sleep ${PROJECT_SOURCE_DIR}/src/sleep/sleep.c $<TARGET_OBJECTS:f_sleep>)
7+
target_include_directories(c_f_sleep PUBLIC ${PROJECT_SOURCE_DIR}/src/sleep)
8+
9+
# not OBJECT so include propagates
10+
add_library(cpp_f_sleep ${PROJECT_SOURCE_DIR}/src/sleep/sleep.cpp $<TARGET_OBJECTS:f_sleep>)
11+
target_include_directories(cpp_f_sleep PUBLIC ${PROJECT_SOURCE_DIR}/src/sleep)
912

1013
add_executable(fortran_c_sleep test_sleep.f90)
1114
target_link_libraries(fortran_c_sleep PRIVATE c_f_sleep)

test/sleep/test_sleep.f90

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
program sleep_demo
22

3-
use, intrinsic :: iso_fortran_env, only : int64
3+
use, intrinsic :: iso_fortran_env, only : int64, stderr => error_unit
44

55
use sleep_std, only : sleep_ms
66

@@ -28,8 +28,16 @@ program sleep_demo
2828

2929
t_ms = real(toc-tic) * 1000. / real(trate)
3030

31-
if (t_ms < 0.5 * millisec) error stop 'actual sleep time was too short'
32-
if (t_ms > 2 * millisec) error stop 'actual sleep time was too long'
31+
if (t_ms < 0.5 * millisec) then
32+
!> slept less than half expected time
33+
write(stderr, '(a, f9.6)') 'ERROR: measured sleep time too short (millisec): ', t_ms
34+
error stop
35+
endif
36+
if (t_ms > 2 * millisec) then
37+
!> slept more than twice expected time
38+
write(stderr, '(a, f9.6)') 'ERROR: measure sleep time too long (millisec): ', t_ms
39+
error stop
40+
endif
3341

3442
print '(A, F6.1)', 'OK: test_sleep: slept for (ms): ', t_ms
3543

0 commit comments

Comments
 (0)