Atomic operations on multiple CPUs

Source: Internet
Author: User

About single CPU, multi-CPU on the atomic operation from http://software.intel.com/zh-cn/blogs/2010/01/14/cpucpu? Cid = Sw: prccsdn956

The so-called atomic operation is "an uninterrupted operation or a series of operations ".

Hardware-level atomic operations:
In a single processor system (uniprocessor), operations that can be completed in a single command can be considered as "Atomic operations", because interruptions can only occur between commands. This is also why test_and_set, test_and_clear and other commands are introduced in some CPU command systems for the critical resource mutex.

The symmetric multi-processor structure is different. Because multiple processors in the system run independently, operations that can be completed in a single command may be affected.

On the X86 platform, the CPU provides a means to lock the bus during command execution. There is a lead on the CPU chip # hlock pin. If the assembly language program adds the prefix "Lock" before a command ", the compiled machine code lowers the potential of the # hlock pin when the CPU executes this command until the end of this command, thereby locking the bus, in this way, other CPUs on the same bus cannot access the memory temporarily through the bus, ensuring the atomicity of this command in a multi-processor environment.
Software-level atomic operations:
Software-level atomic operations depend on hardware atomic operations.
For Linux, the kernel provides two sets of atomic operation interfaces: one for integer operations and the other for individual bits.
2.1. Atomic Integer Operation
Atomic operations for integers can only process atomic_t data. The Int type of C language is not used here, mainly because:

1) Let the atomic function only accept the atomic_t type operand. It can ensure that the atomic operation is only used with this special type of data.

2) use the atomic_t type to ensure that the compiler does not optimize access to the corresponding values.

3) The atomic_t type can be used to shield Data Types in different architectures. Although the integer data on all machines supported by Linux is 32 bits, the code using atomic_t can only use this type of data as 24 bits. This restriction is entirely because the implementation of atomic operations is different from that of other systems in the structure of the iSCSI system: A 32-bit int-type low 8-bit locks are embedded, because the iSCSI architecture lacks command-level support for atomic operations, this lock can only be used to avoid concurrent access to atomic data.

The most common use of atomic integer operations is to implement counters. The atomic Integer Operation list is defined in. Atomic operations are usually restrained functions, which are often implemented through Embedded Assembly commands. If a function is atomic, it is often defined as a macro.

When writing the kernel, the operation is also simple:

Atomic_t use_cnt;

Atomic_set (& use_cnt, 2 );

Atomic_add (4, & use_cnt );

Atomic_inc (use_cnt );

2.2. atomicity and Sequence

Atomicity ensures that commands are not interrupted during execution, either fully or not executed at all. The sequence ensures that even if two or more commands appear in an independent execution thread or even on an independent processor, they should be executed in a proper order.

2.3. Atomic bit operation

The atomic bit operation is defined in the file. It is strange that bitwise operation functions operate on normal memory addresses. In most cases, the atomic bit operation accesses a long memory, so the bit number is between 0 and 31 (between 0 and 63 on 64-bit machines), but there is no limit on the range of the bit number.

To write the kernel code, you only need to give the operation function a pointer to the data you want, and then you can perform bitwise operations:

Unsigned long word = 0;

Set_bit (0, & Word);/* 0th bits are set */

Set_bit (1, & Word);/* 1st bits are set */

Clear_bit (1, & Word);/* The 1st bits are cleared */

Change_bit (0, & Word);/* flip 0th bits */

Why focus on atomic operations?
1) When we confirm that an operation is atomic, in a multi-threaded environment, we can avoid simply adding a lock with high performance overhead to the perimeter to protect this operation.
2) with atomic operations, we can implement mutex locks.
3) With the help of mutex lock, we can change some column operations into atomic operations.

Is x ++ an atomic operation in gnu c?
No. X ++ is completed by three commands. X ++ is not an atomic operation under a single CPU.
Corresponds to 3 Assembly commands
Movl X, % eax
Addl $1, % eax
Movl % eax, X
Corresponds
++ X;
004232fa mov eax, dword ptr [x]
004232fd add eax, 1
00423300 mov dword ptr [X], eax
There are still three commands.
Therefore, ++ X and X ++ are not atomic operations. The steps include getting x values from memory into registers, adding registers, and writing values into memory.

How to Implement the atomicity of X ++?
On a single processor, if multi-thread scheduling is disabled when x ++ is executed, the atom can be implemented. Because the multi-thread concurrency of a single process is pseudo-concurrency.
On a multi-processor, the lock function provided by the CPU is required. Lock bus. Read, modify, and write back the memory to prohibit other CPUs from accessing the bus. At the same time, I estimate that the OS will not schedule the current thread when the lock command is used to lock the bus. If it is transferred, it will be troublesome.

The possible causes of problems in a multi-processor system are:
If you do not use the lock command prefix to lock the bus, Other Processors may encounter exceptions or interruptions during a memory access period, and the unwritten address may be modified during exception handling, in this way, when the INC operation is complete, invalid data will be generated (overwrite the previous modification ).

Spinlock is used for CPU synchronization, and its implementation is based on the instructions of the CPU to lock the data bus.
When a CPU locks the data bus, it reads a memory unit (spinlock_t) to determine whether the spinlock has been locked by another CPU. if no, it is written into a specific value, indicating that the lock is successful, and then return. if yes, it will repeat the above operation until the operation is successful, or the number of spin times exceeds a set value. commands that lock the data bus can only ensure that the CPU occupies the data bus.
A single CPU can use a spinlock, but the data bus does not need to be locked.

When a single CPU is locked, if it fails, it will not sleep and will continue to be tried. If it is a single CPU, it will make other processes unable to be moved.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.