Skip to content

Commit 7fbbba0

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 c789d1f commit 7fbbba0

6 files changed

+67
-7
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

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set(SRC
33
stdlib_experimental_io.f90
44
stdlib_experimental_error.f90
55
stdlib_experimental_optval.f90
6+
stdlib_experimental_system.F90
67
)
78

89
add_library(fortran_stdlib ${SRC})

src/stdlib_experimental_system.F90

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module stdlib_experimental_system
2+
use, intrinsic :: iso_c_binding, only : c_int
3+
implicit none
4+
private
5+
6+
interface
7+
#ifdef _WIN32
8+
subroutine system_sleep(ms) bind (C, name='Sleep')
9+
import c_int
10+
integer(c_int), value, intent(in) :: ms
11+
end subroutine system_sleep
12+
#else
13+
subroutine system_sleep(us) bind (C, name='usleep')
14+
import c_int
15+
integer(c_int), value, intent(in) :: us
16+
end subroutine system_sleep
17+
#endif
18+
end interface
19+
20+
public :: sleep
21+
22+
contains
23+
24+
subroutine sleep(millisec)
25+
integer, intent(in) :: millisec
26+
27+
#ifndef _WIN32
28+
millisec = millisec * 1000
29+
#endif
30+
31+
call system_sleep(int(millisec, c_int))
32+
end subroutine sleep
33+
34+
end module stdlib_experimental_system

src/tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_subdirectory(ascii)
22
add_subdirectory(io)
33
add_subdirectory(optval)
4+
add_subdirectory(system)
45

56
add_executable(test_skip test_skip.f90)
67
target_link_libraries(test_skip fortran_stdlib)

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> 1234)
5+
set_tests_properties(Sleep PROPERTIES TIMEOUT 5)

src/tests/system/test_sleep.f90

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

0 commit comments

Comments
 (0)