From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA12890; Fri, 6 Sep 2002 20:23:31 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id UAA12962 for ; Fri, 6 Sep 2002 20:23:30 +0200 (MET DST) Received: from relay.rinet.ru (relay.rinet.ru [195.54.192.35]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g86INT901570 for ; Fri, 6 Sep 2002 20:23:29 +0200 (MET DST) Received: (from uucp@localhost) by relay.rinet.ru (8.11.6/8.11.6) with UUCP id g86INSk12973 for caml-list@inria.fr; Fri, 6 Sep 2002 22:23:28 +0400 (MSD) X-Envelope-To: caml-list@inria.fr Received: from dialin1.stormoff (ROVER1) [192.168.0.129] by stormoff with esmtp (Exim 3.12 #1 (Debian)) id 17nNlF-00053z-00; Fri, 06 Sep 2002 22:23:18 +0400 X-Comment-To: Xavier Leroy To: caml-list@inria.fr Subject: Re: [Caml-list] Inter-thread exceptions References: <20020903164851.A26567@kruuna.Helsinki.FI> <20020905133052.C5343@pauillac.inria.fr> From: Dmitry Bely Date: Fri, 06 Sep 2002 22:22:13 +0400 In-Reply-To: <20020905133052.C5343@pauillac.inria.fr> (Xavier Leroy's message of "Thu, 5 Sep 2002 13:30:52 +0200") Message-ID: User-Agent: Gnus/5.090005 (Oort Gnus v0.05) XEmacs/21.4 (Common Lisp (Windows [3]), i586-pc-win32) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Xavier Leroy writes: >> How big a task would it be to implement asynchronous inter-thread >> exceptions? In practice the feature would amount to a function: >> >> val raise_to : exn -> Thread.t -> unit >> >> This would be most handy for implementing eg. timeouts: just make a thread >> that sleeps and raises an exception in the other thread when the time is >> spent. > > Yes, this is a reasonable model for thread cancellation -- a lot better > than Thread.kill, because the "victim" thread can catch the exception > and perform cleanup actions like releasing mutexes. > > It's not hard to implement with bytecode-level threads (where Caml > does the scheduling), but there is a major difficulty with system > threads: if the target thread is blocked on a system call such as > network I/O, it is extremely difficult to terminate the system call > prematurely and have the target thread honor the exception immediately. Nethertheless I think it's possible for network I/O, and even portable enough (should work under pthreads/windows threads). Roughly the idea is following: 0. Create a global "interrupt" socket. 1. Perform select() before each blocking syscall, waiting for readability of "interrupt" socket or the necessary state of "main" socket. 3. If "main" socket was in the necessary state, do the syscall. 4. If "interrupt" socket is readable, determine interrupt to which thread is requested. If it's other thread, call sched_yield() for pthreads (nothing under Windows) and return to (1) Interrupt is performed by sending some predefined data to "interrupt" socket. Of course this assumes some overhead, but hopefully not that much. - Dmitry Bely ------------------- 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