From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id CD8E3BB81 for ; Sun, 4 Dec 2005 01:16:31 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id jB40GVKG030348 for ; Sun, 4 Dec 2005 01:16:31 +0100 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 BAA23010 for ; Sun, 4 Dec 2005 01:16:30 +0100 (MET) Received: from web34610.mail.mud.yahoo.com (web34610.mail.mud.yahoo.com [209.191.68.144]) by nez-perce.inria.fr (8.13.0/8.13.0) with SMTP id jB40GToh030345 for ; Sun, 4 Dec 2005 01:16:30 +0100 Received: (qmail 39623 invoked by uid 60001); 4 Dec 2005 00:16:29 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=pOyxx7k3CLzlpr4RlsRjl0amiFoNqNy03euyhSjtTehIOHb5dIpI2WFIhP1Zr80dnhoSBfT37Mw0EBrYxtRxbBpGcunu6NgerSoEBxy4CIEkCrQ8edtclgUJcHoH1pLiBJNpoz7iMvWHL9WB+JTs8YtVql3DC84sjLGVyWNfLbg= ; Message-ID: <20051204001629.39621.qmail@web34610.mail.mud.yahoo.com> Received: from [69.110.33.39] by web34610.mail.mud.yahoo.com via HTTP; Sat, 03 Dec 2005 16:16:29 PST Date: Sat, 3 Dec 2005 16:16:29 -0800 (PST) From: Ker Lutyn Subject: Re: [Caml-list] Are unreachable threads garbage collected? To: Jonathan Roewen , caml-list@inria.fr In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Miltered: at nez-perce with ID 4392355F.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 4392355D.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 threads:01 garbage:01 reworking:01 buffered:01 buffered:01 struct:01 rec:01 rec:01 gc'd:01 translated:01 ocaml:01 garbage:01 struct:01 caml-list:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: ** X-Spam-Status: No, score=2.2 required=5.0 tests=FORGED_YAHOO_RCVD autolearn=disabled version=3.0.3 Here's my reworking of this example to make the thread go away if the buffered channel becomes "unreachable". The strategy is to allocate the channel object at the very end and associate a Gc.finalise function with it that syncs an event on a kill channel with the thread. Does this look like a reasonable design pattern for this? module Buffered_channel = struct open Event type 'a t = 'a channel * 'a channel let make () = let i = new_channel () in let o = new_channel () in let rec queue_event = function | [], [] -> wrap (receive i) (fun x -> Some ([x], [])) | (a :: aa) as aaa, bbb -> choose [ wrap (receive i) (fun x -> Some (aaa, x :: bbb)); wrap (send o a) (fun () -> Some (aa, bbb)) ] | [], bbb -> queue_event (List.rev bbb, []) in let kill_channel = new_channel () in let kill_event = wrap (receive kill_channel) (fun () -> None) in let rec loop q = match select [kill_event; queue_event q] with | None -> () | Some q -> loop q in ignore (Thread.create loop ([], [])); let kill_send () = sync (send kill_channel ()) in let kill_function _ = ignore (Thread.create kill_send ()) in let bc = i, o in Gc.finalise kill_function bc; bc let send (i, _) x = Event.send i x let receive (_, o) = Event.receive o end --- Jonathan Roewen wrote: > The channel won't become unreachable: The infinite loop as the thread > function keeps it alive. Only way for any of it to be GC'd is if one > of the functions for wrap throws an exception that propogates out. > > Jonathan > > On 12/4/05, Ker Lutyn wrote: > > >From Reppy's paper, an implementation of buffered channels translated to > OCaml. > > Note the internal thread to manage the state. If the buffered channel > becomes > > unreachable, will this thread be garbage collected? > > > > module Buffered_channel = struct > > open Event > > type 'a t = 'a channel * 'a channel > > let make () = > > let i = new_channel () in > > let o = new_channel () in > > let rec loop = function > > | [], [] -> loop ([sync (receive i)], []) > > | (a :: aa) as aaa, bbb -> select [ > > wrap (receive i) (fun x -> loop (aaa, x :: bbb)); > > wrap (send o a) (fun () -> loop (aa, bbb)) > > ] > > | [], bbb -> loop (List.rev bbb, []) > > in ignore (Thread.create loop ([], [])); (i, o) > > let send (i, _) x = Event.send i x > > let receive (_, o) = Event.receive o > > end > > > > > > > > > > __________________________________ > > Start your day with Yahoo! - Make it your home page! > > http://www.yahoo.com/r/hs > > > > _______________________________________________ > > Caml-list mailing list. Subscription management: > > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > > Archives: http://caml.inria.fr > > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > > Bug reports: http://caml.inria.fr/bin/caml-bugs > > > __________________________________ Start your day with Yahoo! - Make it your home page! http://www.yahoo.com/r/hs