IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
atomic.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2007-2011 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24
25using System;
26
27#if IMPORTER
28using IKVM.Reflection;
30
31using Type = IKVM.Reflection.Type;
32#else
33using System.Reflection;
34using System.Reflection.Emit;
35#endif
36
37using InstructionFlags = IKVM.Runtime.ClassFile.Method.InstructionFlags;
38
39namespace IKVM.Runtime
40{
41
43 {
44
45 internal static bool Emit(RuntimeByteCodeJavaType.FinishContext context, RuntimeJavaType wrapper, CodeEmitter ilgen, ClassFile classFile, int i, ClassFile.Method.Instruction[] code, InstructionFlags[] flags)
46 {
47 if (i >= 3
48 && (flags[i - 0] & InstructionFlags.BranchTarget) == 0
49 && (flags[i - 1] & InstructionFlags.BranchTarget) == 0
50 && (flags[i - 2] & InstructionFlags.BranchTarget) == 0
51 && (flags[i - 3] & InstructionFlags.BranchTarget) == 0
52 && code[i - 1].NormalizedOpCode == NormalizedByteCode.__ldc_nothrow
53 && code[i - 2].NormalizedOpCode == NormalizedByteCode.__ldc
54 && code[i - 3].NormalizedOpCode == NormalizedByteCode.__ldc)
55 {
56 // we now have a structural match, now we need to make sure that the argument values are what we expect
57 RuntimeJavaType tclass = classFile.GetConstantPoolClassType(code[i - 3].Arg1);
58 RuntimeJavaType vclass = classFile.GetConstantPoolClassType(code[i - 2].Arg1);
59 string fieldName = classFile.GetConstantPoolConstantString(code[i - 1].Arg1);
60 if (tclass == wrapper && !vclass.IsUnloadable && !vclass.IsPrimitive && !vclass.IsNonPrimitiveValueType)
61 {
62 RuntimeJavaField field = wrapper.GetFieldWrapper(fieldName, vclass.SigName);
63 if (field != null && !field.IsStatic && field.IsVolatile && field.DeclaringType == wrapper && field.FieldTypeWrapper == vclass)
64 {
65 // everything matches up, now call the actual emitter
66 ilgen.Emit(OpCodes.Pop);
67 ilgen.Emit(OpCodes.Pop);
68 ilgen.Emit(OpCodes.Pop);
69 ilgen.Emit(OpCodes.Newobj, context.GetAtomicReferenceFieldUpdater(field));
70 return true;
71 }
72 }
73 }
74 return false;
75 }
76
77 internal static void EmitImpl(RuntimeContext context, TypeBuilder tb, FieldInfo field)
78 {
79 EmitCompareAndSet(context, "compareAndSet", tb, field);
80 EmitGet(context, tb, field);
81 EmitSet(context, "set", tb, field);
82 }
83
84 private static void EmitCompareAndSet(RuntimeContext context, string name, TypeBuilder tb, FieldInfo field)
85 {
86 MethodBuilder compareAndSet = tb.DefineMethod(name, MethodAttributes.Public | MethodAttributes.Virtual, context.Types.Boolean, new Type[] { context.Types.Object, context.Types.Object, context.Types.Object });
87 ILGenerator ilgen = compareAndSet.GetILGenerator();
88 ilgen.Emit(OpCodes.Ldarg_1);
89 ilgen.Emit(OpCodes.Castclass, field.DeclaringType);
90 ilgen.Emit(OpCodes.Ldflda, field);
91 ilgen.Emit(OpCodes.Ldarg_3);
92 ilgen.Emit(OpCodes.Castclass, field.FieldType);
93 ilgen.Emit(OpCodes.Ldarg_2);
94 ilgen.Emit(OpCodes.Castclass, field.FieldType);
95 ilgen.Emit(OpCodes.Call, MakeCompareExchange(context, field.FieldType));
96 ilgen.Emit(OpCodes.Ldarg_2);
97 ilgen.Emit(OpCodes.Ceq);
98 ilgen.Emit(OpCodes.Ret);
99 }
100
101 internal static MethodInfo MakeCompareExchange(RuntimeContext context, Type type)
102 {
103 return context.InterlockedMethods.CompareExchangeOfT.MakeGenericMethod(type);
104 }
105
106 static void EmitGet(RuntimeContext context, TypeBuilder tb, FieldInfo field)
107 {
108 MethodBuilder get = tb.DefineMethod("get", MethodAttributes.Public | MethodAttributes.Virtual, context.Types.Object, new Type[] { context.Types.Object });
109 ILGenerator ilgen = get.GetILGenerator();
110 ilgen.Emit(OpCodes.Ldarg_1);
111 ilgen.Emit(OpCodes.Castclass, field.DeclaringType);
112 ilgen.Emit(OpCodes.Volatile);
113 ilgen.Emit(OpCodes.Ldfld, field);
114 ilgen.Emit(OpCodes.Ret);
115 }
116
117 static void EmitSet(RuntimeContext context, string name, TypeBuilder tb, FieldInfo field)
118 {
119 MethodBuilder set = tb.DefineMethod(name, MethodAttributes.Public | MethodAttributes.Virtual, context.Types.Void, new Type[] { context.Types.Object, context.Types.Object });
120 CodeEmitter ilgen = context.CodeEmitterFactory.Create(set);
121 ilgen.Emit(OpCodes.Ldarg_1);
122 ilgen.Emit(OpCodes.Castclass, field.DeclaringType);
123 ilgen.Emit(OpCodes.Ldarg_2);
124 ilgen.Emit(OpCodes.Castclass, field.FieldType);
125 ilgen.Emit(OpCodes.Volatile);
126 ilgen.Emit(OpCodes.Stfld, field);
127 ilgen.EmitMemoryBarrier();
128 ilgen.Emit(OpCodes.Ret);
129 ilgen.DoEmit();
130 }
131
132 }
133
135 {
136
137 readonly RuntimeContext context;
138
139 internal readonly MethodInfo AddInt32;
140 internal readonly MethodInfo CompareExchangeInt32;
141 internal readonly MethodInfo CompareExchangeInt64;
142 internal readonly MethodInfo CompareExchangeOfT;
143 internal readonly MethodInfo ExchangeOfT;
144
150 {
151 this.context = context;
152
153 var type = context.Resolver.ResolveCoreType(typeof(System.Threading.Interlocked).FullName).AsReflection();
154 AddInt32 = type.GetMethod("Add", [context.Types.Int32.MakeByRefType(), context.Types.Int32]);
155 CompareExchangeInt32 = type.GetMethod("CompareExchange", new Type[] { context.Types.Int32.MakeByRefType(), context.Types.Int32, context.Types.Int32 });
156 CompareExchangeInt64 = type.GetMethod("CompareExchange", new Type[] { context.Types.Int64.MakeByRefType(), context.Types.Int64, context.Types.Int64 });
157 foreach (MethodInfo m in type.GetMethods())
158 {
159 if (m.IsGenericMethodDefinition)
160 {
161 switch (m.Name)
162 {
163 case "CompareExchange":
164 CompareExchangeOfT = m;
165 break;
166 case "Exchange":
167 ExchangeOfT = m;
168 break;
169 }
170 }
171 }
172
173 }
174 }
175
176}
IKVM.Reflection.Type Type
IKVM.Reflection.FieldInfo FieldInfo
IKVM.Reflection.MethodInfo MethodInfo
IKVM.Runtime.ClassFile.Method.InstructionFlags InstructionFlags
Definition atomic.cs:37
CodeEmitter Create(MethodBuilder mb)
Creates a new instance.
InterlockedMethods(RuntimeContext context)
Initializes a new instance.
Definition atomic.cs:149
Maintains services relevant to an instane of the IKVM runtime.
CodeEmitterFactory CodeEmitterFactory
Gets the CodeEmitterFactory associated with this instance of the runtime.
Types Types
Gets the Types associated with this instance of the runtime.
InterlockedMethods InterlockedMethods
Gets the InterlockedMethods associated with this instance of the runtime.
ISymbolResolver Resolver
Gets the ISymbolResolver associated with this instance of the runtime.