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