Semaphores and spin locks (3): Implementation of Linux semaphores

Source: Internet
Author: User
Tags semaphore
1. Definition:
Header file: <ASM/semaphore. h>
Data Type: struct semaphore
Directly create:
Void sema_init (struct semaphore * SEM, int Val);/* where Val is the initial value of the semaphore */

Secondary macro:
Declare_mutex (name);/* initialize a semaphore variable named name to 1 */
Declare_mutex_locked (name);/* initialize a semaphore variable named name to 0 */

Dynamic Allocation:
/* Used for runtime initialization */
Void init_mutex (struct semaphore * SEM );
Void init_mutex_locked (struct semaphore * SEM );

In the Linux World, the p function is called down, which means that the function reduces the semaphore value. It may put the caller in sleep state and wait until the semaphore becomes available, then grant the caller access to the protected resources. the down function has three versions:
/* Reduce the semaphore value and wait until necessary */
Void down (struct semaphore * SEM );

/* The version that can be interrupted. Commonly Used */
Void down_interruptible (struct semephore * SEM );
As a general rule, we should not use a non-interrupted version.
If this function is used, if the operation is interrupted, the function returns a non-zero value, and the caller does not own the semaphore.
Therefore, to use the function correctly, always check the return value and respond accordingly.

/* Never sleep. For example, if the semaphore is not available during the call, a non-0 value is returned immediately */
Void down_trylock (struct semaphore * SEM );

After a thread successfully calls a version of the Down function, it is called that the thread has the semaphore and can access the critical section protected by the semaphore. after the mutex operation is completed, the semaphore must be released.
The V function in Linux is up:
/* After the call is up, the caller no longer owns the semaphore */
Void up (struct semaphore * SEM );

2. Example:
We have a shared data structure:
Struct st_data
{
Char name [32];
Char data [128];
Int data_len;
};
This data structure is accessed by multiple processes at the same time.
To prevent these processes from generating a race state when accessing the structure, we add a semaphore to the bottom of the structure:
Struct st_data
{
Char name [32];/* name */
Char data [128];/* Data */
Int data_len;/* Data Length */
Struct semaphore SEM;/* semaphore */
};
Semaphores must be initialized before use and before other parts of shared data are available. therefore, we call init_mutex before assigning values to other data. Otherwise, a race state will be created, that is, some code may access them before the semaphore is ready.

St_data data;
Init_mutex (& Data-> SEM );
Setup_data (& data);/* initialize data */
...
...

Next, we must carefully check the Code to ensure that the data structure is not accessed when the semaphore is not available. For example, add the following at the beginning of data_write:
If (down_interruptible (& Data-> SEM ))
Return-erestartsys;
This is the return value of down_interruptible. If a non-0 value is returned, the operation is interrupted. in this case, the common task is to return-erestartsys. after the returned value is obtained, the kernel restarts the call from the beginning or returns the error to the user.
If-erestartsys is returned, you must first undo the modification so that the system call can correctly retry the operation. If the operation cannot be undone,-eintr should be returned, indicating that the operation is interrupted.

Whether data_write can complete other work, it must release the semaphore:
Out:
Up (& Data-> SEM );
Return retval;

Errors may occur in data_write, including memory allocation failure. In these cases, the Code executes goto out to ensure that the semaphore is correctly released.

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.