如何执行一小段C代码原子

时间:2021-06-09 01:06:06

I have this piece of C code:

我有这段C代码:

// ... many stuff here ...
if ( ((*(ptr + 0xffce)) & 3) ) {
     *(ptr + 0xffce) |= 3;
     *(ptr + 0xd415) = 1 << var;
}
// ... many stuff here ...

where some bits are getting logically linked and the result values are written to the memory.

其中一些位逻辑链接,结果值写入内存。

For my program it is critical that the CPU isn't detracted during the execution of this program part. Therefore it must be executed atomic and behave in the way that the code block is indivisible. The scheduler should only be allowed to detract the CPU before or after this if block.

对于我的程序,在执行该程序部分期间不要减损CPU是至关重要的。因此,它必须以原子方式执行,并且以代码块不可分割的方式运行。应该只允许调度程序在此if块之前或之后分解CPU。

How do I achieve this on a normal Linux ecosystem (with a user space program)?

如何在普通的Linux生态系统(使用用户空间程序)上实现这一目标?

EDIT: the comments bellow indicate that it might not be possible to execute the code block without having the CPU possibly detracted by the scheduler. The opposite question would be: is it generally possible to achieve this goal and what do I need to do for that?

编辑:下面的评论表明,如果没有CPU可能被调度程序贬低,可能无法执行代码块。相反的问题是:通常是否有可能实现这一目标,我需要做些什么呢?

1 个解决方案

#1


1  

In general, writes to HW should be placed in kernel modules. The code you're displaying (writes to hw addresses) will not work in userspace if the platform is protected by MMU or some other memory management. In theory, the user address space is actually different from kernel address space.

通常,对HW的写入应放在内核模块中。如果平台受MMU或其他内存管理保护,您正在显示的代码(写入hw地址)将无法在用户空间中工作。理论上,用户地址空间实际上与内核地址空间不同。

When in kernel mode, the solution will most likely present it self. There're many timing/protect mechanisms in the kernel: preempt_disable, spin_locks, in_atomic, interrupts, high priority kernel timers, whatever.

在内核模式下,解决方案很可能会自我呈现。内核中有许多时序/保护机制:preempt_disable,spin_locks,in_atomic,中断,高优先级内核定时器等等。

in general you can achieve what you're trying to do, by disabling all interrupts, on the given platform. This normally takes an RTOS or kernel code. (Disabling all interrupts will also prevent the scheduler from running.) However, in most platforms this is frowned upon. (With good reason.) So you have to do it, as per the platform guidelines. The "preempt_disable" is a good example on how to disable the scheduler, without disabling all else.

通常,您可以通过在给定平台上禁用所有中断来实现您尝试执行的操作。这通常需要RTOS或内核代码。 (禁用所有中断也会阻止调度程序运行。)但是,在大多数平台上,这都是不受欢迎的。 (有充分的理由。)所以你必须按照平台指南来做。 “preempt_disable”是关于如何禁用调度程序的一个很好的示例,而不会禁用所有其他内容。

#1


1  

In general, writes to HW should be placed in kernel modules. The code you're displaying (writes to hw addresses) will not work in userspace if the platform is protected by MMU or some other memory management. In theory, the user address space is actually different from kernel address space.

通常,对HW的写入应放在内核模块中。如果平台受MMU或其他内存管理保护,您正在显示的代码(写入hw地址)将无法在用户空间中工作。理论上,用户地址空间实际上与内核地址空间不同。

When in kernel mode, the solution will most likely present it self. There're many timing/protect mechanisms in the kernel: preempt_disable, spin_locks, in_atomic, interrupts, high priority kernel timers, whatever.

在内核模式下,解决方案很可能会自我呈现。内核中有许多时序/保护机制:preempt_disable,spin_locks,in_atomic,中断,高优先级内核定时器等等。

in general you can achieve what you're trying to do, by disabling all interrupts, on the given platform. This normally takes an RTOS or kernel code. (Disabling all interrupts will also prevent the scheduler from running.) However, in most platforms this is frowned upon. (With good reason.) So you have to do it, as per the platform guidelines. The "preempt_disable" is a good example on how to disable the scheduler, without disabling all else.

通常,您可以通过在给定平台上禁用所有中断来实现您尝试执行的操作。这通常需要RTOS或内核代码。 (禁用所有中断也会阻止调度程序运行。)但是,在大多数平台上,这都是不受欢迎的。 (有充分的理由。)所以你必须按照平台指南来做。 “preempt_disable”是关于如何禁用调度程序的一个很好的示例,而不会禁用所有其他内容。