csynchronizationsemaphorevolatilertems

Are variables updated by signal handlers optimized out when using RTEMS semaphore synchronization?


Let's say that the function isr_callback() is called on hardware interrupts. If my_function() sets the variable data to 0, and waits for tx_complete_semaphore, will the variable data be updated to 1 in my_function() when tx_complete_semaphore is released by isr_callback()? Or does the variable data have to be qualified as volatile to be updated properly in my_function()?

static int data;
static rtems_id tx_complete_semaphore;
void isr_callback(void)
{
  data = 1;
  /* interrupts as disabled here */
  rtems_semaphore_release(tx_complete_semaphore);
}
  
void my_function(void)
{
  data = 0;
  /* data will be 0 here */
  printf("data is %i", data)
  /* Interrupts are enabled here */
  rtems_semaphore_obtain(tx_complete_semaphore,
                         RTEMS_WAIT,
                         RTEMS_NO_TIMEOUT);
  /* what is the value of data here? */
  printf("data is %i", data);
}

Solution

  • No, the qualifier 'volatile' is not needed here since RTEMS semaphore synchronization makes sure that the variable data is correctly updated in my_function().

    I checked the generated asssembler code for two cases, one where data was volatile, and one where data was not volatile. In both cases, data was loaded after obtaining the semaphore, but only in the first case data was loaded before obtaining the semaphore.