kernel: add defines for delta_ticks_from_prev special values

Use _INACTIVE instead of hardcoding -1.

_EXPIRED is defined as -2 and will be used for an improvement so that
interrupts are not locked for a non-deterministic amount of time while
handling expired timeouts.

_abort_timeout/_abort_thread_timeout return _INACTIVE instead of -1 if
the timeout has already been disabled.

Change-Id: If99226ff316a62c27b2a2e4e874388c3c44a8aeb
Signed-off-by: Benjamin Walsh <benjamin.walsh@windriver.com>
diff --git a/include/kernel.h b/include/kernel.h
index 2e7801a..e206f29 100644
--- a/include/kernel.h
+++ b/include/kernel.h
@@ -243,13 +243,19 @@
  * @cond INTERNAL_HIDDEN
  */
 
+/* timeout has timed out and is not on _timeout_q anymore */
+#define _EXPIRED (-2)
+
+/* timeout is not in use */
+#define _INACTIVE (-1)
+
 #ifdef CONFIG_SYS_CLOCK_EXISTS
 #define _THREAD_TIMEOUT_INIT(obj) \
 	(obj).nano_timeout = { \
 	.node = { {0}, {0} }, \
 	.thread = NULL, \
 	.wait_q = NULL, \
-	.delta_ticks_from_prev = -1, \
+	.delta_ticks_from_prev = _INACTIVE, \
 	},
 #else
 #define _THREAD_TIMEOUT_INIT(obj)
@@ -695,7 +701,7 @@
 
 #define K_TIMER_INITIALIZER(obj, expiry, stop) \
 	{ \
-	.timeout.delta_ticks_from_prev = -1, \
+	.timeout.delta_ticks_from_prev = _INACTIVE, \
 	.timeout.wait_q = NULL, \
 	.timeout.thread = NULL, \
 	.timeout.func = _timer_expiration_handler, \
diff --git a/kernel/unified/include/ksched.h b/kernel/unified/include/ksched.h
index 6e9fe90..d7b3578 100644
--- a/kernel/unified/include/ksched.h
+++ b/kernel/unified/include/ksched.h
@@ -268,7 +268,7 @@
 static inline int _is_thread_timeout_active(struct k_thread *thread)
 {
 #ifdef CONFIG_SYS_CLOCK_EXISTS
-	return thread->base.timeout.delta_ticks_from_prev != -1;
+	return thread->base.timeout.delta_ticks_from_prev != _INACTIVE;
 #else
 	return 0;
 #endif
diff --git a/kernel/unified/include/timeout_q.h b/kernel/unified/include/timeout_q.h
index fc73091..2993634 100644
--- a/kernel/unified/include/timeout_q.h
+++ b/kernel/unified/include/timeout_q.h
@@ -38,7 +38,7 @@
 	 * not dealing with timeouts does not have to handle this, such as when
 	 * waiting forever on a semaphore.
 	 */
-	t->delta_ticks_from_prev = -1;
+	t->delta_ticks_from_prev = _INACTIVE;
 
 	/*
 	 * Must be initialized here so that the _fiber_wakeup family of APIs can
@@ -98,7 +98,7 @@
 	struct _timeout *t = (void *)sys_dlist_get(timeout_q);
 	struct k_thread *thread = t->thread;
 
-	t->delta_ticks_from_prev = -1;
+	t->delta_ticks_from_prev = _INACTIVE;
 
 	K_DEBUG("timeout %p\n", t);
 	if (thread != NULL) {
@@ -127,14 +127,13 @@
 	}
 }
 
-/* returns 0 in success and -1 if the timer has expired */
-
+/* returns _INACTIVE if the timer has already expired */
 static inline int _abort_timeout(struct _timeout *t)
 {
 	sys_dlist_t *timeout_q = &_timeout_q;
 
-	if (-1 == t->delta_ticks_from_prev) {
-		return -1;
+	if (t->delta_ticks_from_prev == _INACTIVE) {
+		return _INACTIVE;
 	}
 
 	if (!sys_dlist_is_tail(timeout_q, &t->node)) {
@@ -144,11 +143,12 @@
 		next->delta_ticks_from_prev += t->delta_ticks_from_prev;
 	}
 	sys_dlist_remove(&t->node);
-	t->delta_ticks_from_prev = -1;
+	t->delta_ticks_from_prev = _INACTIVE;
 
 	return 0;
 }
 
+/* returns _INACTIVE if the timer has already expired */
 static inline int _abort_thread_timeout(struct k_thread *thread)
 {
 	return _abort_timeout(&thread->base.timeout);
diff --git a/kernel/unified/sched.c b/kernel/unified/sched.c
index c78be4e..ce999ac 100644
--- a/kernel/unified/sched.c
+++ b/kernel/unified/sched.c
@@ -313,7 +313,7 @@
 		return;
 	}
 
-	if (_abort_thread_timeout(thread) < 0) {
+	if (_abort_thread_timeout(thread) == _INACTIVE) {
 		irq_unlock(key);
 		return;
 	}
diff --git a/kernel/unified/timer.c b/kernel/unified/timer.c
index 000be24..d1315f5 100644
--- a/kernel/unified/timer.c
+++ b/kernel/unified/timer.c
@@ -111,7 +111,7 @@
 
 	unsigned int key = irq_lock();
 
-	if (timer->timeout.delta_ticks_from_prev != -1) {
+	if (timer->timeout.delta_ticks_from_prev != _INACTIVE) {
 		_abort_timeout(&timer->timeout);
 	}
 
@@ -128,11 +128,11 @@
 	__ASSERT(!_is_in_isr(), "");
 
 	int key = irq_lock();
-	int stopped = _abort_timeout(&timer->timeout);
+	int inactive = (_abort_timeout(&timer->timeout) == _INACTIVE);
 
 	irq_unlock(key);
 
-	if (stopped == -1) {
+	if (inactive) {
 		return;
 	}
 
@@ -175,7 +175,7 @@
 	uint32_t result = timer->status;
 
 	if (result == 0) {
-		if (timer->timeout.delta_ticks_from_prev != -1) {
+		if (timer->timeout.delta_ticks_from_prev != _INACTIVE) {
 			/* wait for timer to expire or stop */
 			_pend_current_thread(&timer->wait_q, K_FOREVER);
 			_Swap(key);
@@ -201,7 +201,7 @@
 	unsigned int key = irq_lock();
 	int32_t remaining_ticks;
 
-	if (timeout->delta_ticks_from_prev == -1) {
+	if (timeout->delta_ticks_from_prev == _INACTIVE) {
 		remaining_ticks = 0;
 	} else {
 		/*