forked from fortran-lang/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
6 changed files
with
80 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module stdlib_experimental_system | ||
use, intrinsic :: iso_c_binding, only : c_int, c_long | ||
implicit none | ||
private | ||
public :: sleep | ||
|
||
interface | ||
#ifdef _WIN32 | ||
subroutine winsleep(dwMilliseconds) bind (C, name='Sleep') | ||
!! void Sleep(DWORD dwMilliseconds) | ||
!! https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep | ||
import c_long | ||
integer(c_long), value, intent(in) :: dwMilliseconds | ||
end subroutine winsleep | ||
#else | ||
integer(c_int) function usleep(usec) bind (C) | ||
!! int usleep(useconds_t usec); | ||
!! https://linux.die.net/man/3/usleep | ||
import c_int | ||
integer(c_int), value, intent(in) :: usec | ||
end function usleep | ||
#endif | ||
end interface | ||
|
||
contains | ||
|
||
subroutine sleep(millisec) | ||
integer, intent(in) :: millisec | ||
integer(c_int) :: ierr | ||
|
||
#ifdef _WIN32 | ||
!! PGI Windows, Ifort Windows, .... | ||
call winsleep(int(millisec, c_long)) | ||
#else | ||
!! Linux, Unix, MacOS, MSYS2, ... | ||
ierr = usleep(int(millisec * 1000, c_int)) | ||
if (ierr/=0) error stop 'problem with usleep() system call' | ||
#endif | ||
|
||
|
||
end subroutine sleep | ||
|
||
end module stdlib_experimental_system |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
add_executable(test_sleep test_sleep.f90) | ||
target_link_libraries(test_sleep fortran_stdlib) | ||
|
||
add_test(NAME Sleep COMMAND $<TARGET_FILE:test_sleep> 650) | ||
set_tests_properties(Sleep PROPERTIES TIMEOUT 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
program test_sleep | ||
|
||
use stdlib_experimental_system, only : sleep | ||
|
||
implicit none | ||
|
||
integer :: ierr, millisec | ||
character(8) :: argv | ||
|
||
millisec = 780 | ||
call get_command_argument(1, argv, status=ierr) | ||
if (ierr==0) read(argv,*) millisec | ||
|
||
if (millisec<0) millisec=0 | ||
|
||
call sleep(millisec) | ||
|
||
end program |