是否可以在运行时基于 CPU 架构编写条件代码?

发布于 2024-12-11 10:13:23 字数 357 浏览 0 评论 0 原文

我正在使用 .Net 4.5(预览版...4 对于这个问题来说就可以了)。我正在做线程工作。

根据我的研究,我知道 x86 CPU 具有强大的内存模型,这意味着写入不会被重新排序。这使得释放锁变得安全。对于具有较弱内存模型的 Itanium CPU 来说,情况并非如此。

我了解易失性、内存屏障和执行重排序原则。

理想情况下,如果 CPU 是 Itanium,则在关键点插入内存屏障,但如果 CPU 是 x86,则不需要。是否可以动态地执行此操作,就像 JIT 处理的运行时编译器指令一样?

如果没有,我意识到我将需要为两个平台进行单独的构建。在这种情况下,在没有 2 组 C# 文件的情况下,最优雅的方法是什么,而只是简单地更改目标?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送舟行 2024-12-18 10:13:23

回答你的主要问题;我认为目前不可能将 CIL 指令有条件地编译为基于平台的机器指令(除了 JIT 编译器中内置的指令)。

从一组源代码创建两个(或更多)版本的主要工具仍然是 预处理器指令

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.

世态炎凉 2024-12-18 10:13:23

我不知道这是否对您有帮助,但是这个答案中描述了几个选项:

在另一个答案中,有人提供了以下示例代码(来自 Paint.NET)的链接来确定操作系统架构。您可以进行细微调整以包含 IA64 检查:

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文