From 6cd025ed002278ae21d9aad59978e8081e2b29e5 Mon Sep 17 00:00:00 2001 From: Marcos Vives Del Sol Date: Sat, 20 Jun 2015 16:59:09 +0200 Subject: [PATCH] Added special case for timeouts that don't expire --- libnfc/timing.c | 17 ++++++++++++++--- libnfc/timing.h | 6 ++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/libnfc/timing.c b/libnfc/timing.c index 8b17d71..e937a39 100644 --- a/libnfc/timing.c +++ b/libnfc/timing.c @@ -22,6 +22,9 @@ #define MICROS_PER_SEC 1000000 #define NANOS_PER_SEC 1000000000 +#define MAGIC_EXPIRED ((uint64_t) -1) +#define MAGIC_NEVER ((uint64_t) -2) + // Use Windows' API directly if Win32 for highest possible resolution #if defined(CYGWIN) || defined(_WIN32) # include @@ -61,14 +64,22 @@ void timeout_init(timeout_t * to, unsigned int millis) { *to = time_millis() + millis; } +void timeout_never(timeout_t * to) { + *to = MAGIC_NEVER; +} + bool timeout_check(timeout_t * to) { - if (*to == 0) { - return false; + switch (*to) { + case MAGIC_EXPIRED: + return false; + case MAGIC_NEVER: + return true; } ms_t now = time_millis(); if (now >= *to) { - *to = 0; + // Mark as expired and fail in next check + *to = MAGIC_EXPIRED; } return true; diff --git a/libnfc/timing.h b/libnfc/timing.h index beb9e47..5ee7bab 100644 --- a/libnfc/timing.h +++ b/libnfc/timing.h @@ -38,6 +38,12 @@ typedef ms_t timeout_t; */ void timeout_init(timeout_t * to, unsigned int millis); +/** + * @brief Initializes a timeout which never expires + * @param to Timeout handle + */ +void timeout_never(timeout_t * to); + /** * @brief Checks if the timeout has NOT expired * @param to Timeout handle