Added special case for timeouts that don't expire

This commit is contained in:
Marcos Vives Del Sol 2015-06-20 16:59:09 +02:00
parent a9b1b94ffd
commit 6cd025ed00
2 changed files with 20 additions and 3 deletions

View File

@ -22,6 +22,9 @@
#define MICROS_PER_SEC 1000000 #define MICROS_PER_SEC 1000000
#define NANOS_PER_SEC 1000000000 #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 // Use Windows' API directly if Win32 for highest possible resolution
#if defined(CYGWIN) || defined(_WIN32) #if defined(CYGWIN) || defined(_WIN32)
# include <windows.h> # include <windows.h>
@ -61,14 +64,22 @@ void timeout_init(timeout_t * to, unsigned int millis) {
*to = time_millis() + millis; *to = time_millis() + millis;
} }
void timeout_never(timeout_t * to) {
*to = MAGIC_NEVER;
}
bool timeout_check(timeout_t * to) { bool timeout_check(timeout_t * to) {
if (*to == 0) { switch (*to) {
return false; case MAGIC_EXPIRED:
return false;
case MAGIC_NEVER:
return true;
} }
ms_t now = time_millis(); ms_t now = time_millis();
if (now >= *to) { if (now >= *to) {
*to = 0; // Mark as expired and fail in next check
*to = MAGIC_EXPIRED;
} }
return true; return true;

View File

@ -38,6 +38,12 @@ typedef ms_t timeout_t;
*/ */
void timeout_init(timeout_t * to, unsigned int millis); 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 * @brief Checks if the timeout has NOT expired
* @param to Timeout handle * @param to Timeout handle