Deep understanding of how mutex (mutex) and conditional variable (CONDVAR) work together in the Solaris kernel

Source: Internet
Author: User

Writing kernel modules on Solaris always uses a mutex (mutex) and condition variable (condvar), Time flies Riyuerusuo, the Solaris ship said sinking is sinking, the mood is not too good at the moment (ORZ). Every time a young talented colleague (such as Letty) asked how the mutex and CV worked together, I could never give a very clear explanation. Until today, after reading the source code of Cv_wait (), I can finally give them a clear answer.

Solaris source code can not be publicly pasted out, fortunately there are OpenSolaris successor Illumos. First paste the source of the cv_wait (), and then talk about the mutex (mutex) and the condition variable (condvar) work together.

185 /*186 * Block on the indicated condition variable and release the187 * Associated Kmutex while blocked.188*/189 void theCv_wait (kcondvar_t *CVP, kmutex_t *MP)191 {192     if(PANICSTR)193             return;194ASSERT (!quiesce_active);195196ASSERT (Curthread->t_schedflag &ts_dont_swap);197Thread_lock (Curthread);/*Lock the thread*/198Cv_block ((condvar_impl_t *) CVP);199Thread_unlock_nopreempt (Curthread);/*Unlock the Waiters field*/ $Mutex_exit (MP);201Swtch ();202Mutex_enter (MP);203}

Note: 200-202 lines, explain later.

First, a typical example of using mutexes and CVS is such a drop,

1 Statickmutex_t Mutex;2 Statickcondvar_t CONDV;3 StaticUnsignedintReady =0;4 5 /*1. Init Mutex and CV*/6Mutex_init (&mutex, NULL, mutex_driver, NULL);7Cv_init (&condv, NULL, cv_driver, NULL);8 9 /*2. Use Mutex and CV*/Ten  One   /*Thread 1*/|/*Thread 2*/ AMutex_enter (&mutex); | Mutex_enter (&mutex); -    while(Ready = =0)           | Ready =1; -Cv_wait (&CONDV, &mutex); | Cv_signal (&CONDV); theMutex_exit (&mutex); | Mutex_exit (&mutex); -  - /*3. Destroy mutex and CV*/ -Cv_destroy (&CONDV); +Mutex_destroy (&mutex);
    1. In thread 1, the mutex is obtained first, then the condition (ready==0) is determined, and if so, call Cv_wait (&CONDV, &mutex) into sleep;
    2. In thread 2, the mutex is obtained first, then the ready is assigned a value of 1, and call Cv_signal (&CONDV) wakes the sleeping Thread1 while releasing the held mutex;
    3. Once the Thread1 wakes up, it will re-determine whether the condition (ready==0) is true, and if not, release the mutex. (Of course, if it is set up, it will go to sleep again, waiting for the next wake up)

Then , the question (Letty asked me ) came : since thread 1 got a mutex in L12 and then slept through, then how can thread 2 get a mutex?

This is a good question, a really good question! (Ps. Lao Mei said that every time she was asked to live.

Before today I could not answer, or can only reckoned to answer that "can only see concrete implementation." Well, I'm really finished today. In the source code of Cv_wait (),

 $     Mutex_exit (MP); 201     Swtch (); 202    Mutex_enter (MP);

The No. 200 row of the mutex is released, then the No. 201 line goes to sleep, waiting to be awakened. Once awakened, the mutex is re-acquired on line No. 202.

That is, release the mutex before bedtime and wake up to get the mutex. This way other threads have the opportunity to work after the mutex and wake up the sleeping thread after the work is done.

This also explains why the cv_wait () function is not only a parameter kcondvar_t *CVP, but also contains the parameter kmutex_t *mp.

In writing, I would like to summarize with one sentence,"The source code is the final world." if you want to be a very good programmer, please remember RTFSC.

Ps:

1. If you want to further understand why the conditional variable and the mutex are used to ensure synchronization, please Google yourself or read the OS-related book.

2. All the co-operation principles on mutexes and condition variables should be consistent, such as POSIX Pthread_mutex and pthread_cond,linux kernel mutexes and competion variable.

Deep understanding of how mutex (mutex) and conditional variable (CONDVAR) work together in the Solaris kernel

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.