src/Pure/ML-Systems/multithreading.ML
changeset 25735 4d147263f71f
parent 25704 df9c8074ff09
child 25775 90525e67ede7
equal deleted inserted replaced
25734:b00b903ae8ae 25735:4d147263f71f
    21   val self_critical: unit -> bool
    21   val self_critical: unit -> bool
    22   datatype 'a task =
    22   datatype 'a task =
    23     Task of {body: unit -> unit, cont: 'a -> 'a, fail: 'a -> 'a} | Wait | Terminate;
    23     Task of {body: unit -> unit, cont: 'a -> 'a, fail: 'a -> 'a} | Wait | Terminate;
    24   val schedule: int -> ('a -> 'a task * 'a) -> 'a -> exn list
    24   val schedule: int -> ('a -> 'a task * 'a) -> 'a -> exn list
    25   val serial: unit -> int
    25   val serial: unit -> int
       
    26   val get_data: 'a Universal.tag -> 'a option
       
    27   val put_data: 'a Universal.tag * 'a -> unit
    26 end;
    28 end;
    27 
    29 
    28 structure Multithreading: MULTITHREADING =
    30 structure Multithreading: MULTITHREADING =
    29 struct
    31 struct
       
    32 
       
    33 (* options *)
    30 
    34 
    31 val trace = ref (0: int);
    35 val trace = ref (0: int);
    32 fun tracing _ _ = ();
    36 fun tracing _ _ = ();
    33 
    37 
    34 val available = false;
    38 val available = false;
    35 val max_threads = ref (1: int);
    39 val max_threads = ref (1: int);
    36 
    40 
       
    41 
       
    42 (* critical section *)
       
    43 
    37 fun self_critical () = false;
    44 fun self_critical () = false;
    38 fun NAMED_CRITICAL _ e = e ();
    45 fun NAMED_CRITICAL _ e = e ();
    39 fun CRITICAL e = e ();
    46 fun CRITICAL e = e ();
       
    47 
       
    48 
       
    49 (* scheduling *)
    40 
    50 
    41 datatype 'a task =
    51 datatype 'a task =
    42   Task of {body: unit -> unit, cont: 'a -> 'a, fail: 'a -> 'a} | Wait | Terminate;
    52   Task of {body: unit -> unit, cont: 'a -> 'a, fail: 'a -> 'a} | Wait | Terminate;
    43 
    53 
    44 fun schedule _ _ _ = raise Fail "No multithreading support";
    54 fun schedule _ _ _ = raise Fail "No multithreading support";
    45 
    55 
       
    56 
       
    57 (* serial numbers *)
       
    58 
    46 local val count = ref (0: int)
    59 local val count = ref (0: int)
    47 in fun serial () = (count := ! count + 1; ! count) end;
    60 in fun serial () = (count := ! count + 1; ! count) end;
       
    61 
       
    62 
       
    63 (* thread data *)
       
    64 
       
    65 local
       
    66 
       
    67 val data = ref ([]: Universal.universal ref list);
       
    68 
       
    69 fun find_data tag =
       
    70   let
       
    71     fun find (r :: rs) = if Universal.tagIs tag (! r) then SOME r else find rs
       
    72       | find [] = NONE;
       
    73   in find (! data) end;
       
    74 
       
    75 in
       
    76 
       
    77 fun get_data tag = Option.map (Universal.tagProject tag o !) (find_data tag);
       
    78 
       
    79 fun put_data (tag, x) =
       
    80   (case find_data tag of
       
    81     SOME r => r := Universal.tagInject tag x
       
    82   | NONE => data := ref (Universal.tagInject tag x) :: ! data);
       
    83 
       
    84 end;
    48 
    85 
    49 end;
    86 end;
    50 
    87 
    51 structure BasicMultithreading: BASIC_MULTITHREADING = Multithreading;
    88 structure BasicMultithreading: BASIC_MULTITHREADING = Multithreading;
    52 open BasicMultithreading;
    89 open BasicMultithreading;