tools/testing/selftests/timens/clock_nanosleep.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/timens/clock_nanosleep.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/timens/clock_nanosleep.c- Extension
.c- Size
- 3063 bytes
- Lines
- 152
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
sched.hsys/timerfd.hsys/syscall.htime.hunistd.hstdlib.hstdio.hstdint.hpthread.hsignal.hstring.hlog.htimens.h
Detected Declarations
struct thread_argsfunction test_sigfunction run_testfunction main
Annotated Snippet
struct thread_args {
struct timespec *now, *rem;
pthread_mutex_t *lock;
int clockid;
int abs;
};
void *call_nanosleep(void *_args)
{
struct thread_args *args = _args;
clock_nanosleep(args->clockid, args->abs ? TIMER_ABSTIME : 0, args->now, args->rem);
pthread_mutex_unlock(args->lock);
return NULL;
}
static int run_test(int clockid, int abs)
{
struct timespec now = {}, rem;
struct thread_args args = { .now = &now, .rem = &rem, .clockid = clockid};
struct timespec start;
pthread_mutex_t lock;
pthread_t thread;
int j, ok, ret;
signal(SIGUSR1, test_sig);
signal(SIGUSR2, test_sig);
pthread_mutex_init(&lock, NULL);
pthread_mutex_lock(&lock);
if (clock_gettime(clockid, &start) == -1) {
if (errno == EINVAL && check_skip(clockid))
return 0;
return pr_perror("clock_gettime");
}
if (abs) {
now.tv_sec = start.tv_sec;
now.tv_nsec = start.tv_nsec;
}
now.tv_sec += 3600;
args.abs = abs;
args.lock = &lock;
ret = pthread_create(&thread, NULL, call_nanosleep, &args);
if (ret != 0) {
pr_err("Unable to create a thread: %s", strerror(ret));
return 1;
}
/* Wait when the thread will call clock_nanosleep(). */
ok = 0;
for (j = 0; j < 8; j++) {
/* The maximum timeout is about 5 seconds. */
usleep(10000 << j);
/* Try to interrupt clock_nanosleep(). */
pthread_kill(thread, SIGUSR1);
usleep(10000 << j);
/* Check whether clock_nanosleep() has been interrupted or not. */
if (pthread_mutex_trylock(&lock) == 0) {
/**/
ok = 1;
break;
}
}
if (!ok)
pthread_kill(thread, SIGUSR2);
pthread_join(thread, NULL);
pthread_mutex_destroy(&lock);
if (!ok) {
ksft_test_result_pass("clockid: %d abs:%d timeout\n", clockid, abs);
return 1;
}
if (rem.tv_sec < 3300 || rem.tv_sec > 3900) {
pr_fail("clockid: %d abs: %d remain: %ld\n",
clockid, abs, rem.tv_sec);
return 1;
}
ksft_test_result_pass("clockid: %d abs:%d\n", clockid, abs);
return 0;
}
int main(int argc, char *argv[])
Annotation
- Immediate include surface: `sched.h`, `sys/timerfd.h`, `sys/syscall.h`, `time.h`, `unistd.h`, `stdlib.h`, `stdio.h`, `stdint.h`.
- Detected declarations: `struct thread_args`, `function test_sig`, `function run_test`, `function main`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.