glibc 2.21 Linux
Go to nanosleep system call.
glibc is the standard implementation for stdlib on most Linux distributions of Linux.
How to find him: first reflex:
git ls-files | grep sleep
It contains:
sysdeps/unix/sysv/linux/sleep.c
and we know that:
sysdeps/unix/sysv/linux/
contains Linux features.
At the top of this file we see:
unsigned int __sleep (unsigned int seconds)
So, if you trust the comments, we do mostly.
At the bottom:
weak_alias (__sleep, sleep)
which basically says __sleep == sleep . The function uses nanosleep through:
result = __nanosleep (&ts, &ts);
After greppingg:
git grep nanosleep | grep -v abilist
we get a small list of interesting entries, and I think __nanosleep is defined in:
sysdeps/unix/sysv/linux/syscalls.list
in line:
nanosleep - nanosleep Ci:pp __nanosleep nanosleep
which is some supermaster DRY format processed by:
sysdeps/unix/make-syscalls.sh
Then from the build directory:
grep -r __nanosleep
Leads us to: /sysd-syscalls , which is what make-syscalls.sh generates and contains:
#### CALL=nanosleep NUMBER=35 ARGS=i:pp SOURCE=- ifeq (,$(filter nanosleep,$(unix-syscalls))) unix-syscalls += nanosleep $(foreach p,$(sysd-rules-targets),$(foreach o,$(object-suffixes),$(objpfx)$(patsubst %,$p,nanosleep)$o)): \ $(..)sysdeps/unix/make-syscalls.sh $(make-target-directory) (echo '#define SYSCALL_NAME nanosleep'; \ echo '#define SYSCALL_NARGS 2'; \ echo '#define SYSCALL_SYMBOL __nanosleep'; \ echo '#define SYSCALL_CANCELLABLE 1'; \ echo '#include <syscall-template.S>'; \ echo 'weak_alias (__nanosleep, nanosleep)'; \ echo 'libc_hidden_weak (nanosleep)'; \ ) | $(compile-syscall) $(foreach p,$(patsubst %nanosleep,%,$(basename $(@F))),$($(p)CPPFLAGS)) endif
It looks like part of the Makefile. git grep sysd-syscalls shows that it is enabled:
sysdeps/unix/Makefile:23:-include $(common-objpfx)sysd-syscalls
compile-syscall looks like a key part, so we find:
Note that -x assembler-with-cpp is a gcc option.
#define options, such as:
#define SYSCALL_NAME nanosleep
and then use them at:
#include <syscall-template.S>
Well, this is until I continue the game to expand the macro.
I think then it generates a posix/nanosleep.o file that should be connected with everything.
Linux 4.2 x86_64 nanosleep syscall
Uses the scheduler: this is not a busy dream.
Search ctags:
sys_nanosleep
Leads us to kernel/time/hrtimer.c :
SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
hrtimer stands for High Resolution Timer. From there, the main line looks like this:
hrtimer_nanosleepdo_nanosleepset_current_state(TASK_INTERRUPTIBLE); which is intermittent sleepfreezable_schedule(); that calls schedule() and allows you to start other processes
hrtimer_start_expireshrtimer_start_range_ns- TODO: reach
arch/x86 time level
A few articles about this: