Skip to content

Commit 7dcbff9

Browse files
committed
add system module
There are a number of capabilities it would be useful to bring from cstdlib and STL. This is an initial demonstration, replacing the non-cross-compiler sleep() with a standard implmeentation that works across compilers and operating systems, with millisecond integer input.
1 parent e2b0cda commit 7dcbff9

6 files changed

+79
-8
lines changed

README.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Fortran Standard Library
22

3+
[![Actions Status](https://github.com/fortran-lang/stdlib/workflows/CI/badge.svg)](https://github.com/fortran-lang/stdlib/actions)
4+
[![Actions Status](https://github.com/fortran-lang/stdlib/workflows/CI_windows/badge.svg)](https://github.com/fortran-lang/stdlib/actions)
5+
36
## Scope
47

58
The goal of the Fortran Standard Library is to achieve the following general scope:
@@ -16,19 +19,19 @@ The goal of the Fortran Standard Library is to achieve the following general sco
1619

1720
### Get the code
1821

19-
```
22+
```sh
2023
git clone https://github.com/fortran-lang/stdlib
2124
cd stdlib
2225
```
2326

2427
### Build with CMake
2528

26-
```
27-
mkdir build
28-
cd build
29-
cmake ..
30-
make
31-
ctest
29+
```sh
30+
cmake -B build
31+
32+
cmake --build build
33+
34+
cmake --build build --target test
3235
```
3336

3437
### Build with make

src/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ set(SRC
22
stdlib_experimental_ascii.f90
33
stdlib_experimental_io.f90
44
stdlib_experimental_error.f90
5-
stdlib_experimental_optval.f90
65
stdlib_experimental_kinds.f90
6+
stdlib_experimental_optval.f90
7+
stdlib_experimental_system.F90
78
)
89

910
add_library(fortran_stdlib ${SRC})

src/stdlib_experimental_system.F90

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module stdlib_experimental_system
2+
use, intrinsic :: iso_c_binding, only : c_int, c_long
3+
implicit none
4+
private
5+
public :: sleep
6+
7+
interface
8+
#ifdef _WIN32
9+
subroutine winsleep(dwMilliseconds) bind (C, name='Sleep')
10+
!! void Sleep(DWORD dwMilliseconds)
11+
!! https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep
12+
import c_long
13+
integer(c_long), value, intent(in) :: dwMilliseconds
14+
end subroutine winsleep
15+
#else
16+
integer(c_int) function usleep(usec) bind (C)
17+
!! int usleep(useconds_t usec);
18+
!! https://linux.die.net/man/3/usleep
19+
import c_int
20+
integer(c_int), value, intent(in) :: usec
21+
end function usleep
22+
#endif
23+
end interface
24+
25+
contains
26+
27+
subroutine sleep(millisec)
28+
integer, intent(in) :: millisec
29+
integer(c_int) :: ierr
30+
31+
#ifdef _WIN32
32+
!! PGI Windows, Ifort Windows, ....
33+
call winsleep(int(millisec, c_long))
34+
#else
35+
!! Linux, Unix, MacOS, MSYS2, ...
36+
ierr = usleep(int(millisec * 1000, c_int))
37+
if (ierr/=0) error stop 'problem with usleep() system call'
38+
#endif
39+
40+
41+
end subroutine sleep
42+
43+
end module stdlib_experimental_system

src/tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ endmacro(ADDTEST)
99
add_subdirectory(ascii)
1010
add_subdirectory(io)
1111
add_subdirectory(optval)
12+
add_subdirectory(system)
1213

1314
ADDTEST(always_skip)
1415
set_tests_properties(always_skip PROPERTIES SKIP_RETURN_CODE 77)

src/tests/system/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_executable(test_sleep test_sleep.f90)
2+
target_link_libraries(test_sleep fortran_stdlib)
3+
4+
add_test(NAME Sleep COMMAND $<TARGET_FILE:test_sleep> 650)
5+
set_tests_properties(Sleep PROPERTIES TIMEOUT 5)

src/tests/system/test_sleep.f90

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
program test_sleep
2+
3+
use stdlib_experimental_system, only : sleep
4+
5+
implicit none
6+
7+
integer :: ierr, millisec
8+
character(8) :: argv
9+
10+
millisec = 780
11+
call get_command_argument(1, argv, status=ierr)
12+
if (ierr==0) read(argv,*) millisec
13+
14+
if (millisec<0) millisec=0
15+
16+
call sleep(millisec)
17+
18+
end program

0 commit comments

Comments
 (0)