This patch implements clock_adjtime() with CLOCKFD support, enabling
precise time and frequency adjustments for PTP clocks through the
standard POSIX clock API.
Key changes include:
1. New clock_adjtime() system call:
- Added sched/clock/clock_adjtime.c implementation
- Provides POSIX-compliant time adjustment interface
- Supports both standard clocks and CLOCKFD-based dynamic clocks
- Added CONFIG_CLOCK_ADJTIME Kconfig option
2. CLOCKFD support in clock_adjtime():
- Detects CLOCKFD-encoded clockids via CLOCKFD_TO_FD() check
- Extracts file descriptor and validates through fs_getfilep()
- Issues PTP_CLOCK_ADJTIME ioctl to underlying PTP clock device
- Returns clock status and adjustment results via struct timex
3. Enhanced struct timex support:
- Extended include/sys/timex.h with additional ADJ_* flags
- Added ADJ_OFFSET, ADJ_FREQUENCY, ADJ_MAXERROR, ADJ_ESTERROR
- Added ADJ_STATUS, ADJ_TIMECONST, ADJ_TAI, ADJ_SETOFFSET
- Added ADJ_MICRO, ADJ_NANO for time unit selection
- Added STA_* status flags for clock state reporting
4. Clock operations supported:
- ADJ_SETOFFSET: Apply time offset adjustment
- ADJ_FREQUENCY: Adjust clock frequency (PPM)
- ADJ_MAXERROR: Set maximum error estimate
- ADJ_ESTERROR: Set estimated error
- ADJ_STATUS: Modify clock status bits
- ADJ_NANO: Use nanosecond resolution
- ADJ_SETOFFSET: Set absolute time offset
5. API declarations:
- Added clock_adjtime() prototype in include/nuttx/clock.h
- Enabled by CONFIG_CLOCK_ADJTIME configuration
- Compatible with Linux clock_adjtime() interface
Usage example:
int fd = open("/dev/ptp0", O_RDWR);
struct timex tx = {0};
tx.modes = ADJ_FREQUENCY;
tx.freq = 10000000; /* Adjust frequency by 10 PPM */
clock_adjtime(CLOCKFD(fd), &tx);
close(fd);
This completes the PTP clock framework's POSIX clock API integration,
providing comprehensive time control capabilities for precision timing
applications including PTP synchronization daemons (ptp4l, timemaster).
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This patch extends CLOCKFD support to clock_getres(), allowing
applications to query the resolution of PTP clocks through file
descriptors using the standard POSIX clock API.
Key changes include:
1. Move clock_getres() from libc to kernel:
- Relocated from libs/libc/sched/clock_getres.c to sched/clock/
- Enables direct kernel-level file descriptor handling
- Maintains POSIX compliance while adding CLOCKFD support
2. CLOCKFD support in clock_getres():
- Detects CLOCKFD-encoded clockids using CLOCKFD_TO_FD() check
- Extracts file descriptor from clockid parameter
- Validates file descriptor through fs_getfilep()
- Issues PTP_CLOCK_GETRES ioctl to query clock resolution
3. PTP clock resolution query:
- Retrieves resolution from PTP clock device via ioctl
- Returns nanosecond-precision timing resolution
- Enables applications to determine clock accuracy capabilities
- Falls back to standard clock resolution for non-CLOCKFD clockids
4. Error handling:
- Returns EINVAL for invalid CLOCKFD file descriptors
- Properly manages file reference counting with fs_putfilep()
- Maintains backward compatibility with existing clock types
5. Build system updates:
- Updated libs/libc/sched/CMakeLists.txt and Make.defs
- Updated sched/clock/CMakeLists.txt to include clock_getres.c
- Ensures proper compilation in kernel context
Usage example:
int fd = open("/dev/ptp0", O_RDONLY);
struct timespec res;
clock_getres(CLOCKFD(fd), &res); /* Query PTP clock resolution */
printf("Resolution: %ld.%09ld sec\n", res.tv_sec, res.tv_nsec);
close(fd);
This completes the basic POSIX clock API support for dynamic PTP clocks,
providing applications with comprehensive clock information access.
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This patch implements POSIX-compliant CLOCKFD support, enabling user
applications to access dynamic PTP clocks through file descriptors
combined with clock_gettime() and clock_settime() system calls.
Key changes include:
1. CLOCKFD macro support:
- Added CLOCKFD() macro in include/nuttx/clock.h
- Allows encoding file descriptor into clockid_t: CLOCKFD(fd)
- Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts)
2. clock_gettime() enhancements:
- Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids
- Extracts file descriptor from clockid using CLOCKFD_TO_FD()
- Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME
- Falls back to standard clock handling for non-CLOCKFD clockids
3. clock_settime() enhancements:
- Modified sched/clock/clock_settime.c to support CLOCKFD clockids
- Extracts file descriptor and validates accessibility
- Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device
- Maintains backward compatibility with standard POSIX clocks
4. File descriptor validation:
- Checks file descriptor validity using fs_getfilep()
- Ensures proper reference counting with fs_putfilep()
- Returns appropriate error codes (EINVAL, EBADF) on failures
5. Integration with PTP clock framework:
- Seamlessly integrates with PTP clock driver's ioctl interface
- Enables standard POSIX clock API usage for dynamic PTP clocks
- No changes required to existing applications using standard clocks
Usage example:
int fd = open("/dev/ptp0", O_RDONLY);
struct timespec ts;
clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */
clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */
close(fd);
This implementation follows the Linux kernel's PTP clock model, providing
a familiar interface for developers working with PTP hardware.
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
If the wdog use the fine-grained spin-lock, and allow the callback execution without the lock held, there will be incorrect interleaving.
E.g. The first `nxsem_timeout` callback function caused the second semaphore wait to fail.
Core 0 [nxsem_clockwait] | Core 1
enter_critical_section() | ...
wd_start(nxsem_timeout) | ...
nxsem_wait(sem) | wd_expiration() --> nxsem_timeout
wd_cancel(&rtcb->waitdog) | try enter_critical_section()
leave_critical_section() | Failed retry...
....nxsem_clockwait | Failed retry...
enter_critical_section() | Failed retry...
wd_start(nxsem_timeout) | Failed retry...
nxsem_wait(sem) | Core 1 enter the critical section
| nxsem_wait_irq(wtcb, ETIMEDOUT) -> incorrectly wake-up the rtcb.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit reduced 1 write operation for wdog deleting and decoupled
the WDOG_ISACTIVE with the list implementation.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit moved the g_wdtimernested to sched_timerexpiration, since
wdog and hrtimer can share it.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit changed the return value semantics.
In past designs, timers that were expected to fire immediately were delayed by at least one tick, resulting in a decrease in real-time performance.
This commit re-encoded the return value to allow the timers fire
immediately.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This reverts commit 29e50ffa73.
reason:
Placing the main thread and the gourd in the same memory block, and allocating and freeing memory simultaneously, presents the following two problems:
When the main thread creates a child thread and performs a detach operation, there is a possibility that the main thread may have exited, but the main thread's TCB (Transaction Control Block) may not have been released.
This could potentially cause the main thread's TCB to be double-freed. The core contradiction in this problem lies in binding the main thread's TCB (Trust Container Registry) and the group together. When releasing the main thread's TCB, an additional check is needed to ensure the main thread was the last to leave the group. If this check and the free operation are atomically guaranteed, the logic is sound, and double freeing won't occur. However, this atomicity cannot be completely guaranteed. If other free operations cause a block, problems still arise.
Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
Add a safe synchronous hrtimer cancel API, hrtimer_cancel_sync().
If the timer callback is currently executing, this function waits
until the callback has completed and the timer state transitions
to HRTIMER_STATE_INACTIVE.
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
NuttX provides the wdog module to implement timers for the scheduler.
While it is lightweight and efficient, wdog only supports tick-level timers,
typically in milliseconds. Although the tick duration can be configured,
setting it to microsecond or nanosecond resolution is not practical.
Doing so may cause an interrupt storm, where the CPU is constantly
occupied handling tick interrupts.
To address this limitation, a new hrtimer module is introduced.
It coexists with the wdog module, providing both tick-level timers
for wdog and high-resolution timers (nanosecond-level) directly to users.
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
Remove unnecessary #ifdef CONFIG_DISABLE_ENVIRON.
This makes sure environment resources are always cleaned up,
regardless of configuration.
Signed-off-by: ligd <liguiding1@xiaomi.com>
The current implementation only calls
clock_inittime() when RTC is enabled,
which causes it to be skipped on other
architectures. This patch ensures
clock_inittime() is invoked during clock
initialization for all cases.
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
As pull request apache#17200 & apache#17368 introduced support for scheduling sleep, a documentation is needed for different sleep interfaces.
This patch adds the description for sleep interfaces currently provided in NuttX, including Scheduled sleep(nxsched_sleep()...), Signal-scheduled sleep(nxsig_sleep()...), and Busy sleep(up_udelay()).
Signed-off-by: Haokun Dong <donghaokun@lixiang.com>
This reverts the removal of ndelay_accurate from #14450, since as
mentioned in #17011, this fails to consider the `sim` architecture
where CONFIG_BOARD_LOOPSPERMSEC was set to 0 because of reliance on the
accurate implementations of the up_delay functions. All the commit did
was remove a more accurate implementation in favour of a less accurate
one.
Signed-off-by: Matteo Golin <matteo.golin@gmail.com>
nxsig_ismember() has a return type of int, but the current
implementation returns a boolean value, which is incorrect.
All callers should determine membership by checking whether
the return value is 1 or 0, which is also consistent with the POSIX sigismember() API.
Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
Initialize the signal action pool during the signal initialization
phase to improve performance and reduce footprint.
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
Introduce the nxched_nanosleep() API to provide a lightweight
nanosecond-level sleep based on nxsched_ticksleep().
This API offers the same functionality as nxsig_nanosleep() but without
signal-related overhead, making it suitable for implementing libc
sleep() or usleep() when signals are disabled.
Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
clock_compare() should be used when comparing clock tick values to
ensure correct handling of tick wrap-around and time ordering.
Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
Both the watchdog timeout and signal dispatch paths already cancel
the watchdog timer, so the explicit wd_cancel() call is redundant.
Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
Always compute the expected wake-up time by default, so the remaining
time can be calculated correctly when the flag is not TIMER_ABSTIME.
Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
Events are not defined in the POSIX standard, so signals are not required
to wake tasks that are in the TSTATE_WAIT_EVENT state.
Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
If the task is blocked waiting for a event, then that task must
be unblocked when a signal is received.
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
Make g_system_ticks a static variable, as it is only accessed by
clock_get_sched_ticks().
This change improves code modularity and enhances safety, since
clock_get_sched_ticks() performs proper synchronization when
reading g_system_ticks.
Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
nxsched_deliver_task() or nxsched_merge_pending() should only be
called when a context switch is required. This behavior is
independent of whether the current task is locked.
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>