Actual source code: psleep.c

  1: #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for usleep()  */
  2: #include <petscsys.h>
  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

 13: /*@
 14:    PetscSleep - Sleeps some number of seconds.

 16:    Not Collective

 18:    Input Parameters:
 19: .  s - number of seconds to sleep

 21:    Note:
 22:    If s is negative waits for keyboard input

 24:    Level: intermediate

 26: @*/
 27: PetscErrorCode PetscSleep(PetscReal s)
 28: {
 29:   if (s < 0) getc(stdin);

 31:     /* Some systems consider it an error to call nanosleep or usleep for more than one second so we only use them for subsecond sleeps. */
 32: #if defined(PETSC_HAVE_NANOSLEEP)
 33:   else if (s < 1) {
 34:     struct timespec rq;
 35:     rq.tv_sec  = 0;
 36:     rq.tv_nsec = (long)(s * 1e9);
 37:     nanosleep(&rq, NULL);
 38:   }
 39: #elif defined(PETSC_HAVE_USLEEP)
 40:   /* POSIX.1-2001 deprecates this in favor of nanosleep because nanosleep defines interaction with signals */
 41:   else if (s < 1) usleep((unsigned int)(s * 1e6));
 42: #endif

 44: #if defined(PETSC_HAVE_SLEEP)
 45:   else
 46:     sleep((int)s);
 47: #elif defined(PETSC_HAVE__SLEEP) && defined(PETSC_HAVE__SLEEP_MILISEC)
 48:   else _sleep((int)(s * 1000));
 49: #elif defined(PETSC_HAVE__SLEEP)
 50:   else _sleep((int)s);
 51: #else
 52:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "No support for sleep() on this machine");
 53: #endif
 54:   return 0;
 55: }