[m-rev.] diff: java mutables don't require synchronisation
Peter Wang
novalazy at gmail.com
Fri May 7 13:48:01 AEST 2010
Branches: main, 10.04
Reads/writes of 32-bit variables in Java are are atomic and do not require
further synchronisation. Don't generate `synchronized' statements in Java
mutable accessors.
compiler/make_hlds_passes.m:
compiler/prog_mutable.m:
As above.
diff --git a/compiler/make_hlds_passes.m b/compiler/make_hlds_passes.m
index ad1bf0d..ebf59a6 100644
--- a/compiler/make_hlds_passes.m
+++ b/compiler/make_hlds_passes.m
@@ -2327,20 +2327,13 @@ add_java_mutable_defn(TargetMutableName, Type, IsConstant, IsThreadLocal,
get_java_mutable_global_foreign_defn(_ModuleInfo, _Type, TargetMutableName,
IsConstant, IsThreadLocal, Context, DefnItem) :-
- MutableMutexVarName = mutable_mutex_var_name(TargetMutableName),
-
(
IsThreadLocal = mutable_not_thread_local,
- (
- IsConstant = yes,
- LockDefn = []
- ;
- IsConstant = no,
- LockDefn = ["static final java.lang.Object ", MutableMutexVarName,
- " = new java.lang.Object();\n"]
- ),
+ % Synchronization is only required for double and long values, which
+ % Mercury does not expose. We could also use the volatile keyword.
+ % (Java Language Specification, 2nd Ed., 17.4).
DefnBody = string.append_list([
- "static java.lang.Object ", TargetMutableName, ";\n" | LockDefn])
+ "static java.lang.Object ", TargetMutableName, ";\n"])
;
IsThreadLocal = mutable_thread_local,
DefnBody = string.append_list([
@@ -2425,9 +2418,7 @@ add_java_mutable_primitive_preds(TargetMutableName, ModuleName, MutableName,
MutableMutexVarName = mutable_mutex_var_name(TargetMutableName),
(
IsThreadLocal = mutable_not_thread_local,
- GetCode =
- "\tsynchronized (" ++ MutableMutexVarName ++ ") {\n" ++
- "\t\tX = " ++ TargetMutableName ++ ";\n\t}\n"
+ GetCode = "\tX = " ++ TargetMutableName ++ ";\n"
;
IsThreadLocal = mutable_thread_local,
GetCode = "\tX = " ++ TargetMutableName ++ ".get();\n"
@@ -2463,8 +2454,7 @@ add_java_mutable_primitive_preds(TargetMutableName, ModuleName, MutableName,
),
(
IsThreadLocal = mutable_not_thread_local,
- SetCode = "\tsynchronized (" ++ MutableMutexVarName ++ ") {\n" ++
- "\t\t" ++ TargetMutableName ++ " = X;\n\t}\n"
+ SetCode = "\t" ++ TargetMutableName ++ " = X;\n"
;
IsThreadLocal = mutable_thread_local,
SetCode = "\t" ++ TargetMutableName ++ ".set(X);\n"
diff --git a/compiler/prog_mutable.m b/compiler/prog_mutable.m
index 5d67513..feb6158 100644
--- a/compiler/prog_mutable.m
+++ b/compiler/prog_mutable.m
@@ -223,7 +223,6 @@
%
% :- pragma foreign_code("Java", "
% static java.lang.Object mutable_<varname>;
-% static final java.lang.Object mutable_<varname>_lock = new Object();
% ").
%
% :- initialise initialise_mutable_<varname>/0.
@@ -234,19 +233,17 @@
% impure set_<varname>(<initval>).
%
% Operations on mutables are defined in terms of the following two predicates.
-% They are actually "safe": we synchronize the access and assignment statements
-% within the predicates (by the Java specification, only actually necessary for
-% doubles and longs). They are named so to minimise the differences with the C
-% backends.
+% They are actually "safe": by the Java specification, 32-bit variables are
+% loaded/stored atomically. Doubles and longs may be treated as two 32-bit
+% variables, but Mercury does not expose them yet. The predicates are named so
+% to minimise the differences with the C backends.
%
% :- impure pred unsafe_set_<varname>(<vartype>::in(<varinst>)) is det.
% :- pragma foreign_proc("Java",
% unsafe_set_<varname>(X::in(<varinst>)),
% [will_not_call_mercury, thread_safe],
% "
-% synchronized (mutable_<varname>_lock) {
-% mutable_<varname> = X;
-% }
+% mutable_<varname> = X;
% ").
%
% :- semipure pred unsafe_get_<varname>(<vartype>::out(<varinst>)) is det.
@@ -254,9 +251,7 @@
% unsafe_get_varname(X::out(<varinst>)),
% [promise_semipure, will_not_call_mercury, thread_safe],
% "
-% synchronized (mutable_<varname>_lock) {
-% X = mutable_<varname>;
-% }
+% X = mutable_<varname>;
% ").
%
% As mutable_<varname> has the type `java.lang.Object' a cast is required
--------------------------------------------------------------------------
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