Version française
Home     About     Download     Resources     Contact us    
Browse thread
select (or polling) on in_channel?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alex Baretta <alex@b...>
Subject: Re: [Caml-list] select (or polling) on in_channel?
Eijiro Sumii wrote:
> Thanks to everyone for ideas on how to "select" Pervasives.in_channel.
> Since there seems to be no better solution than
> 
>   (1) hacking the low-level structure by using external C, or
> 
>   (2) reinventing the high-level library on top of Unix I/O,
> ...

I have not been following this thread closely, so I did not answer 
earlier. My impression is that select and buffered IO are concepts that 
do not live well together. I This is probably the reason the Caml team 
has for not implementing such a feature in the Pervasives channels API.

I would attempt to model the problem in a multithreaded paradigm, where 
the the "selecting" is implicit in the thread scheduling mechanism. Of 
course, if you allow the use of threads, the selecting on channels 
becomes fairly trivial.

let (++) x f = f x

let input_string =
   let buf =  String.create 1024 in
     fun ic ->
       let bytes = input ic buf 0 1024 in
         String.sub buf 0 bytes

module IO_manager = struct
   type tio_manager = string Event.channel
   let wrap_in_channel (evch : io_manager) (ic : input_channel)=
     let rec cycle () =
       ic ++ input_string ++ Event.send ++ Event.sync;
       cycle ()
     in Thread.create cycle ()
end

Of course, this is a very rough sketch of message_passing API based on 
the Event module, encapsulating the select facility on top of an 
input_channel. Actually, ic could be any resource the program might need 
to synchronize on.

Let me add that Baretta DE&IT is using this paradigm to multiplex IO 
from/to different field buses in its Ocaml based, SoftPLC / SoftCNC kernel.

Alex

-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>