~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/runtime/atomic.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
25
using System;
 
26
using System.Collections.Generic;
 
27
#if STATIC_COMPILER
 
28
using IKVM.Reflection;
 
29
using IKVM.Reflection.Emit;
 
30
using Type = IKVM.Reflection.Type;
 
31
#else
 
32
using System.Reflection;
 
33
using System.Reflection.Emit;
 
34
#endif
 
35
using IKVM.Internal;
 
36
using InstructionFlags = IKVM.Internal.ClassFile.Method.InstructionFlags;
 
37
 
 
38
static class AtomicReferenceFieldUpdaterEmitter
 
39
{
 
40
        internal static bool Emit(DynamicTypeWrapper.FinishContext context, TypeWrapper wrapper, CodeEmitter ilgen, ClassFile classFile, int i, ClassFile.Method.Instruction[] code, InstructionFlags[] flags)
 
41
        {
 
42
                if (i >= 3
 
43
                        && (flags[i - 0] & InstructionFlags.BranchTarget) == 0
 
44
                        && (flags[i - 1] & InstructionFlags.BranchTarget) == 0
 
45
                        && (flags[i - 2] & InstructionFlags.BranchTarget) == 0
 
46
                        && (flags[i - 3] & InstructionFlags.BranchTarget) == 0
 
47
                        && code[i - 1].NormalizedOpCode == NormalizedByteCode.__ldc_nothrow
 
48
                        && code[i - 2].NormalizedOpCode == NormalizedByteCode.__ldc
 
49
                        && code[i - 3].NormalizedOpCode == NormalizedByteCode.__ldc)
 
50
                {
 
51
                        // we now have a structural match, now we need to make sure that the argument values are what we expect
 
52
                        TypeWrapper tclass = classFile.GetConstantPoolClassType(code[i - 3].Arg1);
 
53
                        TypeWrapper vclass = classFile.GetConstantPoolClassType(code[i - 2].Arg1);
 
54
                        string fieldName = classFile.GetConstantPoolConstantString(code[i - 1].Arg1);
 
55
                        if (tclass == wrapper && !vclass.IsUnloadable && !vclass.IsPrimitive && !vclass.IsNonPrimitiveValueType)
 
56
                        {
 
57
                                FieldWrapper field = wrapper.GetFieldWrapper(fieldName, vclass.SigName);
 
58
                                if (field != null && !field.IsStatic && field.IsVolatile && field.DeclaringType == wrapper && field.FieldTypeWrapper == vclass)
 
59
                                {
 
60
                                        // everything matches up, now call the actual emitter
 
61
                                        ilgen.Emit(OpCodes.Pop);
 
62
                                        ilgen.Emit(OpCodes.Pop);
 
63
                                        ilgen.Emit(OpCodes.Pop);
 
64
                                        ilgen.Emit(OpCodes.Newobj, context.GetAtomicReferenceFieldUpdater(field));
 
65
                                        return true;
 
66
                                }
 
67
                        }
 
68
                }
 
69
                return false;
 
70
        }
 
71
 
 
72
        internal static void EmitImpl(TypeBuilder tb, FieldInfo field)
 
73
        {
 
74
                EmitCompareAndSet("compareAndSet", tb, field);
 
75
                EmitGet(tb, field);
 
76
                EmitSet("set", tb, field);
 
77
        }
 
78
 
 
79
        private static void EmitCompareAndSet(string name, TypeBuilder tb, FieldInfo field)
 
80
        {
 
81
                MethodBuilder compareAndSet = tb.DefineMethod(name, MethodAttributes.Public | MethodAttributes.Virtual, Types.Boolean, new Type[] { Types.Object, Types.Object, Types.Object });
 
82
                ILGenerator ilgen = compareAndSet.GetILGenerator();
 
83
                ilgen.Emit(OpCodes.Ldarg_1);
 
84
                ilgen.Emit(OpCodes.Castclass, field.DeclaringType);
 
85
                ilgen.Emit(OpCodes.Ldflda, field);
 
86
                ilgen.Emit(OpCodes.Ldarg_3);
 
87
                ilgen.Emit(OpCodes.Castclass, field.FieldType);
 
88
                ilgen.Emit(OpCodes.Ldarg_2);
 
89
                ilgen.Emit(OpCodes.Castclass, field.FieldType);
 
90
                ilgen.Emit(OpCodes.Call, MakeCompareExchange(field.FieldType));
 
91
                ilgen.Emit(OpCodes.Ldarg_2);
 
92
                ilgen.Emit(OpCodes.Ceq);
 
93
                ilgen.Emit(OpCodes.Ret);
 
94
        }
 
95
 
 
96
        internal static MethodInfo MakeCompareExchange(Type type)
 
97
        {
 
98
                MethodInfo interlockedCompareExchange = null;
 
99
                foreach (MethodInfo m in JVM.Import(typeof(System.Threading.Interlocked)).GetMethods())
 
100
                {
 
101
                        if (m.Name == "CompareExchange" && m.IsGenericMethodDefinition)
 
102
                        {
 
103
                                interlockedCompareExchange = m;
 
104
                                break;
 
105
                        }
 
106
                }
 
107
                return interlockedCompareExchange.MakeGenericMethod(type);
 
108
        }
 
109
 
 
110
        private static void EmitGet(TypeBuilder tb, FieldInfo field)
 
111
        {
 
112
                MethodBuilder get = tb.DefineMethod("get", MethodAttributes.Public | MethodAttributes.Virtual, Types.Object, new Type[] { Types.Object });
 
113
                ILGenerator ilgen = get.GetILGenerator();
 
114
                ilgen.Emit(OpCodes.Ldarg_1);
 
115
                ilgen.Emit(OpCodes.Castclass, field.DeclaringType);
 
116
                ilgen.Emit(OpCodes.Volatile);
 
117
                ilgen.Emit(OpCodes.Ldfld, field);
 
118
                ilgen.Emit(OpCodes.Ret);
 
119
        }
 
120
 
 
121
        private static void EmitSet(string name, TypeBuilder tb, FieldInfo field)
 
122
        {
 
123
                MethodBuilder set = tb.DefineMethod(name, MethodAttributes.Public | MethodAttributes.Virtual, Types.Void, new Type[] { Types.Object, Types.Object });
 
124
                CodeEmitter ilgen = CodeEmitter.Create(set);
 
125
                ilgen.Emit(OpCodes.Ldarg_1);
 
126
                ilgen.Emit(OpCodes.Castclass, field.DeclaringType);
 
127
                ilgen.Emit(OpCodes.Ldarg_2);
 
128
                ilgen.Emit(OpCodes.Castclass, field.FieldType);
 
129
                ilgen.Emit(OpCodes.Volatile);
 
130
                ilgen.Emit(OpCodes.Stfld, field);
 
131
                ilgen.EmitMemoryBarrier();
 
132
                ilgen.Emit(OpCodes.Ret);
 
133
                ilgen.DoEmit();
 
134
        }
 
135
}