| author | blanchet | 
| Fri, 28 Sep 2012 09:12:49 +0200 | |
| changeset 49631 | 9ce0c2cbadb8 | 
| parent 49319 | f4b91a3a5f0f | 
| child 52945 | 13687b130f1f | 
| permissions | -rw-r--r-- | 
| 28199 | 1 | (* Title: Pure/Concurrent/par_list.ML | 
| 2 | Author: Makarius | |
| 3 | ||
| 4 | Parallel list combinators. | |
| 5 | ||
| 6 | Notes: | |
| 7 | ||
| 8 | * These combinators only make sense if the operator (function or | |
| 9 | predicate) applied to the list of operands takes considerable | |
| 10 | time. The overhead of scheduling is significantly higher than | |
| 11 | just traversing the list of operands sequentially. | |
| 12 | ||
| 28358 | 13 | * The order of operator application is non-deterministic. Watch out | 
| 28199 | 14 | for operators that have side-effects or raise exceptions! | 
| 15 | *) | |
| 16 | ||
| 17 | signature PAR_LIST = | |
| 18 | sig | |
| 44265 
b94951f06e48
export Par_List.managed_results, to enable specific treatment of results apart from default Par_Exn.release_first;
 wenzelm parents: 
44247diff
changeset | 19 |   val managed_results: string -> ('a -> 'b) -> 'a list -> 'b Exn.result list
 | 
| 41711 | 20 |   val map_name: string -> ('a -> 'b) -> 'a list -> 'b list
 | 
| 28199 | 21 |   val map: ('a -> 'b) -> 'a list -> 'b list
 | 
| 22 |   val get_some: ('a -> 'b option) -> 'a list -> 'b option
 | |
| 23 |   val find_some: ('a -> bool) -> 'a list -> 'a option
 | |
| 24 |   val exists: ('a -> bool) -> 'a list -> bool
 | |
| 25 |   val forall: ('a -> bool) -> 'a list -> bool
 | |
| 26 | end; | |
| 27 | ||
| 29368 | 28 | structure Par_List: PAR_LIST = | 
| 28199 | 29 | struct | 
| 30 | ||
| 41673 | 31 | fun managed_results name f xs = | 
| 41675 | 32 | if null xs orelse null (tl xs) orelse | 
| 33 | not (Multithreading.enabled ()) orelse Multithreading.self_critical () | |
| 34 | then map (Exn.capture f) xs | |
| 35 | else | |
| 49319 | 36 | uninterruptible (fn restore_attributes => fn () => | 
| 37 | let | |
| 38 | val group = Future.new_group (Future.worker_group ()); | |
| 39 | val futures = | |
| 40 |           Future.forks {name = name, group = SOME group, deps = [], pri = 0, interrupts = true}
 | |
| 41 | (map (fn x => fn () => f x) xs); | |
| 42 | val results = | |
| 43 | restore_attributes Future.join_results futures | |
| 44 | handle exn => | |
| 45 | (if Exn.is_interrupt exn then Future.cancel_group group else (); reraise exn); | |
| 46 | in results end) (); | |
| 28199 | 47 | |
| 44247 | 48 | fun map_name name f xs = Par_Exn.release_first (managed_results name f xs); | 
| 41711 | 49 | fun map f = map_name "Par_List.map" f; | 
| 28199 | 50 | |
| 51 | fun get_some f xs = | |
| 52 | let | |
| 53 | exception FOUND of 'b option; | |
| 54 | fun found (Exn.Exn (FOUND some)) = some | |
| 55 | | found _ = NONE; | |
| 40700 
4b4dfe05b5d7
clarified Par_List.managed_results, with explicit propagation of outermost physical interrupt to forked futures (e.g. to make timeout apply here as expected and prevent zombies);
 wenzelm parents: 
37865diff
changeset | 56 | val results = | 
| 41673 | 57 | managed_results "Par_List.get_some" | 
| 58 | (fn x => (case f x of NONE => () | some => raise FOUND some)) xs; | |
| 28199 | 59 | in | 
| 60 | (case get_first found results of | |
| 61 | SOME y => SOME y | |
| 44247 | 62 | | NONE => (Par_Exn.release_first results; NONE)) | 
| 28199 | 63 | end; | 
| 64 | ||
| 65 | fun find_some P = get_some (fn x => if P x then SOME x else NONE); | |
| 66 | ||
| 67 | fun exists P = is_some o get_some (fn x => if P x then SOME () else NONE); | |
| 68 | fun forall P = not o exists (not o P); | |
| 69 | ||
| 70 | end; |