Pergunta

I'm using .Net 4.5 (preview... 4 is fine for the purposes of this question). I'm doing threading work.

Based on my studies, I know that x86 CPUs have a strong memory model, which means writes won't be reordered. This makes releasing locks safe. This is not true for Itanium CPUs, which have a weak memory model.

I understand volatile, memory barriers, and execution reordering principles.

What I need ideally is to insert memory barriers at key points if the CPU is Itanium, but not if it's x86. Is it possible to do this dynamically, as in have runtime compiler directives that the JIT processes?

If not, I realise I will need to have separate builds for the two platforms. In that case, what is the most elegant way to do this without having 2 sets of C# files, but rather simply changing the target?

Foi útil?

Solução

In answer to your main question; I don't think it's currently possible to have CIL instructions conditionally compiled to machine instructions based on platform (other than what's been baked into the JIT compiler).

Your primary tool for creating two (or more) builds from one set of source is still preprocessor directives.

Outras dicas

I have no idea if this will help you or not, but there are a couple of options described in this answer:

In another answer, somebody provided a link to the following sample code (from Paint.NET) to determine OS architecture. You could make a minor adjustment to include the IA64 check:

private enum Platform
{
    X86,
    X64,
    Unknown
}

internal const ushort PROCESSOR_ARCHITECTURE_INTEL = 0;
internal const ushort PROCESSOR_ARCHITECTURE_IA64 = 6;
internal const ushort PROCESSOR_ARCHITECTURE_AMD64 = 9;
internal const ushort PROCESSOR_ARCHITECTURE_UNKNOWN = 0xFFFF;

[StructLayout(LayoutKind.Sequential)]
internal struct SYSTEM_INFO
{
    public ushort wProcessorArchitecture;
    public ushort wReserved;
    public uint dwPageSize;
    public IntPtr lpMinimumApplicationAddress;
    public IntPtr lpMaximumApplicationAddress;
    public UIntPtr dwActiveProcessorMask;
    public uint dwNumberOfProcessors;
    public uint dwProcessorType;
    public uint dwAllocationGranularity;
    public ushort wProcessorLevel;
    public ushort wProcessorRevision;
};

[DllImport("kernel32.dll")]
internal static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);        

private static Platform GetPlatform()
{
    SYSTEM_INFO sysInfo = new SYSTEM_INFO();
    GetNativeSystemInfo(ref sysInfo);

    switch (sysInfo.wProcessorArchitecture)
    {
        case PROCESSOR_ARCHITECTURE_AMD64:
            return Platform.X64;

        case PROCESSOR_ARCHITECTURE_INTEL:
            return Platform.X86;

        default:
            return Platform.Unknown;
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top