busybox/libbb/bb_do_delay.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Busybox utility routines.
   4 *
   5 * Copyright (C) 2005 by Tito Ragusa <tito-wolit@tiscali.it>
   6 *
   7 * Licensed under GPLv2, see file LICENSE in this source tree.
   8 */
   9#include "libbb.h"
  10
  11/* void FAST_FUNC bb_do_delay(unsigned seconds) { ... } - no users yet */
  12
  13#ifndef LOGIN_FAIL_DELAY
  14#define LOGIN_FAIL_DELAY 3
  15#endif
  16void FAST_FUNC pause_after_failed_login(void)
  17{
  18#if 0 /* over-engineered madness */
  19        time_t end, diff;
  20
  21        end = time(NULL) + LOGIN_FAIL_DELAY;
  22        diff = LOGIN_FAIL_DELAY;
  23        do {
  24                sleep(diff);
  25                diff = end - time(NULL);
  26        } while (diff > 0);
  27#else
  28        sleep(LOGIN_FAIL_DELAY);
  29#endif
  30}
  31
  32void FAST_FUNC sleep1(void)
  33{
  34        sleep(1);
  35}
  36
  37void FAST_FUNC msleep(unsigned ms)
  38{
  39#if 0
  40        /* 1. usleep(n) is not guaranteed by standards to accept n >= 1000000
  41         * 2. multiplication in usleep(ms * 1000) can overflow if ms > 4294967
  42         *    (sleep of ~71.5 minutes)
  43         * Let's play safe and loop:
  44         */
  45        while (ms > 500) {
  46                usleep(500000);
  47                ms -= 500;
  48        }
  49        usleep(ms * 1000);
  50#else
  51//usleep is often implemented as a call to nanosleep.
  52//Simply do the same to implement msleep.
  53//it's marginally larger, but wakes your CPU less often:
  54//function    old     new   delta
  55//msleep       45      52      +7
  56        struct timespec ts;
  57        ts.tv_sec = ms / 1000;
  58        ts.tv_nsec = (ms % 1000) * 1000000;
  59        /*
  60         * If a signal has non-default handler, nanosleep returns early.
  61         * Our version of msleep doesn't return early
  62         * if interrupted by such signals:
  63         */
  64        while (nanosleep(&ts, &ts) != 0)
  65                continue;
  66#endif
  67}
  68