Skip to content

Commit 2341bf9

Browse files
youvedeep-singhAnas Nashif
authored and
Anas Nashif
committed
kernel: posix: reorganize posix internal function.
calculate_timeout function calcualtes timeout in msecs from timespec. It is used multiple place inside posix code. So moving it under pthead_common.c file. Signed-off-by: Youvedeep Singh <[email protected]>
1 parent 5e6c2c3 commit 2341bf9

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

kernel/posix/pthread_common.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <pthread.h>
99
#include "ksched.h"
1010
#include "wait_q.h"
11+
#include "time.h"
1112

1213
void ready_one_thread(_wait_q_t *wq)
1314
{
@@ -18,3 +19,26 @@ void ready_one_thread(_wait_q_t *wq)
1819
_ready_thread(th);
1920
}
2021
}
22+
23+
s64_t timespec_to_timeoutms(const struct timespec *abstime)
24+
{
25+
s64_t milli_secs;
26+
s32_t secs, nsecs;
27+
struct timespec curtime;
28+
29+
/* FIXME: Zephyr does have CLOCK_REALTIME to get time.
30+
* As per POSIX standard time should be calculated wrt CLOCK_REALTIME.
31+
* Zephyr deviates from POSIX 1003.1 standard on this aspect.
32+
*/
33+
clock_gettime(CLOCK_MONOTONIC, &curtime);
34+
secs = abstime->tv_sec - curtime.tv_sec;
35+
nsecs = abstime->tv_nsec - curtime.tv_nsec;
36+
37+
if (secs < 0 || (secs == 0 && nsecs < NSEC_PER_MSEC)) {
38+
milli_secs = K_NO_WAIT;
39+
} else {
40+
milli_secs = secs * MSEC_PER_SEC + nsecs / NSEC_PER_MSEC;
41+
}
42+
43+
return milli_secs;
44+
}

kernel/posix/pthread_rwlock.c

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#define CONCURRENT_READER_LIMIT (CONFIG_MAX_PTHREAD_COUNT + 1)
1515

16-
static s64_t calculate_timeout(const struct timespec *abstime);
16+
s64_t timespec_to_timeoutms(const struct timespec *abstime);
1717
static u32_t read_lock_acquire(pthread_rwlock_t *rwlock, s32_t timeout);
1818
static u32_t write_lock_acquire(pthread_rwlock_t *rwlock, s32_t timeout);
1919

@@ -93,7 +93,7 @@ int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
9393
return EINVAL;
9494
}
9595

96-
timeout = (s32_t) calculate_timeout(abstime);
96+
timeout = (s32_t) timespec_to_timeoutms(abstime);
9797

9898
if (read_lock_acquire(rwlock, timeout) != 0) {
9999
ret = ETIMEDOUT;
@@ -155,7 +155,7 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
155155
return EINVAL;
156156
}
157157

158-
timeout = (s32_t) calculate_timeout(abstime);
158+
timeout = (s32_t) timespec_to_timeoutms(abstime);
159159

160160
if (write_lock_acquire(rwlock, timeout) != 0) {
161161
ret = ETIMEDOUT;
@@ -254,34 +254,4 @@ static u32_t write_lock_acquire(pthread_rwlock_t *rwlock, s32_t timeout)
254254
return ret;
255255
}
256256

257-
static s64_t calculate_timeout(const struct timespec *abstime)
258-
{
259-
s64_t milli_secs;
260-
s32_t secs;
261-
struct timespec curtime;
262-
263-
/* FIXME: Zephyr does have CLOCK_REALTIME to get time.
264-
* As per POSIX standard time should be calculated wrt CLOCK_REALTIME.
265-
* Zephyr deviates from POSIX 1003.1 standard on this aspect.
266-
*/
267-
clock_gettime(CLOCK_MONOTONIC, &curtime);
268-
secs = abstime->tv_sec - curtime.tv_sec;
269-
270-
if (abstime->tv_sec < curtime.tv_sec ||
271-
(secs == 0 && abstime->tv_nsec <= curtime.tv_nsec)) {
272-
milli_secs = K_NO_WAIT;
273-
} else {
274-
milli_secs = (abstime->tv_nsec - curtime.tv_nsec) /
275-
NSEC_PER_MSEC;
276-
277-
if (milli_secs < 0) {
278-
milli_secs += MSEC_PER_SEC;
279-
secs -= 1;
280-
}
281-
282-
milli_secs += (long)secs * MSEC_PER_SEC;
283-
}
284-
285-
return milli_secs;
286-
}
287257

0 commit comments

Comments
 (0)