title: How are the HAL lock and unlock functions used and why?
body: <p>I'm trying to understand code which is written by another programmer. It used <a href="http://en.wikipedia.org/wiki/I%C2%B2C" rel="noreferrer">I²C</a> communication to write data on the EEPROM of an STM32 microcontroller.</p>
  
  <p>Generally I understood how his code works, but I can't understand why he used the <code>HAL_LOCK</code> and <code>HAL_UNCLOCK</code> functions.</p>
  
  <p>These is the code of these methods:</p>
  
  <pre><code>typedef enum
  {
      HAL_UNLOCKED = 0x00U,
      HAL_LOCKED   = 0x01U
  } HAL_LockTypeDef;
  
  
  #if (USE_RTOS == 1)
  
      /* Reserved for future use */
      #error "USE_RTOS should be 0 in the current HAL release"
  
  #else
  
    #define __HAL_LOCK(__HANDLE__)                 \
        do{                                        \
            if((__HANDLE__)-&gt;Lock == HAL_LOCKED)   \
            {                                      \
               return HAL_BUSY;                    \
            }                                      \
            else                                   \
            {                                      \
               (__HANDLE__)-&gt;Lock = HAL_LOCKED;    \
            }                                      \
        } while (0)
  
  
    #define __HAL_UNLOCK(__HANDLE__)              \
        do{                                       \
            (__HANDLE__)-&gt;Lock = HAL_UNLOCKED;    \
        } while (0)
  </code></pre>
  
  <p>In which cases can these methods be used?</p>