-
Notifications
You must be signed in to change notification settings - Fork 22
time: handle CLOCK_THREAD_CPUTIME_ID #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| */ | ||
|
|
||
| #include <sys/time.h> | ||
| #include <sys/threads.h> | ||
| #include <time.h> | ||
| #include <errno.h> | ||
| #include <stdlib.h> | ||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This chain of 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
|
||
| now = info.cpuTime; | ||
| } | ||
|
Comment on lines
109
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a couple of issues in this block:
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; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The signature of the
externfunctionthreadsinfohas 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.