Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0004090OCamlotherlibspublic2006-08-24 02:312016-12-07 16:59
Assigned To 
PlatformOSOS Version
Product Version3.09.2 
Target VersionFixed in Version 
Summary0004090: A better interface for select
DescriptionThe existing interface for is adequate, but it's often not so nice to work with. Here's an example using the current API:

(* You have a data structure with some file descriptors and auxilliary data *)
let foo = [(fd1, "hello"); (fd2, "world"); (fd3, "baz")] in
(* Now you need a list of file descriptors *)
let fds = fst foo in
(* Feed them to select *)
let _, outs, _ = [] fds [] (-1.0) in
(* Now do something with each active descriptor *)
List.iter (fun fd ->
  let data = List.assoc fd foo in
  do_something fd data) outs

The problem is that when you select on a file descriptor there's usually some context you care about that's associated with that descriptor -- e.g. some data to write or a buffer to read into. This means that you need to create an otherwise useless list of file descriptors (fds in the example) before calling select, and after you find out that file descriptor x is ready you have to search through some auxilliary data structure to find the data associated with x. This means extra work for the programmer and the program both before and after the select call.

I would like to propose a new, additional interface for the select call, which I'm calling select2. You can probably guess the idea by looking at the type.

val select2:
   (Unix.file_descr * 'a) list ->
   (Unix.file_descr * 'b) list ->
   (Unix.file_descr * 'c) list ->
   float ->
      (Unix.file_descr * 'a) list *
      (Unix.file_descr * 'b) list *
      (Unix.file_descr * 'c) list

To be clear, the idea is that each file descriptor can have a value associated with it, and these values are returned along with the file descriptors on output. This interface is much nicer to work with and even slightly more efficient, since there's no need to search for the contextual data after the call. The example becomes:

(* You have a data structure with some file descriptors and auxilliary data *)
let foo = [(fd1, "hello"); (fd2, "world"); (fd3, "baz")] in
(* Feed it to select2 *)
let _, outs, _ = Unix.select2 [] foo [] (-1.0) in
(* Now do something with the data *)
List.iter (fun (fd, data) -> do_something fd data) outs

I've attached a patch against OCaml 3.09.2 that implements select2 for Unix platforms. The Win32 implementation would be trivial. The vmthreads implementation would probably best be done as an OCaml wrapper of regular select. I will be happy to do this work if the patch is accepted, but first I thought I would see if the OCaml team is interested in the idea.
Attached Filesdiff file icon select2.diff [^] (7,179 bytes) 2006-08-24 02:31 [Show Content]

- Relationships

-  Notes
There are no notes attached to this issue.

- Issue History
Date Modified Username Field Change
2006-08-24 02:31 n8gray New Issue
2006-08-24 02:31 n8gray File Added: select2.diff
2006-08-29 16:33 doligez Status new => acknowledged
2013-09-03 17:23 doligez Tag Attached: patch
2016-12-07 16:59 shinwell Category OCaml general => OCaml otherlibs
2017-02-23 16:42 doligez Category OCaml otherlibs => otherlibs

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker