[m-rev.] diff: update dining philosopher examples

Julien Fischer juliensf at csse.unimelb.edu.au
Wed Nov 3 18:04:58 AEDT 2010


Hi,

The contents of extras/concurrency are currently a bit of a mess.
I am going to move the useful bits, the examples, into
samples/concurrency and then delete the rest of it.  The samples
directory is a more appropriateplace for the examples since the thread
module and friends are now part of the standard library.

I intend to do something similar for extras/lazy_evaluation as well
now that the lazy module is part of the standard library.

--------------------

Update the various dining philosopher examples in the extras distribution.

extras/concurrency/philo.m:
 	Use a mutable to pass data between threads rather than the global
 	module in this directory.  This makes the example more self-contained
 	and also removes dependencies on C foreign code.

extras/concurrency/philo[23].m:
 	Provide an implementation of rand_sleep/3 for the Java grade.

Julien.

Index: philo.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/concurrency/philo.m,v
retrieving revision 1.3
diff -u -r1.3 philo.m
--- philo.m	1 Feb 2007 08:07:58 -0000	1.3
+++ philo.m	3 Nov 2010 06:56:23 -0000
@@ -28,7 +28,6 @@

  :- implementation.

-:- import_module global.
  :- import_module thread.
  :- import_module thread.semaphore.

@@ -39,6 +38,11 @@

  %-----------------------------------------------------------------------------%

+:- mutable(fork_global, forks, forks(yes, yes, yes, yes, yes), ground,
+    [untrailed, attach_to_io_state]).
+
+%-----------------------------------------------------------------------------%
+
  :- type forks
      --->    forks(bool, bool, bool, bool, bool).

@@ -50,32 +54,30 @@
      ;       sartre.

  main(!IO) :-
-    new(Lock, !IO),
-    signal(Lock, !IO),
-    new(forks(yes, yes, yes, yes, yes), ForkGlob, !IO),
-    spawn(philosopher(plato, Lock, ForkGlob), !IO),
-    spawn(philosopher(aristotle, Lock, ForkGlob), !IO),
-    spawn(philosopher(descartes, Lock, ForkGlob), !IO),
-    spawn(philosopher(russell, Lock, ForkGlob), !IO),
-    philosopher(sartre, Lock, ForkGlob, !IO).
+    semaphore.new(Lock, !IO),
+    semaphore.signal(Lock, !IO),
+    spawn(philosopher(plato, Lock), !IO),
+    spawn(philosopher(aristotle, Lock), !IO),
+    spawn(philosopher(descartes, Lock), !IO),
+    spawn(philosopher(russell, Lock), !IO),
+    philosopher(sartre, Lock, !IO).

-:- pred philosopher(philosopher::in, semaphore::in, global(forks)::in,
-    io::di, io::uo) is cc_multi.
+:- pred philosopher(philosopher::in, semaphore::in, io::di, io::uo) is cc_multi.

-philosopher(Who, Lock, ForkGlob, !IO) :-
+philosopher(Who, Lock, !IO) :-
      name(Who, Name),
      io.format("%s is thinking.\n", [s(Name)], !IO),
-    wait(Lock, !IO),
-    get(ForkGlob, Forks0, !IO),
+    semaphore.wait(Lock, !IO),
+    get_fork_global(Forks0, !IO),
      ( forks(Who, Forks0, Forks1) ->
-        set(ForkGlob, Forks1, !IO),
-        signal(Lock, !IO),
+        set_fork_global(Forks1, !IO),
+        semaphore.signal(Lock, !IO),
          io.format("%s is eating.\n", [s(Name)], !IO),
-        wait(Lock, !IO),
-        get(ForkGlob, Forks2, !IO),
+        semaphore.wait(Lock, !IO),
+        get_fork_global(Forks2, !IO),
          ( forks(Who, Forks3, Forks2) ->
-            set(ForkGlob, Forks3, !IO),
-            signal(Lock, !IO)
+            set_fork_global(Forks3, !IO),
+            semaphore.signal(Lock, !IO)
          ;
              error("all forked up")
          )
@@ -83,7 +85,7 @@
          % Our 2 forks were not available
          signal(Lock, !IO)
      ),
-    philosopher(Who, Lock, ForkGlob, !IO).
+    philosopher(Who, Lock, !IO).

  :- pred forks(philosopher, forks, forks).
  :- mode forks(in, in, out) is semidet.
@@ -104,4 +106,5 @@
  name(sartre,    "Sartre").

  %-----------------------------------------------------------------------------%
+:- end_module philo.
  %-----------------------------------------------------------------------------%
Index: philo2.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/concurrency/philo2.m,v
retrieving revision 1.9
diff -u -r1.9 philo2.m
--- philo2.m	1 Feb 2007 08:07:58 -0000	1.9
+++ philo2.m	3 Nov 2010 06:56:23 -0000
@@ -102,14 +102,26 @@
  name(russell,   "Russell").
  name(sartre,    "Sartre").

+%---------------------------------------------------------------------------%
+
  :- pragma foreign_code("C#", "
      public static System.Random rng = new System.Random();
  ").

+:- pragma foreign_decl("Java", "
+
+import java.util.Random;
+
+").
+
+:- pragma foreign_code("Java", "
+    public static Random rng = new Random();
+").
+
  :- pred rand_sleep(int::in, io::di, io::uo) is det.

  :- pragma foreign_proc("C",
-    rand_sleep(Int::in, IO0::di, IO::uo),
+    rand_sleep(Int::in, _IO0::di, _IO::uo),
      [promise_pure, thread_safe, will_not_call_mercury],
  "
  #ifdef _MSC_VER
@@ -117,7 +129,6 @@
  #else
      sleep((rand() % Int));
  #endif
-    IO =  IO0;
  ").

  :- pragma foreign_proc("C#",
@@ -127,5 +138,17 @@
      System.Threading.Thread.Sleep(rng.Next(Int) * 1000);
  ").

+:- pragma foreign_proc("Java",
+    rand_sleep(Int::in, _IO0::di, _IO::uo),
+    [promise_pure, thread_safe, will_not_call_mercury],
+"
+    try {
+        Thread.sleep(rng.nextInt(Int) * 1000);
+    } catch ( InterruptedException e ) {
+        /* Just return if we are interrupted.*/
+    }
+").
+
  %---------------------------------------------------------------------------%
+:- end_module philo2.
  %---------------------------------------------------------------------------%
Index: philo3.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/extras/concurrency/philo3.m,v
retrieving revision 1.6
diff -u -r1.6 philo3.m
--- philo3.m	1 Feb 2007 08:07:58 -0000	1.6
+++ philo3.m	3 Nov 2010 06:56:23 -0000
@@ -1,8 +1,10 @@
  %-----------------------------------------------------------------------------%
+% vim: ts=4 sw=4 et tw=0 wm=0 ff=unix ft=mercury
+%-----------------------------------------------------------------------------%
+%
  % philo3.m
  % Copyright (C) 2001-2002 Ralph Becket <rbeck at microsoft.com>
  % Mon May 14 14:32:29 BST 2001
-% vim: ts=4 sw=4 et tw=0 wm=0 ff=unix ft=mercury
  %
  % RELEASED TO THE MERCURY PROJECT FOR DISTRIBUTION UNDER
  % WHATEVER LICENCE IS DEEMED APPROPRIATE BY THE PROJECT
@@ -81,6 +83,16 @@
  	public static System.Random rng = new System.Random();
  ").

+:- pragma foreign_decl("Java", "
+
+import java.util.Random;
+
+").
+
+:- pragma foreign_code("Java", "
+    public static Random rng = new Random();
+").
+
  :- pred rand_sleep(int::in, io::di, io::uo) is det.
  :- pragma foreign_proc("C",
      rand_sleep(Int::in, IO0::di, IO::uo),
@@ -101,5 +113,17 @@
  	System.Threading.Thread.Sleep(rng.Next(Int) * 1000);
  ").

+:- pragma foreign_proc("Java",
+    rand_sleep(Int::in, _IO0::di, _IO::uo),
+    [promise_pure, thread_safe, will_not_call_mercury],
+"
+    try {
+        Thread.sleep(rng.nextInt(Int) * 1000);
+    } catch ( InterruptedException e ) {
+        /* Just return if we are interrupted.*/
+    }
+").
+
  %-----------------------------------------------------------------------------%
+:- end_module philo3.
  %-----------------------------------------------------------------------------%
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list