This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

what is meant by mutexes and semaphore?

Hi,

Please let know

1.what is meant by mutexes and semaphore?

2.what is the role of these in threadx?

  • As per my knowledge,

    Mutexes - it protect the resource. example : if there is an multiple thread,then it will execute the thread one by one,if that thread execution complete,then next thread start execute.

    semaphore - it will execute n number threads(depends on ram). Each thread will execute at certain or particular time.

    If anything wrong ,please explain me with real world concept it helps me to understand sweetly.

  • A mutex is indeed used to lock a resource to a single thread. If you are using some hardware periphery like a UART or I2C e.g., you would use a mutex to let one thread have exclusive access to that resource. The I2C framwork incorporates this already, you could use

    [name_of_I2C_instance].p_api->lockWait([name_of_I2C_instance].p_ctrl,TX_WAIT_FOREVER);

    and make the thread wait until it has gained access to the I2C bus to use it for whatever you desire. afterwards, you will of course have to unlock it again to give other threads the chance to use the periphery.

    A semaphore on the other hand can be used for signalling between threads or between a callback function and a thread. A simple example here is the usage of the USB framework, where a semaphore is used to let the USB thread know that a PC has been connected via the callback function 

    void ux_cdc_device0_instance_activate (VOID * cdc_instance)
    {
        /* Save the CDC instance.  */
        g_cdc = (UX_SLAVE_CLASS_CDC_ACM *) cdc_instance;
        tx_semaphore_put(&g_cdc_activate_semaphore0);
    }

    where the USB thread would wait for the sempahore g_cdc_activate_semaphore0 to get a value with

    tx_semaphore_get (&g_cdc_activate_semaphore0, TX_WAIT_FOREVER);

    and only begin to run when the semaphore has been put through the callback function.

    If there are more questions left, don't hesitate to ask.

    Kind Regards,

    Christoph

  • Hi Christoph,

    sweet explanation,i understand about mutex. In semaphore,what is that meant by "signalling between threads or callback function"? 

  • Like I already explained, you can make a thread wait for a certain event with it (like the activation of the USB CDC class).

    Another example: You are using a UART to communicate with a peripheral IC. When you used the write command of the UART driver, after the writing has completed, an event will fire in your UART callback with the code UART_EVENT_TX_COMPLETE. This signals to you that another transmission can be started. On this event, you could call a PUT into a semaphore which you can try to GET before writing more data onto the UART. Through this, the callback function signals a thread that the writing has finished and another writing can be started. To sum it up:

    //this here is the write function, which waits until the semaphore has been filled by the callback
    //the semaphore is initialized to '1' when the controller starts, so that the first transmission can be sent
    
    void write_uart5(void)
    {
    	tx_semaphore_get(&g_semaphore_uart5_tx_complete,TX_WAIT_FOREVER);
    	g_uart5.p_api->write(g_uart5.p_ctrl,(UCHAR*)SEND,send_buffer_ptr); //SEND is the data buffer
    	send_buffer_ptr=0;
    }
    
    //UART callback function, gets called whenever there is an event within the UART periphery
    void uart5_callback(uart_callback_args_t *p_args)
    {
    	switch(p_args->event)
    	{
    		case UART_EVENT_RX_CHAR:
    		case UART_EVENT_BREAK_DETECT:
    			TEL[TEL_PTR++]=(UCHAR)p_args->data;                             //TEL is the receive data buffer
    			if(TEL[TEL_PTR-1]==0x0A && TEL[TEL_PTR-2]==0x0D)				//End of Telegram
    			{
    				TEL[TEL_PTR]=0;
    				tx_queue_send(&g_queue_uart_5_rx,TEL,TX_NO_WAIT);
    				TEL_PTR=0;
    			}
    			break;
    		case UART_EVENT_TX_COMPLETE:
    			tx_semaphore_put(&g_semaphore_uart5_tx_complete);
    			break;
    		default:
    			break;
    	}
    }

  • There are a LOT of references for those types of questions on the web. This group is for Synergy-related issues on Renesas processors. ThreadX (now  Azure RTOS) is a Microsoft-supported product.

  • okay larry_c_mass