Can someone explain the source code for lowlevellock.h

-1

I understand the first bit indicates lock
what does the atomic_increment do? what does this line do "atomic_add_zero (mutex, 0x80000000))" why do we need to check atomic_bit_test_set again in while loop?

and what is the basic idea for this? Thanks!

 static inline void
 __generic_mutex_lock (int *mutex)
 {
   unsigned int v;

   if (atomic_bit_test_set (mutex, 31) == 0)
     return;
 
   atomic_increment (mutex);
 
   while (1)
     {
       if (atomic_bit_test_set (mutex, 31) == 0)
         {
           atomic_decrement (mutex);
           return;
         }
       v = *mutex;
       if (v >= 0)
         continue;

       lll_futex_wait (mutex, v,
                       // XYZ check mutex flag
                       LLL_SHARED);
     }
 }

 
 static inline void
 __generic_mutex_unlock (int *mutex)
 {
   if (atomic_add_zero (mutex, 0x80000000))
     return;
 
   lll_futex_wake (mutex, 1,
                   // XYZ check mutex flag
                   LLL_SHARED);
}
c
linux
operating-system
locking
mutex
asked on Stack Overflow Jul 20, 2020 by hounangogogo • edited Jul 20, 2020 by ndim

1 Answer

1

It increments the mutex as an atomic operation. That means that it will be performed in a way that no other thread may access it during the operation. It avoids race conditions.

answered on Stack Overflow Jul 20, 2020 by klutt

User contributions licensed under CC BY-SA 3.0