caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Are unreachable threads garbage collected?
       [not found] <ad8cfe7e0512031554g73aaf5f8w142196c4f9c939c0@mail.gmail.com>
@ 2005-12-04  0:16 ` Ker Lutyn
  2005-12-04  2:28   ` osiire
  0 siblings, 1 reply; 2+ messages in thread
From: Ker Lutyn @ 2005-12-04  0:16 UTC (permalink / raw)
  To: Jonathan Roewen, caml-list

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Are unreachable threads garbage collected?
  2005-12-04  0:16 ` [Caml-list] Are unreachable threads garbage collected? Ker Lutyn
@ 2005-12-04  2:28   ` osiire
  0 siblings, 0 replies; 2+ messages in thread
From: osiire @ 2005-12-04  2:28 UTC (permalink / raw)
  To: caml-list


On Sat, 3 Dec 2005 16:16:29 -0800 (PST)
In <20051204001629.39621.qmail@web34610.mail.mud.yahoo.com>
Ker Lutyn <ker527mail@yahoo.com> wrote:
> 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?

I think the strategy is reasonable. But the pattern can be more simple.

let make_gc_event v =
  let gc_channel = new_channel () in
  let gc_send () = sync (send gc_channel ()) in
  let finalise _ = ignore (Thread.create gc_send ()) in
  Gc.finalise finalise v;
  wrap (receive gc_channel)

usage is as follow:

  let bc = i, o in
  let gc = make_gc_event bc in
  let rec loop q =
    select [
      wrap (receive i) (fun x -> let ... in loop q);
      wrap (send o v) (fun _ -> loop q);
      gc (fun () -> Printf.printf "killed\n"; flush stdout; ())
    ]
  in
  ignore (Thread.create loop q);
  bc

----
 osiire


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-12-04  2:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <ad8cfe7e0512031554g73aaf5f8w142196c4f9c939c0@mail.gmail.com>
2005-12-04  0:16 ` [Caml-list] Are unreachable threads garbage collected? Ker Lutyn
2005-12-04  2:28   ` osiire

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).