Implement fhandler reference counting.

* cygheap.h
(cygheap_fdmanip::release): Make virtual.
(cygheap_fdnew::~cygheap_fdnew): New destructor increments reference count when
fd has been allocated.
(cygheap_fdget::fh): New (old?) field.
(cygheap_fdget::cygheap_fdget): Increment reference count when we've found an
active fd.  Set fh appropriately.
(cygheap_fdget::~cygheap_fdget): Decrement reference count when appropriate.
Delete fh if reference count goes to zero.
(cygheap_fdget::release): New function.  Do more bookkeping on release.
* dtable.cc (dtable::release): Change from void to boolean return.  Only delete
the fhandler when its reference count is <= 0 (this should be a fairly unusual
case).  Return true if fhandler has been deleted.
(cygwin_attach_handle_to_fd): Increment reference count when fh is assigned.
(dtable::init_std_file_from_handle): Ditto.
* dtable.h (dtable::release): Change return to boolean.
* fhandler.cc (fhandler_base::fhandler_base): Set new isclosed flag to false.
Set _refcnt to zero.
(fhandler_base::close): Simplify paranoid debugging output.  Set new isclosed()
flag.
(fhandler_base_overlapped::wait_overlapped): Use isclosed() flag to avoid
querying the exception handle.
* fhandler.h (fhandler_base::_refcnt): New field.
(fhandler_base::refcnt): New function.
(fhandler_base::isclosed): Implement.
(fhandler_base::fhandler_base): Set isclosed to false.
* syscalls.cc: Remove space after function before parentheses for several
strace printfs.
(dup): Add standard strace "leaver" code.
(dup2): Ditto.
(dup3): Ditto.
(remove): Ditto.
(getpid): Ditto.
(getppid): Ditto.
(lseek64): Fix strace debugging to correctly use %R.
* fhandler_termios.cc (fhandler_termios::tcsetpgrp): Avoid sending signals to
other processes if we're debugging since it can cause a deadlock with the
calling debugger.
* exceptions.cc (_cygtls::call_signal_handler): Add debugging-only strace
output.
This commit is contained in:
Christopher Faylor
2011-12-17 00:03:31 +00:00
parent 6eee66418c
commit ca35d41cec
9 changed files with 141 additions and 46 deletions

View File

@@ -241,16 +241,26 @@ dtable::find_unused_handle (int start)
return -1;
}
void
bool
dtable::release (int fd)
{
if (!not_open (fd))
bool deleted;
if (not_open (fd))
deleted = false;
else
{
if (fds[fd]->need_fixup_before ())
dec_need_fixup_before ();
delete fds[fd];
if (fds[fd]->refcnt (-1) > 0)
deleted = false;
else
{
deleted = true;
delete fds[fd];
}
fds[fd] = NULL;
}
return deleted;
}
extern "C" int
@@ -261,6 +271,7 @@ cygwin_attach_handle_to_fd (char *name, int fd, HANDLE handle, mode_t bin,
fd = cygheap->fdtab.find_unused_handle ();
fhandler_base *fh = build_fh_name (name);
cygheap->fdtab[fd] = fh;
cygheap->fdtab[fd]->refcnt (1);
fh->init (handle, myaccess, bin ?: fh->pc_binmode ());
return fd;
}
@@ -395,6 +406,7 @@ dtable::init_std_file_from_handle (int fd, HANDLE handle)
fh->open_setup (openflags);
fh->usecount = 0;
cygheap->fdtab[fd] = fh;
cygheap->fdtab[fd]->refcnt (1);
set_std_handle (fd);
paranoid_printf ("fd %d, handle %p", fd, handle);
}