| 1 | /* |
| 2 | * Copyright (C) - 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License as published by the |
| 6 | * Free Software Foundation; version 2.1 of the License. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this library; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <time.h> |
| 20 | #include <assert.h> |
| 21 | #include <unistd.h> |
| 22 | #include <stdio.h> |
| 23 | #include <errno.h> |
| 24 | |
| 25 | #define NSEC_PER_SEC 1000000000ULL |
| 26 | #define NSEC_PER_USEC 1000ULL |
| 27 | |
| 28 | static inline |
| 29 | int64_t elapsed_time_ns(struct timespec *t1, struct timespec *t2) |
| 30 | { |
| 31 | struct timespec delta; |
| 32 | |
| 33 | assert(t1 && t2); |
| 34 | delta.tv_sec = t2->tv_sec - t1->tv_sec; |
| 35 | delta.tv_nsec = t2->tv_nsec - t1->tv_nsec; |
| 36 | return ((int64_t) NSEC_PER_SEC * (int64_t) delta.tv_sec) + |
| 37 | (int64_t) delta.tv_nsec; |
| 38 | } |
| 39 | |
| 40 | int usleep_safe(useconds_t usec) |
| 41 | { |
| 42 | int ret = 0; |
| 43 | struct timespec t1, t2; |
| 44 | int64_t time_remaining_ns = (int64_t) usec * (int64_t) NSEC_PER_USEC; |
| 45 | |
| 46 | ret = clock_gettime(CLOCK_MONOTONIC, &t1); |
| 47 | if (ret) { |
| 48 | ret = -1; |
| 49 | perror("clock_gettime"); |
| 50 | goto end; |
| 51 | } |
| 52 | |
| 53 | while (time_remaining_ns > 0) { |
| 54 | ret = usleep(time_remaining_ns / (int64_t) NSEC_PER_USEC); |
| 55 | if (ret && errno != EINTR) { |
| 56 | perror("usleep"); |
| 57 | goto end; |
| 58 | } |
| 59 | |
| 60 | ret = clock_gettime(CLOCK_MONOTONIC, &t2); |
| 61 | if (ret) { |
| 62 | perror("clock_gettime"); |
| 63 | goto end; |
| 64 | } |
| 65 | |
| 66 | time_remaining_ns -= elapsed_time_ns(&t1, &t2); |
| 67 | } |
| 68 | end: |
| 69 | return ret; |
| 70 | } |