Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/sys/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static inline int beginthread(void (*start)(void *), unsigned int priority, void
}


extern int threadsinfo(int n, threadinfo_t *info);
extern int threadsinfo(int *tid, unsigned int flags, int n, threadinfo_t *info);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The signature of the extern function threadsinfo has been changed. This is a breaking API change, as any existing code calling this function will fail to compile. The pull request description marks this as a "non-breaking change", which is inaccurate. Please update the PR description to reflect that this is a breaking change.



extern int priority(int priority);
Expand Down
7 changes: 4 additions & 3 deletions include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
#define CLOCKS_PER_SEC 1000000


#define CLOCK_MONOTONIC 0
#define CLOCK_MONOTONIC_RAW 1
#define CLOCK_REALTIME 2
#define CLOCK_MONOTONIC (0U)
#define CLOCK_MONOTONIC_RAW (1U)
#define CLOCK_REALTIME (2U)
#define CLOCK_THREAD_CPUTIME_ID (3U)


#ifdef __cplusplus
Expand Down
13 changes: 11 additions & 2 deletions time/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

#include <sys/time.h>
#include <sys/threads.h>
#include <time.h>
#include <errno.h>
#include <stdlib.h>
Expand Down Expand Up @@ -92,21 +93,29 @@
int clock_gettime(clockid_t clk_id, struct timespec *tp)
{
time_t now, offs;
int tid;
threadinfo_t info;

if (tp == NULL) {
errno = EINVAL;
return -1;
}

if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW) {
if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW && clk_id != CLOCK_THREAD_CPUTIME_ID) {
errno = EINVAL;
return -1;
}
Comment on lines +104 to 107
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This chain of && conditions to validate clk_id is becoming long and can be hard to read. Using a switch statement would be more readable and maintainable, especially if more clock IDs are added in the future.

	switch (clk_id) {
	case CLOCK_REALTIME:
	case CLOCK_MONOTONIC:
	case CLOCK_MONOTONIC_RAW:
	case CLOCK_THREAD_CPUTIME_ID:
		break;
	default:
		errno = EINVAL;
		return -1;
	}


gettime(&now, &offs);

if (clk_id == CLOCK_REALTIME)
if (clk_id == CLOCK_REALTIME) {
now += offs;
}
else if (clk_id == CLOCK_THREAD_CPUTIME_ID) {
tid = gettid();
threadsinfo(&tid, PH_THREADINFO_CPUTIME, 1, &info);

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv8r52-mps3an536-qemu)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (sparcv8leon-gr740-mini)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7a7-imx6ull-evk)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (aarch64a53-zynqmp-qemu)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7m7-imxrt106x-evk)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (ia32-generic-qemu)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7m4-stm32l4x6-nucleo)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (sparcv8leon-gr712rc-board)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7a9-zynq7000-zturn)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7a9-zynq7000-zedboard)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv8m33-mcxn94x-frdm)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7r5f-zynqmp-qemu)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7m7-imxrt105x-evk)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (armv7m7-imxrt117x-evk)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (riscv64-grfpga-artya7)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)

Check failure on line 116 in time/time.c

View workflow job for this annotation

GitHub Actions / call-ci / build (sparcv8leon-generic-qemu)

'PH_THREADINFO_CPUTIME' undeclared (first use in this function)
now = info.cpuTime;
}
Comment on lines 109 to +118
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There are a couple of issues in this block:

  1. The gettime() function is called unconditionally, even for CLOCK_THREAD_CPUTIME_ID where its results are not used. This is inefficient. The call should only be made when needed.
  2. The return value of threadsinfo() is not checked. If it fails, info.cpuTime could contain an uninitialized value, leading to clock_gettime() returning an incorrect time with a success status (EOK). The return value should be checked for errors.

Here's a suggested refactoring that addresses both points:

	if (clk_id == CLOCK_THREAD_CPUTIME_ID) {
		tid = gettid();
		if (threadsinfo(&tid, PH_THREADINFO_CPUTIME, 1, &info) < 0) {
			return -1; /* Assumes threadsinfo sets errno on failure */
		}
		now = info.cpuTime;
	}
	else {
		gettime(&now, &offs);
		if (clk_id == CLOCK_REALTIME) {
			now += offs;
		}
	}


tp->tv_sec = now / (1000 * 1000);
now -= tp->tv_sec * 1000 * 1000;
Expand Down
Loading