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

@ -30,21 +30,17 @@ fhandler_timerfd::get_proc_fd_name (char *buf)
/* The timers connected to a descriptor are stored on the cygheap
together with their fhandler. */
#define cnew(name, ...) \
({ \
void* ptr = (void*) ccalloc (HEAP_FHANDLER, 1, sizeof (name)); \
ptr ? new (ptr) name (__VA_ARGS__) : NULL; \
})
int
fhandler_timerfd::timerfd (clockid_t clock_id, int flags)
{
timerfd_tracker *tfd = cnew (timerfd_tracker);
timerfd_tracker *tfd = (timerfd_tracker *)
ccalloc (HEAP_FHANDLER, 1, sizeof (timerfd_tracker));
if (!tfd)
{
set_errno (ENOMEM);
return -1;
}
new (tfd) timerfd_tracker ();
int ret = tfd->create (clock_id);
if (ret < 0)
{
@ -178,7 +174,7 @@ fhandler_timerfd::dup (fhandler_base *child, int flags)
__try
{
timerfd_tracker *tfd = (timerfd_tracker *) fhc->timerid;
tfd->increment_instances ();
tfd->dup ();
ret = 0;
}
__except (EFAULT) {}
@ -234,8 +230,9 @@ fhandler_timerfd::fixup_after_exec ()
__try
{
timerfd_tracker *tfd = (timerfd_tracker *) timerid;
tfd->init_fixup_after_fork_exec ();
if (close_on_exec ())
tfd->decrement_instances ();
timerfd_tracker::dtor (tfd);
else
tfd->fixup_after_exec ();
}
@ -243,17 +240,6 @@ fhandler_timerfd::fixup_after_exec ()
__endtry
}
fhandler_timerfd::~fhandler_timerfd ()
{
__try
{
timerfd_tracker *tfd = (timerfd_tracker *) timerid;
timerfd_tracker::dtor (tfd);
}
__except (EFAULT) {}
__endtry
}
int
fhandler_timerfd::close ()
{
@ -262,7 +248,7 @@ fhandler_timerfd::close ()
__try
{
timerfd_tracker *tfd = (timerfd_tracker *) timerid;
tfd->close ();
timerfd_tracker::dtor (tfd);
ret = 0;
}
__except (EFAULT) {}