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