Futex (2) quick introduction to user-state mutex

Source: Internet
Author: User

In the past two days, I reviewed the distributed system and checked the futex (2) Synchronization Mechanism in Linux.

In short, futex (2) is a new synchronization mechanism, which is similar to the general mutex mechanism in terms of function. The two execution bodies that use futex (2) for synchronization must be associated with a shared memory space-the memory space can have the same or different process addresses, as long as they are mapped to the memory address in the same core-therefore, threads and processes (associated through sysvshm) can be synchronized using futex.
Futex (2) supports two operations: Wait (wait) and wake (wake ). The system interface of futex (2) is int futex (void * futex, int op, int Val, const struct timespec * timeout). The first parameter futex points to the mapped memory address, OP indicates the operation type, and timeout indicates the blocking time, while Val indicates different values. In terms of users, futex (2) has some unique features compared to pthread_mutex/pthread_condition_variable and sysv semaphore.

You can specify a condition for the futex wait operation. Pass the constant futex_wait as the parameter to futex as the value of the second op parameter. At this time, the value of the third op parameter will be used as a comparison object. When * (int *) futex = Val, the call body is blocked until the timeout time is reached, or is awakened by other execution bodies. If * (int *) futex! = Val, the lock process will be skipped directly. The system ensures the atomicity of the load-and-compare process by carefully compiled assembly code. The comparison process is completed completely in the user State. That is to say, if the values are not equal and do not meet the waiting conditions, the "System Call" will not be switched to the kernel state for execution, this reduces the loss of kernel-mode round-trip switching at least once than other Synchronization Methods. This is also the origin of the futex name (fast userspace mutex.
Futex's wake can specify the number of wake-up processes. The constant futex_wake is passed to OP to indicate the wake-up operation. The third value Val indicates the maximum number of wake-up execution bodies. Therefore, it is very flexible and convenient to use futex to implement semaphore "broadcast" and wake up a single executor.

Futex (2) and futex (4) have no sample code. I am in my code
# Include <Linux/futex. h>
And then directly use
Futex (void *) P, futex _ {Wait | wake}, 0, (struct timespec *) null)
Then I returned a bunch of errors. The error after-I/usr/src/Linux/include is that the connector cannot find the futex function implementation ?!
Download the futex-2.2.tar.gz from the kernel.org page of rusty and take a look, it is directly defined with _ syscall sys_futex
_ Syscall4 (INT, sys_futex,
Int *, futex,
Int, op,
Int, Val,
Struct timespec *, rel );
However, if I use this statement, the error is in the _ syscall4 line. I searched from Google and found that some people said that the futex interface was not officially export in the earlier kernel, and only the sys_futex system call was provided. So I tried to change the second parameter sys_futex of _ syscall to futex to pass the compilation. It seems that in my kernel (2.6.11, FC4 standard kernel), futex has been officially export, but it has not yet been introduced into glibc.

A simple post/Wait operation:

Int wait_always (void * futexpos ){
// 0 if woken by futex_wake
Return futex (futexpos, futex_wait, * (int *) futexpos, null );
}

Int wait_eq (void * futexpos, int Val ){
Return futex (futexpos, futex_wait, Val, null );
}

Int wake (void * futexpos, int num ){
// Return # processes been woken up
Return futex (futexpos, futex_wake, num, null );
}

In the above Code, the futex parameter passed to Wait _ * and signal must be: 1. the same address in the multi-thread program; or 2. It is the same address in the memory address mapped by a sysv SHM; 3. For a non-/dev/Zero file (currently only common files and/dev/zero are tested) memory ing address, where MMAP must have the prot_read permission.

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.