[Mono-dev] Thread shutdown hook patch

Rodrigo Kumpera kumpera at gmail.com
Wed Feb 11 15:10:00 EST 2009

On Wed, Feb 11, 2009 at 12:55 PM, Paolo Molaro <lupus at ximian.com> wrote:

> On 02/09/09 Rodrigo Kumpera wrote:
> > > The attached patch adds a new hook to allow threads to shutdown after
> the
> > > GC finalizer has finished.
> > > The motivation for it is to improve profiler's reliability at shutdown
> > > time.
> > >
> > > The new callback notifies the thread when regular shutdown starts and
> gives
> > > it a change to not
> > > finish at this time.
> > >
> > > Later on the same callback is used to notify the thread that the last
> stage
> > > in the shutdown sequence
> > > has been reached and it must shutdown.
> > >
> > > The callback is per-thread as I don't want to have tools like the
> profiler
> > > messing up with embedded users.
> I think this instroduces too much complexity: since the API is public
> we'd need to end up maintaining the semantics as they happen to be now.
> It would be much clearer to have a flag on the thread that behaves
> similarly to critical finalizers: the thread that has it set will be
> destroyed as late as possible on shutdown.
> As for the callback, the existing mono_profiler_thread_end () should be
> enough: if it isn't we need to discuss how we can fix that instead of
> adding yet another callback.
> lupus

Let's see, to make what you propose work we would have to do the following

1) Introduce a flag to signal critical threads, which are only shutdown at
the last possible opportunity;
  Together with a mono_thread_set_critical (MonoThread*) public API to set
it, or course.

2) Move the profiler shutdown to mono_runtime_cleanup so it happens before
we wait for critical threads;
  This is required because the thread shutdown code has no means to ask
unmanaged code to shutdown.

A critical thread is one that

mono_profiler_thread_end doesn't play any role for this issue because it is
called once the thread has finished or detached,
which is not the issue here.

With this patch the code in the profiler gets simpler and more natural.
Everything is done from the profiler shutdown hook and
not piece by piece scattered around.

There is one issue with this patch, there is no unmanaged mechanism to
inform a thread that it must shutdown. Right now this isn't
an issue because the only user will be the profiller and it does all it's
cleanup from the shutdown hook. But it might not be the case
for embedders attaching critical threads.

I don't think this is an usable API without a well defined mechanism to let
embedders/tools know when it's time for a critical thread
to begin to shutdown.

The attached patch implements this proposal, please review.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20090211/1b153b2c/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: shutdown_take4.diff
Type: text/x-diff
Size: 6024 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20090211/1b153b2c/attachment.bin 

More information about the Mono-devel-list mailing list