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>
This commit is contained in:
Corinna Vinschen
2019-02-25 20:58:12 +01:00
parent a4e2eb6ba3
commit 98afd02be3
4 changed files with 226 additions and 250 deletions

View File

@ -14,16 +14,6 @@ details. */
class timerfd_shared
{
HANDLE _access_mtx; /* controls access to shared data */
HANDLE _arm_evt; /* settimer sets event when timer is armed,
unsets event when timer gets disarmed. */
HANDLE _disarm_evt; /* settimer sets event when timer is armed,
unsets event when timer gets disarmed. */
HANDLE _timer; /* SynchronizationTimer */
HANDLE _expired_evt; /* Signal if timer expired, Unsignal on read. */
LONG instance_count; /* each open fd increments this.
If 0 -> delete timerfd_shared */
clockid_t _clockid; /* clockid */
struct itimerspec _time_spec; /* original incoming itimerspec */
LONG64 _exp_ts; /* start timestamp or next expire timestamp
@ -33,52 +23,29 @@ class timerfd_shared
int _flags; /* settime flags */
DWORD _tc_time; /* timestamp of the last WM_TIMECHANGE msg */
int create (clockid_t);
bool dtor ();
/* read access methods */
HANDLE arm_evt () const { return _arm_evt; }
HANDLE disarm_evt () const { return _disarm_evt; }
HANDLE timer () const { return _timer; }
HANDLE expired_evt () const { return _expired_evt; }
LONG64 get_clock_now () const { return get_clock (_clockid)->n100secs (); }
struct itimerspec &time_spec () { return _time_spec; }
int flags () const { return _flags; }
int get_flags () const { return _flags; }
void set_flags (int nflags) { _flags = nflags; }
/* write access methods */
void set_clockid (clockid_t clock_id) { _clockid = clock_id; }
void increment_expiration_count (LONG64 add)
{ InterlockedAdd64 (&_expiration_count, add); }
void set_expiration_count (LONG64 newval)
{ InterlockedExchange64 (&_expiration_count, newval); }
LONG64 read_and_reset_expiration_count ()
{
LONG64 ret = InterlockedExchange64 (&_expiration_count, 0);
if (ret)
ResetEvent (_expired_evt);
return ret;
}
bool enter_cs ()
{
return (WaitForSingleObject (_access_mtx, INFINITE) & ~WAIT_ABANDONED_0)
== WAIT_OBJECT_0;
}
void leave_cs ()
{
ReleaseMutex (_access_mtx);
}
LONG64 reset_expiration_count ()
{ return InterlockedExchange64 (&_expiration_count, 0); }
int arm_timer (int, const struct itimerspec *);
int disarm_timer ()
{
ResetEvent (_arm_evt);
memset (&_time_spec, 0, sizeof _time_spec);
_exp_ts = 0;
_interval = 0;
/* _flags = 0; DON'T DO THAT. Required for TFD_TIMER_CANCEL_ON_SET */
NtCancelTimer (timer (), NULL);
SetEvent (_disarm_evt);
return 0;
}
void timer_expired () { SetEvent (_expired_evt); }
void set_exp_ts (LONG64 ts) { _exp_ts = ts; }
friend class timerfd_tracker;
@ -86,13 +53,22 @@ class timerfd_shared
class timerfd_tracker /* cygheap! */
{
HANDLE tfd_shared_hdl; /* handle auf shared mem */
timerfd_shared *tfd_shared; /* pointer auf shared mem, needs
NtMapViewOfSection in each new process. */
/* Shared handles */
HANDLE tfd_shared_hdl; /* handle to shared mem */
HANDLE _access_mtx; /* controls access to shared data */
HANDLE _arm_evt; /* settimer sets event when timer is armed,
unsets event when timer gets disarmed. */
HANDLE _disarm_evt; /* settimer sets event when timer is armed,
unsets event when timer gets disarmed. */
HANDLE _timer; /* SynchronizationTimer */
HANDLE _expired_evt; /* Signal if timer expired, Unsignal on read. */
/* Process-local handles */
HANDLE cancel_evt; /* Signal thread to exit. */
HANDLE sync_thr; /* cygthread sync object. */
LONG local_instance_count; /* each open fd increments this.
/* pointer to shared timerfd, misc */
timerfd_shared *tfd_shared; /* pointer to shared mem, needs
NtMapViewOfSection in each new process. */
LONG instance_count; /* each open fd increments this.
If 0 -> cancel thread. */
DWORD winpid; /* This is used @ fork/exec time to know if
this tracker already has been fixed up. */
@ -105,69 +81,91 @@ class timerfd_tracker /* cygheap! */
bool dtor ();
bool enter_critical_section () const { return tfd_shared->enter_cs (); }
void leave_critical_section () const { tfd_shared->leave_cs (); }
bool enter_critical_section ()
{
return (WaitForSingleObject (_access_mtx, INFINITE) & ~WAIT_ABANDONED_0)
== WAIT_OBJECT_0;
}
void leave_critical_section ()
{
ReleaseMutex (_access_mtx);
}
int arm_timer (int flags, const struct itimerspec *new_value) const
{ return tfd_shared->arm_timer (flags, new_value); }
int disarm_timer () const { return tfd_shared->disarm_timer (); }
void timer_expired () const { tfd_shared->timer_expired (); }
HANDLE arm_evt () const { return _arm_evt; }
HANDLE disarm_evt () const { return _disarm_evt; }
HANDLE timer () const { return _timer; }
HANDLE expired_evt () const { return _expired_evt; }
void timer_expired () { SetEvent (_expired_evt); }
int arm_timer (int flags, const struct itimerspec *new_value);
int disarm_timer ()
{
ResetEvent (_arm_evt);
tfd_shared->disarm_timer ();
NtCancelTimer (timer (), NULL);
SetEvent (_disarm_evt);
return 0;
}
void timer_expired () const { timer_expired (); }
LONG64 expiration_count () const { return tfd_shared->_expiration_count; }
void increment_expiration_count (LONG64 add) const
{ tfd_shared->increment_expiration_count (add); }
void set_expiration_count (LONG64 exp_cnt) const
{ tfd_shared->set_expiration_count ((LONG64) exp_cnt); }
LONG64 read_and_reset_expiration_count () const
{ return tfd_shared->read_and_reset_expiration_count (); }
LONG64 read_and_reset_expiration_count ()
{
LONG64 ret = tfd_shared->reset_expiration_count ();
if (ret)
ResetEvent (_expired_evt);
return ret;
}
struct timespec it_value () const
{ return tfd_shared->time_spec ().it_value; }
struct timespec it_interval () const
{ return tfd_shared->time_spec ().it_interval; }
void set_clockid (clockid_t clock_id) { tfd_shared->set_clockid (clock_id); }
clock_t get_clockid () const { return tfd_shared->_clockid; }
LONG64 get_clock_now () const { return tfd_shared->get_clock_now (); }
struct itimerspec &time_spec () { return tfd_shared->time_spec (); }
LONG64 get_exp_ts () const { return tfd_shared->_exp_ts; }
LONG64 get_interval () const { return tfd_shared->_interval; }
int flags () const { return tfd_shared->flags (); }
void set_interval (LONG64 intv) { tfd_shared->_interval = intv; }
int get_flags () const { return tfd_shared->get_flags (); }
void set_flags (int nflags) { tfd_shared->set_flags (nflags); }
DWORD tc_time () const { return tfd_shared->_tc_time; }
void set_tc_time (DWORD new_time) { tfd_shared->_tc_time = new_time; }
void set_exp_ts (LONG64 ts) const { tfd_shared->set_exp_ts (ts); }
LONG decrement_instances () { return InterlockedDecrement (&instance_count); }
public:
void *operator new (size_t, void *p) __attribute__ ((nothrow)) {return p;}
timerfd_tracker ()
: tfd_shared_hdl (NULL), tfd_shared (NULL), cancel_evt (NULL),
sync_thr (NULL), local_instance_count (1), winpid (0), window (NULL),
atom (0) {}
: tfd_shared_hdl (NULL), _access_mtx (NULL), _arm_evt (NULL),
_disarm_evt (NULL), cancel_evt (NULL), sync_thr (NULL), tfd_shared (NULL),
instance_count (1), winpid (0), window (NULL), atom (0) {}
void init_fixup_after_fork_exec ();
void fixup_after_fork_exec (bool);
void fixup_after_fork ()
{
init_fixup_after_fork_exec ();
fixup_after_fork_exec (false);
}
void fixup_after_exec () { fixup_after_fork_exec (true); }
void dup () { InterlockedIncrement (&instance_count); }
HANDLE get_timerfd_handle () const { return expired_evt (); }
LONG64 wait (bool);
int ioctl_set_ticks (uint64_t);
int create (clockid_t);
int gettime (struct itimerspec *);
int settime (int, const struct itimerspec *, struct itimerspec *);
static void dtor (timerfd_tracker *);
void close ();
int ioctl_set_ticks (uint64_t);
void fixup_after_fork_exec (bool);
void fixup_after_fork () { fixup_after_fork_exec (false); }
void fixup_after_exec () { fixup_after_fork_exec (true); }
HANDLE get_timerfd_handle () const { return tfd_shared->expired_evt (); }
HANDLE get_disarm_evt () const { return tfd_shared->disarm_evt (); }
LONG64 wait (bool);
void increment_global_instances ()
{ InterlockedIncrement (&tfd_shared->instance_count); }
void increment_instances ()
{
InterlockedIncrement (&tfd_shared->instance_count);
InterlockedIncrement (&local_instance_count);
}
void decrement_instances ()
{
InterlockedDecrement (&tfd_shared->instance_count);
InterlockedDecrement (&local_instance_count);
}
static void dtor (timerfd_tracker *);
DWORD thread_func ();
};