lib: posix: Update to new timeout API

Mostly trivial search-and-replace, except for pthread_rwlock.c, where
we need spread timeout over 2 semaphore operations.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
diff --git a/lib/posix/Kconfig b/lib/posix/Kconfig
index 95c3a77..b1a0d52 100644
--- a/lib/posix/Kconfig
+++ b/lib/posix/Kconfig
@@ -12,7 +12,6 @@
 config POSIX_API
 	depends on !ARCH_POSIX
 	bool "POSIX APIs"
-	select LEGACY_TIMEOUT_API
 	help
 	  Enable mostly-standards-compliant implementations of
 	  various POSIX (IEEE 1003.1) APIs.
diff --git a/lib/posix/mqueue.c b/lib/posix/mqueue.c
index 47bee1b..a5728e0 100644
--- a/lib/posix/mqueue.c
+++ b/lib/posix/mqueue.c
@@ -33,9 +33,9 @@
 s64_t timespec_to_timeoutms(const struct timespec *abstime);
 static mqueue_object *find_in_list(const char *name);
 static s32_t send_message(mqueue_desc *mqd, const char *msg_ptr, size_t msg_len,
-			  s32_t timeout);
+			  k_timeout_t timeout);
 static int receive_message(mqueue_desc *mqd, char *msg_ptr, size_t msg_len,
-			   s32_t timeout);
+			   k_timeout_t timeout);
 static void remove_mq(mqueue_object *msg_queue);
 
 /**
@@ -233,9 +233,8 @@
 	    unsigned int msg_prio)
 {
 	mqueue_desc *mqd = (mqueue_desc *)mqdes;
-	s32_t  timeout = K_FOREVER;
 
-	return send_message(mqd, msg_ptr, msg_len, timeout);
+	return send_message(mqd, msg_ptr, msg_len, K_FOREVER);
 }
 
 /**
@@ -249,10 +248,9 @@
 		 unsigned int msg_prio, const struct timespec *abstime)
 {
 	mqueue_desc *mqd = (mqueue_desc *)mqdes;
-	s32_t  timeout;
+	s32_t timeout = (s32_t) timespec_to_timeoutms(abstime);
 
-	timeout = (s32_t) timespec_to_timeoutms(abstime);
-	return send_message(mqd, msg_ptr, msg_len, timeout);
+	return send_message(mqd, msg_ptr, msg_len, K_MSEC(timeout));
 }
 
 /**
@@ -266,10 +264,8 @@
 		   unsigned int *msg_prio)
 {
 	mqueue_desc *mqd = (mqueue_desc *)mqdes;
-	s32_t  timeout = K_FOREVER;
 
-	return receive_message(mqd, msg_ptr, msg_len, timeout);
-
+	return receive_message(mqd, msg_ptr, msg_len, K_FOREVER);
 }
 
 /**
@@ -283,10 +279,9 @@
 			unsigned int *msg_prio, const struct timespec *abstime)
 {
 	mqueue_desc *mqd = (mqueue_desc *)mqdes;
-	s32_t  timeout = K_NO_WAIT;
+	s32_t timeout = (s32_t) timespec_to_timeoutms(abstime);
 
-	timeout = (s32_t) timespec_to_timeoutms(abstime);
-	return receive_message(mqd, msg_ptr, msg_len, timeout);
+	return receive_message(mqd, msg_ptr, msg_len, K_MSEC(timeout));
 }
 
 /**
@@ -366,7 +361,7 @@
 }
 
 static s32_t send_message(mqueue_desc *mqd, const char *msg_ptr, size_t msg_len,
-			  s32_t timeout)
+			  k_timeout_t timeout)
 {
 	s32_t ret = -1;
 
@@ -385,7 +380,7 @@
 	}
 
 	if (k_msgq_put(&mqd->mqueue->queue, (void *)msg_ptr, timeout) != 0) {
-		errno = (timeout == K_NO_WAIT) ?   EAGAIN : ETIMEDOUT;
+		errno = K_TIMEOUT_EQ(timeout, K_NO_WAIT) ? EAGAIN : ETIMEDOUT;
 		return ret;
 	}
 
@@ -393,7 +388,7 @@
 }
 
 static s32_t receive_message(mqueue_desc *mqd, char *msg_ptr, size_t msg_len,
-			     s32_t timeout)
+			     k_timeout_t timeout)
 {
 	int ret = -1;
 
@@ -412,7 +407,7 @@
 	}
 
 	if (k_msgq_get(&mqd->mqueue->queue, (void *)msg_ptr, timeout) != 0) {
-		errno = (timeout != K_NO_WAIT) ? ETIMEDOUT : EAGAIN;
+		errno = K_TIMEOUT_EQ(timeout, K_NO_WAIT) ? EAGAIN : ETIMEDOUT;
 	} else {
 		ret = mqd->mqueue->queue.msg_size;
 	}
diff --git a/lib/posix/pthread.c b/lib/posix/pthread.c
index ca4ed4a..a1437b6 100644
--- a/lib/posix/pthread.c
+++ b/lib/posix/pthread.c
@@ -24,7 +24,7 @@
 	.stack = NULL,
 	.stacksize = 0,
 	.flags = PTHREAD_INIT_FLAGS,
-	.delayedstart = K_NO_WAIT,
+	.delayedstart = 0,
 #if defined(CONFIG_PREEMPT_ENABLED)
 	.schedpolicy = SCHED_RR,
 #else
@@ -190,7 +190,7 @@
 						 (void *)arg, NULL,
 						 threadroutine, prio,
 						 (~K_ESSENTIAL & attr->flags),
-						 attr->delayedstart);
+						 K_MSEC(attr->delayedstart));
 	return 0;
 }
 
diff --git a/lib/posix/pthread_cond.c b/lib/posix/pthread_cond.c
index 39d38dd..ff713a6 100644
--- a/lib/posix/pthread_cond.c
+++ b/lib/posix/pthread_cond.c
@@ -11,7 +11,8 @@
 
 s64_t timespec_to_timeoutms(const struct timespec *abstime);
 
-static int cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut, int timeout)
+static int cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut,
+		     k_timeout_t timeout)
 {
 	__ASSERT(mut->lock_count == 1U, "");
 
@@ -78,5 +79,5 @@
 			   const struct timespec *abstime)
 {
 	s32_t timeout = (s32_t)timespec_to_timeoutms(abstime);
-	return cond_wait(cv, mut, timeout);
+	return cond_wait(cv, mut, K_MSEC(timeout));
 }
diff --git a/lib/posix/pthread_mutex.c b/lib/posix/pthread_mutex.c
index 1f0b374..fe01f94 100644
--- a/lib/posix/pthread_mutex.c
+++ b/lib/posix/pthread_mutex.c
@@ -20,7 +20,7 @@
 	.type = PTHREAD_MUTEX_DEFAULT,
 };
 
-static int acquire_mutex(pthread_mutex_t *m, int timeout)
+static int acquire_mutex(pthread_mutex_t *m, k_timeout_t timeout)
 {
 	int rc = 0, key = irq_lock();
 
@@ -45,7 +45,7 @@
 		return rc;
 	}
 
-	if (timeout == K_NO_WAIT) {
+	if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
 		irq_unlock(key);
 		return EINVAL;
 	}
@@ -78,7 +78,7 @@
 			    const struct timespec *abstime)
 {
 	s32_t timeout = (s32_t)timespec_to_timeoutms(abstime);
-	return acquire_mutex(m, timeout);
+	return acquire_mutex(m, K_MSEC(timeout));
 }
 
 /**
diff --git a/lib/posix/pthread_rwlock.c b/lib/posix/pthread_rwlock.c
index 0f85d5f..24ef2ca 100644
--- a/lib/posix/pthread_rwlock.c
+++ b/lib/posix/pthread_rwlock.c
@@ -71,7 +71,7 @@
 		return EINVAL;
 	}
 
-	return read_lock_acquire(rwlock, K_FOREVER);
+	return read_lock_acquire(rwlock, SYS_FOREVER_MS);
 }
 
 /**
@@ -116,7 +116,7 @@
 		return EINVAL;
 	}
 
-	return read_lock_acquire(rwlock, K_NO_WAIT);
+	return read_lock_acquire(rwlock, 0);
 }
 
 /**
@@ -133,7 +133,7 @@
 		return EINVAL;
 	}
 
-	return write_lock_acquire(rwlock, K_FOREVER);
+	return write_lock_acquire(rwlock, SYS_FOREVER_MS);
 }
 
 /**
@@ -178,7 +178,7 @@
 		return EINVAL;
 	}
 
-	return write_lock_acquire(rwlock, K_NO_WAIT);
+	return write_lock_acquire(rwlock, 0);
 }
 
 /**
@@ -216,7 +216,7 @@
 {
 	u32_t ret = 0U;
 
-	if (k_sem_take(&rwlock->wr_sem, timeout) == 0) {
+	if (k_sem_take(&rwlock->wr_sem, SYS_TIMEOUT_MS(timeout)) == 0) {
 		k_sem_take(&rwlock->reader_active, K_NO_WAIT);
 		k_sem_take(&rwlock->rd_sem, K_NO_WAIT);
 		k_sem_give(&rwlock->wr_sem);
@@ -231,17 +231,23 @@
 {
 	u32_t ret = 0U;
 	s64_t elapsed_time, st_time = k_uptime_get();
+	k_timeout_t k_timeout;
+
+	k_timeout = SYS_TIMEOUT_MS(timeout);
 
 	/* waiting for release of write lock */
-	if (k_sem_take(&rwlock->wr_sem, timeout) == 0) {
-		if (timeout > K_NO_WAIT) {
+	if (k_sem_take(&rwlock->wr_sem, k_timeout) == 0) {
+		/* update remaining timeout time for 2nd sem */
+		if (timeout != SYS_FOREVER_MS) {
 			elapsed_time = k_uptime_get() - st_time;
-			timeout = timeout <= elapsed_time ? K_NO_WAIT :
+			timeout = timeout <= elapsed_time ? 0 :
 				  timeout - elapsed_time;
 		}
 
+		k_timeout = SYS_TIMEOUT_MS(timeout);
+
 		/* waiting for reader to complete operation */
-		if (k_sem_take(&rwlock->reader_active, timeout) == 0) {
+		if (k_sem_take(&rwlock->reader_active, k_timeout) == 0) {
 			rwlock->wr_owner = k_current_get();
 		} else {
 			k_sem_give(&rwlock->wr_sem);
diff --git a/lib/posix/semaphore.c b/lib/posix/semaphore.c
index d3413e3..7823b14 100644
--- a/lib/posix/semaphore.c
+++ b/lib/posix/semaphore.c
@@ -104,12 +104,12 @@
 	abstime_ms = (s64_t)_ts_to_ms(abstime);
 
 	if (abstime_ms <= current_ms) {
-		timeout = K_NO_WAIT;
+		timeout = 0;
 	} else {
 		timeout = (s32_t)(abstime_ms - current_ms);
 	}
 
-	if (k_sem_take(semaphore, timeout)) {
+	if (k_sem_take(semaphore, K_MSEC(timeout))) {
 		errno = ETIMEDOUT;
 		return -1;
 	}