Lock mutex under SMP system

Source: Internet
Author: User
Tags mutex semaphore

Mutual exclusion under the lock mutex up system under the SMP system

On a single-processor (unique process) system, any number of control paths (multiple threads or multiple processes) that are allowed to occur will have a race condition.

1) Short-term mutual exclusion: in a single processor, if the system is not allowed in the kernel state preemption, then there will be no mutex problem, only allow in the kernel preemption, that is, when the time-slice, only to appear mutually exclusive cases. This is primarily used to modify a kernel-shared data structure.

Implementation of the method: Prohibit preemption

2) mutual exclusion from interrupt handlers: mutual exclusion occurs when the process and interrupt handlers (that is, the equivalent of two control paths) access shared resources concurrently.

Method of implementation: Shutdown interrupt

3) long-term mutual exclusion: up system, test flag bit and set, also by prohibit preemption to achieve. is actually the short-term mutex policy setting plus the lock flag + process sleep wake.

mutual exclusion under the SMP system

In the case of SMP, to 1), the prohibition of preemption is not the spirit. Prohibit one CPU from being preempted, and cannot prohibit other CPU preemption. for 2), a local outage and a shutdown of all CPU interrupts occur.

Test-and-set

The classic parallel primitives are test-and-set. The test-and-set operation automatically reads a value from a memory location and writes a new value and returns the old value.

The Test-and-set primitive is sufficient for any other parallel security operation. (In fact, on some CPUs test-and-set is the only such primitive that is provided.) This means that, as long as the introduction of the Test-and-set operation Primitive in the SMP system, it can satisfy all the parallel operations under the SMP system. Below we see the mechanism spin_lock and semaphore of the SMP to prevent competition are based on this operation primitive.

Let's do it now. How Test-and-set operation Primitives are implemented under the x86 system.

On the x86 platform, the lock directive is just the right place to provide this help. (To be precise, lock is a prefix rather than a separate instruction) the lock instruction is used to lock the memory bus during subsequent instruction execution-at least for the destination memory address. Because x86 can lose value directly in memory without having to explicitly read it into a register, it is ready to perform a decrement atomic operation: The lock memory bus then immediately performs a decl operation on the memory location.

Spin_lock locks and Semaphore are based on the basic primitives. Next time again.

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.