-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from scivision/systemlib
add system module
- Loading branch information
Showing
6 changed files
with
95 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> 350) | ||
set_tests_properties(Sleep PROPERTIES TIMEOUT 1) |
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,33 @@ | ||
program test_sleep | ||
use, intrinsic :: iso_fortran_env, only : int64, real64 | ||
use stdlib_experimental_system, only : sleep | ||
|
||
implicit none | ||
|
||
integer :: ierr, millisec | ||
character(8) :: argv | ||
integer(int64) :: tic, toc, trate | ||
real(real64) :: t_ms | ||
|
||
call system_clock(count_rate=trate) | ||
|
||
millisec = 780 | ||
call get_command_argument(1, argv, status=ierr) | ||
if (ierr==0) read(argv,*) millisec | ||
|
||
if (millisec<0) millisec=0 | ||
|
||
call system_clock(count=tic) | ||
call sleep(millisec) | ||
call system_clock(count=toc) | ||
|
||
t_ms = (toc-tic) * 1000._real64 / trate | ||
|
||
if (millisec > 0) then | ||
if (t_ms < 0.5 * millisec) error stop 'actual sleep time was too short' | ||
if (t_ms > 2 * millisec) error stop 'actual sleep time was too long' | ||
endif | ||
|
||
print '(A,F8.3)', 'OK: test_sleep: slept for (ms): ',t_ms | ||
|
||
end program |