xref: /petsc/src/sys/utils/psleep.c (revision 3f02e49b19195914bf17f317a25cb39636853415)
1 #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for usleep()  */
2 #include <petscsys.h>                    /*I   "petscsys.h"    I*/
3 #if defined(PETSC_HAVE_UNISTD_H)
4   #include <unistd.h>
5 #endif
6 #if defined(PETSC_HAVE_DOS_H) /* borland */
7   #include <dos.h>
8 #endif
9 #if defined(PETSC_HAVE_TIME_H)
10   #include <time.h>
11 #endif
12 
13 /*@
14   PetscSleep - Sleeps some number of seconds.
15 
16   Not Collective
17 
18   Input Parameter:
19 . s - number of seconds to sleep
20 
21   Level: intermediate
22 
23   Note:
24   If `s` is negative waits for keyboard input
25 
26 .seealso: `PetscTime()`
27 @*/
28 PetscErrorCode PetscSleep(PetscReal s)
29 {
30   PetscFunctionBegin;
31   if (s < 0) getc(stdin);
32 
33   /* Some systems consider it an error to call nanosleep or usleep for more than one second so we only use them for subsecond sleeps. */
34 #if defined(PETSC_HAVE_NANOSLEEP)
35   else if (s < 1) {
36     struct timespec rq;
37     rq.tv_sec  = 0;
38     rq.tv_nsec = (long)(s * 1e9);
39     nanosleep(&rq, NULL);
40   }
41 #elif defined(PETSC_HAVE_USLEEP)
42   /* POSIX.1-2001 deprecates this in favor of nanosleep because nanosleep defines interaction with signals */
43   else if (s < 1) usleep((unsigned int)(s * 1e6));
44 #endif
45 
46 #if defined(PETSC_HAVE_SLEEP)
47   else
48     sleep((int)s);
49 #elif defined(PETSC_HAVE__SLEEP) && defined(PETSC_HAVE__SLEEP_MILISEC)
50   else _sleep((int)(s * 1000));
51 #elif defined(PETSC_HAVE__SLEEP)
52   else _sleep((int)s);
53 #else
54   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "No support for sleep() on this machine");
55 #endif
56   PetscFunctionReturn(PETSC_SUCCESS);
57 }
58