newlib/winsup/cygwin/timerfd.cc

623 lines
16 KiB
C++
Raw Normal View History

2019-01-19 19:53:48 +01:00
/* timerfd.cc: timerfd helper classes
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include "path.h"
#include "fhandler.h"
#include "pinfo.h"
#include "dtable.h"
#include "cygheap.h"
#include "cygerrno.h"
#include <sys/timerfd.h>
#include "timerfd.h"
#define TFD_CANCEL_FLAGS (TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)
/* Unfortunately MsgWaitForMultipleObjectsEx does not receive WM_TIMECHANGED
messages without a window defined in this process. Create a hidden window
for that purpose. */
void
timerfd_tracker::create_timechange_window ()
{
WNDCLASSW wclass = { 0 };
WCHAR cname[NAME_MAX];
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
__small_swprintf (cname, L"Cygwin.timerfd.%p", this);
wclass.lpfnWndProc = DefWindowProcW;
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
wclass.hInstance = user_data->hmodule;
wclass.lpszClassName = cname;
atom = RegisterClassW (&wclass);
if (!atom)
debug_printf ("RegisterClass %E");
else
{
window = CreateWindowExW (0, cname, cname, WS_POPUP, 0, 0, 0, 0,
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
NULL, NULL, user_data->hmodule, NULL);
if (!window)
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
debug_printf ("CreateWindowEx %E");
}
}
void
timerfd_tracker::delete_timechange_window ()
{
if (window)
DestroyWindow (window);
if (atom)
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
UnregisterClassW ((LPWSTR) (uintptr_t) atom, user_data->hmodule);
}
void
timerfd_tracker::handle_timechange_window ()
{
MSG msg;
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
while (PeekMessageW (&msg, NULL, 0, 0, PM_REMOVE | PM_QS_POSTMESSAGE)
&& msg.message != WM_QUIT)
{
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
DispatchMessageW (&msg);
if (msg.message == WM_TIMECHANGE
&& get_clockid () == CLOCK_REALTIME
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
&& (get_flags () & TFD_CANCEL_FLAGS) == TFD_CANCEL_FLAGS
&& enter_critical_section ())
{
/* make sure to handle each WM_TIMECHANGE only once! */
if (msg.time != tc_time ())
{
set_expiration_count (-1LL);
disarm_timer ();
timer_expired ();
set_tc_time (msg.time);
}
leave_critical_section ();
}
}
}
2019-01-19 19:53:48 +01:00
DWORD
timerfd_tracker::thread_func ()
{
/* Outer loop: Is the timer armed? If not, wait for it. */
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
HANDLE armed[2] = { arm_evt (),
2019-01-19 19:53:48 +01:00
cancel_evt };
create_timechange_window ();
2019-01-19 19:53:48 +01:00
while (1)
{
switch (MsgWaitForMultipleObjectsEx (2, armed, INFINITE, QS_POSTMESSAGE,
MWMO_INPUTAVAILABLE))
2019-01-19 19:53:48 +01:00
{
case WAIT_OBJECT_0:
break;
case WAIT_OBJECT_0 + 1:
goto canceled;
case WAIT_OBJECT_0 + 2:
handle_timechange_window ();
continue;
2019-01-19 19:53:48 +01:00
default:
continue;
}
/* Inner loop: Timer expired? If not, wait for it. */
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
HANDLE expired[3] = { timer (),
disarm_evt (),
2019-01-19 19:53:48 +01:00
cancel_evt };
2019-01-19 19:53:48 +01:00
while (1)
{
switch (MsgWaitForMultipleObjectsEx (3, expired, INFINITE,
QS_POSTMESSAGE,
MWMO_INPUTAVAILABLE))
2019-01-19 19:53:48 +01:00
{
case WAIT_OBJECT_0:
break;
case WAIT_OBJECT_0 + 1:
goto disarmed;
case WAIT_OBJECT_0 + 2:
goto canceled;
case WAIT_OBJECT_0 + 3:
handle_timechange_window ();
continue;
2019-01-19 19:53:48 +01:00
default:
continue;
}
if (!enter_critical_section ())
continue;
/* Make sure we haven't been abandoned and/or disarmed
in the meantime */
if (expiration_count () == -1LL
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
|| IsEventSignalled (disarm_evt ()))
{
leave_critical_section ();
goto disarmed;
}
2019-01-19 19:53:48 +01:00
/* One-shot timer? */
if (!get_interval ())
{
/* Set expiration count, disarm timer */
increment_expiration_count (1);
2019-01-19 19:53:48 +01:00
disarm_timer ();
}
else
{
/* Compute expiration count. */
2019-01-19 19:53:48 +01:00
LONG64 now = get_clock_now ();
LONG64 ts = get_exp_ts ();
LONG64 exp_cnt;
2019-01-19 19:53:48 +01:00
/* Make concessions for unexact realtime clock */
if (ts > now)
ts = now - 1;
exp_cnt = (now - ts + get_interval () - 1) / get_interval ();
increment_expiration_count (exp_cnt);
ts += get_interval () * exp_cnt;
2019-01-19 19:53:48 +01:00
/* Set exp_ts to current timestamp. Make sure exp_ts ends up
bigger than "now" and fix expiration count as required */
while (ts <= (now = get_clock_now ()))
{
exp_cnt = (now - ts + get_interval () - 1) / get_interval ();
increment_expiration_count (exp_cnt);
ts += get_interval () * exp_cnt;
}
2019-01-19 19:53:48 +01:00
set_exp_ts (ts);
/* NtSetTimer allows periods of up to 24 days only. If the time
is longer, we set the timer up as one-shot timer for each
interval. Restart timer here with new due time. */
if (get_interval () > INT_MAX * (NS100PERSEC / MSPERSEC))
{
BOOLEAN Resume = (get_clockid () == CLOCK_REALTIME_ALARM
|| get_clockid () == CLOCK_BOOTTIME_ALARM);
2019-01-19 19:53:48 +01:00
LARGE_INTEGER DueTime = { QuadPart: -get_interval () };
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
NtSetTimer (timer (), &DueTime, NULL, NULL, Resume, 0, NULL);
2019-01-19 19:53:48 +01:00
}
}
2019-01-19 19:53:48 +01:00
/* Arm the expiry object */
timer_expired ();
leave_critical_section ();
}
disarmed:
;
}
canceled:
delete_timechange_window ();
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
/* automatically return the cygthread to the cygthread pool */
_my_tls._ctinfo->auto_release ();
2019-01-19 19:53:48 +01:00
return 0;
}
static DWORD WINAPI
timerfd_thread (VOID *arg)
{
timerfd_tracker *tt = ((timerfd_tracker *) arg);
return tt->thread_func ();
}
int
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
timerfd_tracker::create (clockid_t clock_id)
2019-01-19 19:53:48 +01:00
{
int ret;
NTSTATUS status;
OBJECT_ATTRIBUTES attr;
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
const ACCESS_MASK access = STANDARD_RIGHTS_REQUIRED
| SECTION_MAP_READ | SECTION_MAP_WRITE;
SIZE_T vsize = PAGE_SIZE;
LARGE_INTEGER sectionsize = { QuadPart: PAGE_SIZE };
/* Valid clock? */
if (!get_clock (clock_id))
{
ret = -EINVAL;
goto err;
}
/* Create shared objects */
InitializeObjectAttributes (&attr, NULL, OBJ_INHERIT, NULL, NULL);
/* Create shared section */
status = NtCreateSection (&tfd_shared_hdl, access, &attr, &sectionsize,
PAGE_READWRITE, SEC_COMMIT, NULL);
if (!NT_SUCCESS (status))
{
ret = -geterrno_from_nt_status (status);
goto err;
}
2019-01-19 19:53:48 +01:00
/* Create access mutex */
status = NtCreateMutant (&_access_mtx, MUTEX_ALL_ACCESS, &attr, FALSE);
if (!NT_SUCCESS (status))
{
ret = -geterrno_from_nt_status (status);
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
goto err_close_tfd_shared_hdl;
2019-01-19 19:53:48 +01:00
}
/* Create "timer is armed" event, set to "Unsignaled" at creation time */
status = NtCreateEvent (&_arm_evt, EVENT_ALL_ACCESS, &attr,
NotificationEvent, FALSE);
if (!NT_SUCCESS (status))
{
ret = -geterrno_from_nt_status (status);
goto err_close_access_mtx;
}
/* Create "timer is disarmed" event, set to "Signaled" at creation time */
status = NtCreateEvent (&_disarm_evt, EVENT_ALL_ACCESS, &attr,
NotificationEvent, TRUE);
if (!NT_SUCCESS (status))
{
ret = -geterrno_from_nt_status (status);
goto err_close_arm_evt;
}
/* Create timer */
status = NtCreateTimer (&_timer, TIMER_ALL_ACCESS, &attr,
SynchronizationTimer);
if (!NT_SUCCESS (status))
{
ret = -geterrno_from_nt_status (status);
goto err_close_disarm_evt;
}
/* Create "timer expired" semaphore */
status = NtCreateEvent (&_expired_evt, EVENT_ALL_ACCESS, &attr,
NotificationEvent, FALSE);
if (!NT_SUCCESS (status))
{
ret = -geterrno_from_nt_status (status);
goto err_close_timer;
}
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
/* Create process-local cancel event for this processes timer thread
(has to be recreated after fork/exec)*/
InitializeObjectAttributes (&attr, NULL, 0, NULL, NULL);
status = NtCreateEvent (&cancel_evt, EVENT_ALL_ACCESS, &attr,
NotificationEvent, FALSE);
2019-01-19 19:53:48 +01:00
if (!NT_SUCCESS (status))
{
ret = -geterrno_from_nt_status (status);
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
goto err_close_expired_evt;
2019-01-19 19:53:48 +01:00
}
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
/* Create sync event for this processes timer thread */
status = NtCreateEvent (&sync_thr, EVENT_ALL_ACCESS, &attr,
NotificationEvent, FALSE);
2019-01-19 19:53:48 +01:00
if (!NT_SUCCESS (status))
{
ret = -geterrno_from_nt_status (status);
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
goto err_close_cancel_evt;
2019-01-19 19:53:48 +01:00
}
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
/* Create section mapping (has to be recreated after fork/exec) */
tfd_shared = NULL;
status = NtMapViewOfSection (tfd_shared_hdl, NtCurrentProcess (),
(void **) &tfd_shared, 0, PAGE_SIZE, NULL,
&vsize, ViewShare, 0, PAGE_READWRITE);
2019-01-19 19:53:48 +01:00
if (!NT_SUCCESS (status))
{
ret = -geterrno_from_nt_status (status);
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
goto err_close_sync_thr;
2019-01-19 19:53:48 +01:00
}
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
/* Initialize clock id */
set_clockid (clock_id);
/* Set our winpid for fixup_after_fork_exec */
2019-01-19 19:53:48 +01:00
winpid = GetCurrentProcessId ();
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
/* Start timerfd thread */
2019-01-19 19:53:48 +01:00
new cygthread (timerfd_thread, this, "timerfd", sync_thr);
return 0;
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
err_close_sync_thr:
NtClose (sync_thr);
2019-01-19 19:53:48 +01:00
err_close_cancel_evt:
NtClose (cancel_evt);
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
err_close_expired_evt:
NtClose (_expired_evt);
err_close_timer:
NtClose (_timer);
err_close_disarm_evt:
NtClose (_disarm_evt);
err_close_arm_evt:
NtClose (_arm_evt);
err_close_access_mtx:
NtClose (_access_mtx);
2019-01-19 19:53:48 +01:00
err_close_tfd_shared_hdl:
NtClose (tfd_shared_hdl);
err:
return ret;
}
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
/* Return true if this was the last instance of a timerfd, process-wide,
2019-01-19 19:53:48 +01:00
false otherwise. Basically this is a destructor, but one which may
notify the caller NOT to deleted the object. */
bool
timerfd_tracker::dtor ()
{
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
if (!enter_critical_section ())
return false;
if (decrement_instances () > 0)
{
leave_critical_section ();
return false;
}
if (cancel_evt)
SetEvent (cancel_evt);
if (sync_thr)
2019-01-19 19:53:48 +01:00
{
WaitForSingleObject (sync_thr, INFINITE);
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
NtClose (sync_thr);
2019-01-19 19:53:48 +01:00
}
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
leave_critical_section ();
if (tfd_shared)
NtUnmapViewOfSection (NtCurrentProcess (), tfd_shared);
if (cancel_evt)
NtClose (cancel_evt);
NtClose (tfd_shared_hdl);
NtClose (_expired_evt);
NtClose (_timer);
NtClose (_disarm_evt);
NtClose (_arm_evt);
NtClose (_access_mtx);
2019-01-19 19:53:48 +01:00
return true;
}
void
timerfd_tracker::dtor (timerfd_tracker *tfd)
{
if (tfd->dtor ())
cfree (tfd);
}
int
timerfd_tracker::ioctl_set_ticks (uint64_t new_exp_cnt)
2019-01-19 19:53:48 +01:00
{
LONG64 exp_cnt = (LONG64) new_exp_cnt;
if (exp_cnt == 0 || exp_cnt == -1LL)
return -EINVAL;
if (!enter_critical_section ())
return -EBADF;
set_expiration_count (exp_cnt);
2019-01-19 19:53:48 +01:00
timer_expired ();
leave_critical_section ();
return 0;
2019-01-19 19:53:48 +01:00
}
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
void
timerfd_tracker::init_fixup_after_fork_exec ()
{
/* Run this only if this is the first call, or all previous calls
came from close_on_exec descriptors */
if (winpid == GetCurrentProcessId ())
return;
tfd_shared = NULL;
cancel_evt = NULL;
sync_thr = NULL;
}
2019-01-19 19:53:48 +01:00
void
timerfd_tracker::fixup_after_fork_exec (bool execing)
{
NTSTATUS status;
OBJECT_ATTRIBUTES attr;
SIZE_T vsize = PAGE_SIZE;
/* Run this only once per process */
if (winpid == GetCurrentProcessId ())
return;
/* Recreate shared section mapping */
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
tfd_shared = NULL;
2019-01-19 19:53:48 +01:00
status = NtMapViewOfSection (tfd_shared_hdl, NtCurrentProcess (),
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
(PVOID *) &tfd_shared, 0, PAGE_SIZE, NULL,
&vsize, ViewShare, 0, PAGE_READWRITE);
2019-01-19 19:53:48 +01:00
if (!NT_SUCCESS (status))
api_fatal ("Can't recreate shared timerfd section during %s, status %y!",
execing ? "execve" : "fork", status);
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
/* Create cancel event for this processes timer thread */
InitializeObjectAttributes (&attr, NULL, 0, NULL, NULL);
2019-01-19 19:53:48 +01:00
status = NtCreateEvent (&cancel_evt, EVENT_ALL_ACCESS, &attr,
NotificationEvent, FALSE);
if (!NT_SUCCESS (status))
api_fatal ("Can't recreate timerfd cancel event during %s, status %y!",
execing ? "execve" : "fork", status);
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
/* Create sync event for this processes timer thread */
InitializeObjectAttributes (&attr, NULL, 0, NULL, NULL);
status = NtCreateEvent (&sync_thr, EVENT_ALL_ACCESS, &attr,
NotificationEvent, FALSE);
if (!NT_SUCCESS (status))
api_fatal ("Can't recreate timerfd sync event during %s, status %y!",
execing ? "execve" : "fork", status);
2019-01-19 19:53:48 +01:00
/* Set winpid so we don't run this twice */
winpid = GetCurrentProcessId ();
new cygthread (timerfd_thread, this, "timerfd", sync_thr);
}
LONG64
timerfd_tracker::wait (bool nonblocking)
{
HANDLE w4[2] = { get_timerfd_handle (), NULL };
LONG64 ret;
wait_signal_arrived here (w4[1]);
repeat:
switch (WaitForMultipleObjects (2, w4, FALSE, nonblocking ? 0 : INFINITE))
{
case WAIT_OBJECT_0: /* timer event */
if (!enter_critical_section ())
ret = -EIO;
else
{
ret = read_and_reset_expiration_count ();
2019-01-19 19:53:48 +01:00
leave_critical_section ();
switch (ret)
2019-01-19 19:53:48 +01:00
{
case -1: /* TFD_TIMER_CANCEL_ON_SET */
ret = -ECANCELED;
break;
case 0: /* Another read was quicker. */
if (!nonblocking)
goto repeat;
ret = -EAGAIN;
break;
default: /* Return (positive) expiration count. */
if (ret < 0)
ret = INT64_MAX;
break;
2019-01-19 19:53:48 +01:00
}
}
break;
case WAIT_OBJECT_0 + 1: /* signal */
if (_my_tls.call_signal_handler ())
goto repeat;
ret = -EINTR;
break;
case WAIT_TIMEOUT:
ret = -EAGAIN;
break;
default:
ret = -geterrno_from_win_error ();
break;
}
return ret;
}
int
timerfd_tracker::gettime (struct itimerspec *curr_value)
{
int ret = 0;
__try
{
if (!enter_critical_section ())
{
ret = -EBADF;
__leave;
}
}
__except (NO_ERROR)
{
return -EFAULT;
}
__endtry
__try
{
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
if (IsEventSignalled (disarm_evt ()))
*curr_value = time_spec ();
else
{
LONG64 next_relative_exp = get_exp_ts () - get_clock_now ();
curr_value->it_value.tv_sec = next_relative_exp / NS100PERSEC;
next_relative_exp -= curr_value->it_value.tv_sec * NS100PERSEC;
curr_value->it_value.tv_nsec = next_relative_exp
* (NSPERSEC / NS100PERSEC);
curr_value->it_interval = time_spec ().it_interval;
}
2019-01-19 19:53:48 +01:00
ret = 0;
}
__except (NO_ERROR)
{
ret = -EFAULT;
}
__endtry
leave_critical_section ();
2019-01-19 19:53:48 +01:00
return ret;
}
int
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
timerfd_tracker::arm_timer (int flags, const struct itimerspec *new_value)
2019-01-19 19:53:48 +01:00
{
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
LONG64 interval;
2019-01-19 19:53:48 +01:00
LONG64 ts;
NTSTATUS status;
LARGE_INTEGER DueTime;
BOOLEAN Resume;
LONG Period;
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
ResetEvent (disarm_evt ());
2019-01-19 19:53:48 +01:00
/* Convert incoming itimerspec into 100ns interval and timestamp */
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
interval = new_value->it_interval.tv_sec * NS100PERSEC
+ (new_value->it_interval.tv_nsec + (NSPERSEC / NS100PERSEC) - 1)
/ (NSPERSEC / NS100PERSEC);
2019-01-19 19:53:48 +01:00
ts = new_value->it_value.tv_sec * NS100PERSEC
+ (new_value->it_value.tv_nsec + (NSPERSEC / NS100PERSEC) - 1)
/ (NSPERSEC / NS100PERSEC);
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
set_flags (flags);
2019-01-19 19:53:48 +01:00
if (flags & TFD_TIMER_ABSTIME)
{
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
if (get_clockid () == CLOCK_REALTIME)
2019-01-19 19:53:48 +01:00
DueTime.QuadPart = ts + FACTOR;
else /* non-REALTIME clocks require relative DueTime. */
{
DueTime.QuadPart = get_clock_now () - ts;
2019-01-19 19:53:48 +01:00
/* If the timestamp was earlier than now, compute number
of expirations and offset DueTime to expire immediately. */
2019-01-19 19:53:48 +01:00
if (DueTime.QuadPart >= 0)
DueTime.QuadPart = -1LL;
2019-01-19 19:53:48 +01:00
}
}
else
{
/* Keep relative timestamps relative for the timer, but store the
expiry timestamp absolute for the timer thread. */
DueTime.QuadPart = -ts;
ts += get_clock_now ();
}
time_spec () = *new_value;
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
set_exp_ts (ts);
set_interval (interval);
read_and_reset_expiration_count ();
/* Note: Advanced Power Settings -> Sleep -> Allow Wake Timers
2019-01-19 19:53:48 +01:00
since W10 1709 */
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
Resume = (get_clockid () == CLOCK_REALTIME_ALARM
|| get_clockid () == CLOCK_BOOTTIME_ALARM);
if (interval > INT_MAX * (NS100PERSEC / MSPERSEC))
2019-01-19 19:53:48 +01:00
Period = 0;
else
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
Period = (interval + (NS100PERSEC / MSPERSEC) - 1)
2019-01-19 19:53:48 +01:00
/ (NS100PERSEC / MSPERSEC);
status = NtSetTimer (timer (), &DueTime, NULL, NULL, Resume, Period, NULL);
if (!NT_SUCCESS (status))
{
disarm_timer ();
return -geterrno_from_nt_status (status);
}
Cygwin: timerfd: rework implementation 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>
2019-02-25 20:58:12 +01:00
SetEvent (arm_evt ());
2019-01-19 19:53:48 +01:00
return 0;
}
int
timerfd_tracker::settime (int flags, const struct itimerspec *new_value,
struct itimerspec *old_value)
{
int ret = 0;
__try
{
if (!valid_timespec (new_value->it_value)
|| !valid_timespec (new_value->it_interval))
{
ret = -EINVAL;
__leave;
}
if (!enter_critical_section ())
{
ret = -EBADF;
__leave;
}
if (old_value && (ret = gettime (old_value)) < 0)
__leave;
2019-01-19 19:53:48 +01:00
if (new_value->it_value.tv_sec == 0 && new_value->it_value.tv_nsec == 0)
ret = disarm_timer ();
else
ret = arm_timer (flags, new_value);
leave_critical_section ();
ret = 0;
}
__except (NO_ERROR)
{
ret = -EFAULT;
}
__endtry
return ret;
}