[m-rev.] for review: get Mercury working on Google App Engine

Ian MacLarty maclarty at csse.unimelb.edu.au
Mon Sep 21 13:05:26 AEST 2009


For review by anyone.

Estimated hours taken: 1
Branches: main

Get the standard library working on Google App Engine.

library/io.m:
    Do not use AtomicInteger for ML_next_stream_id.
    Instead use a synchronized method to increment ML_next_stream_id.
    AtomicInteger is not supported on App Engine.

    Do not statically initialize mercury_stdin_binary and
    mercury_stdout_binary.  The initialization code doesn't work on App
    Engine.  Instead initialize these fields when they are required.

Index: library/io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.425
diff -u -r1.425 io.m
--- library/io.m	2 Sep 2009 05:48:01 -0000	1.425
+++ library/io.m	21 Sep 2009 02:53:22 -0000
@@ -5713,10 +5713,6 @@
 }
 ").
 
-:- pragma foreign_decl("Java", local, "
-    import java.util.concurrent.atomic.AtomicInteger;
-").
-
 :- pragma foreign_code("Java",
 "
     /* All text files (including stdin/stdout/stderr) are run through
@@ -5739,12 +5735,15 @@
     */
 
     public abstract static class MR_MercuryFileStruct {
-        public  static final AtomicInteger  ML_next_stream_id
-                                                = new AtomicInteger();
+        public  static int                  ML_next_stream_id = 0;
         public  int                         id;
 
         protected MR_MercuryFileStruct() {
-            id = ML_next_stream_id.getAndAdd(1);
+            assign_id();
+        }
+
+        private synchronized void assign_id() {
+            id = ML_next_stream_id++;
         }
 
         abstract public void close() throws java.io.IOException;
@@ -6398,13 +6397,28 @@
 static MR_TextOutputFile mercury_stderr =
     new MR_TextOutputFile(java.lang.System.err);
 
-static MR_BinaryInputFile mercury_stdin_binary =
-    new MR_BinaryInputFile(
-        new java.io.FileInputStream(java.io.FileDescriptor.in));
-
-static MR_BinaryOutputFile mercury_stdout_binary =
-    new MR_BinaryOutputFile(
-        new java.io.FileOutputStream(java.io.FileDescriptor.out));
+/**
+ * We initialize mercury_stdin_binary and mercury_stdout_binary
+ * only when they are needed,  because the initialization code
+ * does not work on Google's App Engine.
+ */
+static MR_BinaryInputFile mercury_stdin_binary = null;
+
+static MR_BinaryOutputFile mercury_stdout_binary = null;
+
+static void ensure_init_mercury_stdin_binary() {
+    if (mercury_stdin_binary == null) {
+        mercury_stdin_binary = new MR_BinaryInputFile(
+            new java.io.FileInputStream(java.io.FileDescriptor.in));
+    }
+}
+
+static void ensure_init_mercury_stdout_binary() {
+    if (mercury_stdout_binary == null) {
+        mercury_stdout_binary = new MR_BinaryOutputFile(
+            new java.io.FileOutputStream(java.io.FileDescriptor.out));
+    }
+}
 
 // Note: these are also set in io.init_state.
 
@@ -6425,6 +6439,7 @@
 static ThreadLocal<MR_BinaryInputFile> mercury_current_binary_input =
     new InheritableThreadLocal<MR_BinaryInputFile>() {
         protected MR_BinaryInputFile initialValue() {
+            ensure_init_mercury_stdin_binary();
             return mercury_stdin_binary;
         }
     };
@@ -6432,6 +6447,7 @@
 static ThreadLocal<MR_BinaryOutputFile> mercury_current_binary_output =
     new InheritableThreadLocal<MR_BinaryOutputFile>() {
         protected MR_BinaryOutputFile initialValue() {
+            ensure_init_mercury_stdout_binary();
             return mercury_stdout_binary;
         }
     };
@@ -8876,6 +8892,7 @@
     io.stdin_binary_stream_2(Stream::out, _IO0::di, _IO::uo),
     [will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "
+    ensure_init_mercury_stdin_binary();
     Stream = io.mercury_stdin_binary;
 ").
 
@@ -8883,6 +8900,7 @@
     io.stdout_binary_stream_2(Stream::out, _IO0::di, _IO::uo),
     [will_not_call_mercury, promise_pure, thread_safe, tabled_for_io],
 "
+    ensure_init_mercury_stdout_binary();
     Stream = io.mercury_stdout_binary;
 ").
 
--------------------------------------------------------------------------
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