caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Török Edwin" <edwin+ml-ocaml@etorok.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Threads and "transaction isolation" in OCaml
Date: Fri, 16 Aug 2013 11:46:48 +0300	[thread overview]
Message-ID: <520DE6F8.9000105@etorok.net> (raw)
In-Reply-To: <CAP_800r3q72mPT4L9onnvBL6X0CD63F9f-7t=GVA7GcFMrD3vw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1115 bytes --]

On 08/16/2013 12:57 AM, Markus Mottl wrote:
> Hi,
> 
> I just wondered how much we can rely on current OCaml-semantics where
> context-switches are impossible as long as there are no allocations.

I wrote a little test program and this is not true for bytecode -vmthread, but seems to work for bytecode -thread and native -thread:
$ ocamlfind ocamlc race.ml -package threads -vmthread -linkpkg -o race && ./race
started
Check OK
worker 2 OK
worker 3 OK
worker 4 OK
worker 5 OK
worker 7 OK
worker 8 OK
worker 9 OK
worker 10 OK
worker 11 OK
worker 12 OK
worker 13 OK
worker 14 OK
worker 0 OK
worker 1 OK
worker 6 OK
worker 15 OK
Array sum inconsistent: -33


> 
> Anyway, is it considered reasonably future-safe to write code of that
> sort?  I'd rather not have new compiler optimizations, etc., interfere
> with these assumptions in the near future.

I'd consider this very fragile as you'd need to know the implementation details of every function you call (or only calling your own safe functions). I was surprised to find out that even Array.fold_left creates a temporary ref for example.

Best regards,
--Edwin

[-- Attachment #2: race.ml --]
[-- Type: text/x-ocaml, Size: 1254 bytes --]

let n = 10000000
let global = Array.init n (fun i -> 0)
let cond = Condition.create ()
let mutex = Mutex.create ()

let rec check_consistent accum i =
  if i >= 0 then
    check_consistent (accum + global.(i)) (i-1)
  else if accum <> 0 then begin
    Printf.eprintf "Array sum inconsistent: %d\n" accum;
    flush stderr;
    exit 1
  end

let rec update_array x i =
  if i < n then begin
    if i mod 2 = 0 then
      global.(i) <- x
    else
      global.(i) <- -x;
    update_array x (i+1)
  end

let start = ref false

let barrier () =
  (* start all threads at same time *)
  Mutex.lock mutex;
  while !start = false do
    Condition.wait cond mutex
  done;
  Mutex.unlock mutex

let worker i =
  barrier ();
  while true; do
    (* the transaction *)
    update_array i 0;
    (* end of transaction *)
    Printf.printf "worker %d OK\n" i;
    flush stdout;
  done

let verifier () =
  barrier ();
  while true; do
    check_consistent 0 (n-1);
    Printf.printf "Check OK\n";
    flush stdout;
  done

let () =
  let a = Array.init 16 (fun i -> Thread.create worker i) in
  let t = Thread.create verifier () in
  start := true;
  Condition.broadcast cond;
  Printf.eprintf "started\n";
  flush stderr;
  Array.iter Thread.join a;
  Thread.join t

  parent reply	other threads:[~2013-08-16  8:46 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-15 21:57 Markus Mottl
2013-08-16  1:28 ` John F Carr
2013-08-16  2:55   ` Markus Mottl
2013-08-16  8:46 ` Török Edwin [this message]
2013-08-16 16:07   ` Markus Mottl
2013-08-17  0:06     ` Yaron Minsky
2013-08-19  6:10       ` Mark Shinwell
2013-08-19 14:18         ` Markus Mottl
2013-08-19  7:33     ` Xavier Leroy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=520DE6F8.9000105@etorok.net \
    --to=edwin+ml-ocaml@etorok.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).