caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ker Lutyn <ker527mail@yahoo.com>
To: Jonathan Roewen <jonathan.roewen@gmail.com>, caml-list@inria.fr
Subject: Re: [Caml-list] Are unreachable threads garbage collected?
Date: Sat, 3 Dec 2005 16:16:29 -0800 (PST)	[thread overview]
Message-ID: <20051204001629.39621.qmail@web34610.mail.mud.yahoo.com> (raw)
In-Reply-To: <ad8cfe7e0512031554g73aaf5f8w142196c4f9c939c0@mail.gmail.com>

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 <jonathan.roewen@gmail.com> 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 <ker527mail@yahoo.com> 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


       reply	other threads:[~2005-12-04  0:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <ad8cfe7e0512031554g73aaf5f8w142196c4f9c939c0@mail.gmail.com>
2005-12-04  0:16 ` Ker Lutyn [this message]
2005-12-04  2:28   ` osiire

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=20051204001629.39621.qmail@web34610.mail.mud.yahoo.com \
    --to=ker527mail@yahoo.com \
    --cc=caml-list@inria.fr \
    --cc=jonathan.roewen@gmail.com \
    /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).