Add a function timerfd_tracker::enter_critical_section_cancelable,
which is like enter_critical_section but honors a cancel event. Call
this when a timer expires while the timerfd thread is in its inner
loop. This avoids a deadlock if timerfd_tracker::dtor has entered its
critical section and is trying to cancel the thread. See
https://cygwin.com/ml/cygwin/2019-06/msg00096.html.
A sleep is required on Windows 10 64 bit only before calling
RegisterClassW in the timerfd thread, and only when running
under strace. One of the child processes inheriting the timerfd
descriptor will get a STATUS_FLOAT_INEXACT_RESULT exception inside
of msvcrt.dll. It's apparently some timing problem. It occurs
in 4 out of 5 runs under strace only. WOW64 and Windows 7 64 bit
don't have this problem.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
The share section was created using the PAGE_SIZE constant,
but PAGE_SIZE is 64K. Fix that by using wincap.page_size()
instead, which returns the desired actual page size of 4K.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
timerfd_tracker and timerfd_shared classes:
- Just because handles are shared, we don't have to store them in
shared memory. Move share handles into timerfd_tracker class.
- Drop shared instance counter since it's not required anymore.
timerfd_shared only stores the actual timer data.
- Drop timerfd_shared::create, just set clock id.
- Drop timerfd_shared::dtor, it's not required anymore.
- Drop timerfd_tracker::close, just call dtor where required.
- Rename timerfd_tracker::increment_instances to timerfd_tracker::dup.
It's the only reason it exists...
- timerfd_tracker::dtor now checks the non-shared pointers for NULL
before attempting to close them.
- timerfd_tracker::dtor handles decrementing the local instance count
by itself.
- Add a method timerfd_tracker::init_fixup_after_fork_exec to set
non-shared pointers to NULL. Together with the dtor patches it
fixes a problem with close_on_exec timerfd descriptors.
- Fix a bug in handling the thread synchronization event. It's
actually nice to create it before using it...
- Drop using sec_none{_nih} in InitializeObjectAttributes. It's
an unnecessary roundabout route just to get a NULL pointer.
- Slightly rework timechange window handling.
- Add more comments to explain what happens.
fhandler_timerfd:
- Drop cnew macro, it just hides what happens.
- fhandler_timerfd::fixup_after_exec now calls
timerfd_tracker::init_fixup_after_fork_exec first, so a subsequent
call to timerfd_tracker::dtor only works on valid handles.
- fhandler_timerfd::close directly calls timerfd_tracker::dtor now.
- Drop dtor call in fhandler_timerfd destructor.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
timerfd_tracker::fixup_after_fork_exec always tries to restore
the shared timer region at the same address as in the parent.
This is entirely unnecessary and wasn't intended, rather some
kind of copy/paste thinko. Fix that. Print NtMapViewOfSection
status code in api_fatal on failure for debugging.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
- When correcting the next expiration timestamp, the number of
expirations gets computed correctly, just the expiration timestamp
itself is then only incremented by a single interval, rather than
the just computed expired intervals. Fix that.
- drop the local clock variable in timerfd_tracker::create. It doesn't
serve any purpose.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
- split into to __try/__except blocks to make sure
leave_critical_section is always called when required.
- Actually fill time_spec in settime so it_interval is returned
correctly.
- Return all 0 if timer is disarmed.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
The value returned by reading from a timerfd is not an overrun
count in the same sense as for posix timers, it's an expiry counter.
Reflect that in the name.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
- Drop erroneous initial computation of overrun count in settime
for absolute non-realtime clocks. It's repeated in thread_func
and thus counted twice.
- Fix overrun computation for timestamp offsets being a multiple of
the timer interval. The timestamp has to be corrected after the
first offset, otherwise the correction loop counts the intervals
again.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
Non-CLOCK_REALTIME counters always use a relative DueTime in NtSetTimer.
However, relative DueTime has to be negative, but the code
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
- On systems with inexact realtime clock, the current timestamp may
be fractionally smaller than the desired timestamp. This breaks the
computation for incrementing overrun_count so overrun_count may end
up as 0. Expiring the timer with an overrun_count of 0 is a no-go.
Make sure we always increment overrun_count by at least one after
timer expiry.
- Do not expire the timer when another process deletes its timer_tracker.
This, too, may result in a 0 overrun_count.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
Using posix timers "timer_tracker" as base class for timerfd was flawed.
Posix timers are not inherited by child processes and don't survive
execve. The method used by posix timers didn't allow to share timers
between processes. The timers were still per-process timers and worked
entirely separate from each other. Reading from these timers via
different descriptors was only synchronized within the same process.
This does not reflect the timerfd semantics in Linux: The per-file
timers can be dup'ed and survive fork and execve. They are still just
descriptors pointing to the same timer object originally created by
timerfd_create. Synchronization is performed between all descriptor
instances of the same timer, system-wide.
Thus, reimplement timerfd using a timer instance in shared memory,
a kernel timer, and a handful of sync objects.
Every process maintains a per-process timerfd struct on the cygheap
maintaining a per-process thread. Every process sharing the same
timerfd will run this thread checking the state of the timer, similar
to the posix timer thread, just working on the shared objects and
synchronizing its job with each other thread.
Drop the timerfd implementation in the posix timer code and move the
public API to fhandler_timerfd.c. Keep the ttstart timer_tracker
anchor out of "NO_COPY" since the fixup_after_fork code should run to
avoid memory leakage.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>