caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <xavier.leroy@inria.fr>
To: Christoph Bauer <c_bauer@informatik.uni-kl.de>
Cc: OCaml List <caml-list@inria.fr>
Subject: Re: [Caml-list] POSIX Threads: kill
Date: Fri, 16 May 2003 11:54:10 +0200	[thread overview]
Message-ID: <20030516115410.A8817@pauillac.inria.fr> (raw)
In-Reply-To: <m3issbb8pr.fsf@diebuntekuh.de>; from c_bauer@informatik.uni-kl.de on Fri, May 16, 2003 at 10:24:00AM +0200

> in file ocam-3.06/otherlibs/systhreads/thread_posix is this comment:
> 
> (* Thread.kill is currently not implemented due to problems with
>    cleanup handlers on several platforms *)
> 
> Is anybody working on it?

The problem is as follows.  

POSIX threads have a notion of cleanup handlers, which are functions
that are called when the thread dies, either voluntarily (via
pthread_exit) or by being cancelled by another thread (via
pthread_cancel).

On some platforms (Tru64 Unix for sure, perhaps Solaris as well),
cleanup handlers are implemented via C++ exceptions.  (It is true that
POSIX threads is a C interface, however the Tru64 C compiler
understands C++ exceptions even when compiling pure C sources.)
Namely, a cleanup handler is mapped to a try... finally construct that
just does the right thing.

The problem is that C++ exception handling is based on unwinding stack
frames one by one till a matching exception handler is found.  This
requires stack frames to adhere strictly to a particular format, and
be equipped with stack descriptors that the C++ stack unwind mechanism
understands.  But of course the stack frames used ocamlopt-generated
code do not adhere to this format, and do not come with C++ stack
descriptors.  Hence, if the "systhreads" library was using
pthread_exit and pthread_cancel, the C/C++ runtime system would try to
unwind Caml stack frames, and just crash the whole program.

> Is there a solution for linux-i386?

LinuxThreads on Linux doesn't rely on C++ exceptions, so it doesn't
suffer from the problem above.  However, LinuxThreads is being
replaced by NPTL, another, better threading library for Linux, and I
don't know how NPTL implements cleanup handlers.

The general solution is to avoid using Thread.kill.  Terminating another
thread at arbitrary times is an inherently unsafe operation: the
killed thread may be holding mutexes, for instance.  There's a good
explanation of the problems in the Java documentation:

http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

explaining why they "deprecated" their equivalent of Thread.kill.

- Xavier Leroy

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2003-05-16  9:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-16  8:24 Christoph Bauer
2003-05-16  9:54 ` Xavier Leroy [this message]
2003-05-17 13:21   ` John Carr
2003-05-20 19:48     ` [Caml-list] register windows Xavier Leroy
2003-05-20 21:26       ` John Carr
2003-05-24  9:56         ` Xavier Leroy
2003-06-02 21:00     ` [Caml-list] POSIX Threads: kill John Max Skaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030516115410.A8817@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=c_bauer@informatik.uni-kl.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).