Skip to content

Commit f300f4a

Browse files
authored
Merge pull request #54 from scivision/systemlib
add system module
2 parents 12bd060 + 0ea0ee1 commit f300f4a

6 files changed

+95
-8
lines changed

README.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
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+
6+
37
## Goals and Motivation
48

59
The Fortran Standard, as published by the ISO (https://wg5-fortran.org/), does
@@ -31,19 +35,19 @@ The goal of the Fortran Standard Library is to achieve the following general sco
3135

3236
### Get the code
3337

34-
```
38+
```sh
3539
git clone https://github.com/fortran-lang/stdlib
3640
cd stdlib
3741
```
3842

3943
### Build with CMake
4044

41-
```
42-
mkdir build
43-
cd build
44-
cmake ..
45-
make
46-
ctest
45+
```sh
46+
cmake -B build
47+
48+
cmake --build build
49+
50+
cmake --build build --target test
4751
```
4852

4953
### 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> 350)
5+
set_tests_properties(Sleep PROPERTIES TIMEOUT 1)

src/tests/system/test_sleep.f90

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
program test_sleep
2+
use, intrinsic :: iso_fortran_env, only : int64, real64
3+
use stdlib_experimental_system, only : sleep
4+
5+
implicit none
6+
7+
integer :: ierr, millisec
8+
character(8) :: argv
9+
integer(int64) :: tic, toc, trate
10+
real(real64) :: t_ms
11+
12+
call system_clock(count_rate=trate)
13+
14+
millisec = 780
15+
call get_command_argument(1, argv, status=ierr)
16+
if (ierr==0) read(argv,*) millisec
17+
18+
if (millisec<0) millisec=0
19+
20+
call system_clock(count=tic)
21+
call sleep(millisec)
22+
call system_clock(count=toc)
23+
24+
t_ms = (toc-tic) * 1000._real64 / trate
25+
26+
if (millisec > 0) then
27+
if (t_ms < 0.5 * millisec) error stop 'actual sleep time was too short'
28+
if (t_ms > 2 * millisec) error stop 'actual sleep time was too long'
29+
endif
30+
31+
print '(A,F8.3)', 'OK: test_sleep: slept for (ms): ',t_ms
32+
33+
end program

0 commit comments

Comments
 (0)