tx_thread_sleep not sleeping for desired time

Michael Fowler 131 Reputation points
2023-03-16T17:37:38.53+00:00

Hello,

I have the following line, assert(TX_SUCCESS==tx_thread_sleep(2)); I can see that the thread is generally not sleeping at all. There are no calls to tx_thread_wait_abort in the code. What might be causing the thread to ignore the sleep?

Azure RTOS
Azure RTOS
An Azure embedded development suite including a small but powerful operating system for resource-constrained devices.
331 questions
{count} votes

3 answers

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 14,831 Reputation points Microsoft Employee
    2023-03-16T18:50:53.12+00:00

    Hi @Michael Fowler thank you for posting this question here. The documentation states that the tx_thread_sleep is using timer_ticks as an input parameter which is of type ULONG.

    UINT tx_thread_sleep(ULONG timer_ticks);
    
    

    I believe the value 2 set to the function does not translate to 2 seconds. I could not find any information on the documentation that denotes how the timer_ticks denote time. I suspect the timer_ticks might be in milliseconds. I will check this with our team and provide more information on this.

    The example provided in the section https://video2.skills-academy.com/en-us/azure/rtos/threadx/chapter4#example-66 uses the value 100 as an input parameter for the function. Please refer the below code

    UINT status;
    
    /* Make the calling thread sleep for 100
    timer-ticks. */
    status = tx_thread_sleep(100);
    
    /* If status equals TX_SUCCESS, the currently running
    application thread slept for the specified number of
    timer-ticks. */
    
    

    Can you try setting a higher value such as 2000 and see if you notice the same behavior in the code?

    Please let us know if the above feedback helps.


  2. Scott Azure RTOS 4,051 Reputation points
    2023-03-31T15:40:24.5466667+00:00

    Hi Michael,

    How can you tell if the thread is sleeping or not? What is the call to tx_thread_sleep returning?

    Do you have a periodic timer interrupt set up (something like the SysTick in the Cortex-M architecture) that calls _tx_timer_interrupt?

    0 comments No comments

  3. QuantumCache 20,266 Reputation points
    2023-04-04T16:09:20.8033333+00:00

    Hi Michael, Another possibility is that the thread is being blocked by a synchronization object. In this case, you may want to check if the thread is waiting on any synchronization objects and ensure that they are being released properly.

    #include "tx_api.h"
    
    #define THREAD_STACK_SIZE 1024
    #define THREAD_PRIORITY 5
    
    UINT thread_sleep_demo();
    
    // Thread entry function
    VOID thread_entry(ULONG initial_input);
    
    TX_THREAD my_thread;
    UCHAR my_thread_stack[THREAD_STACK_SIZE];
    
    int main()
    {
        UINT status;
    
        // Initialize the RTOS
        tx_kernel_enter();
    
        // Create the thread
        status = tx_thread_create(&my_thread, "MyThread", thread_entry, 0, my_thread_stack, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
    
        // Check if the thread was created successfully
        if (status != TX_SUCCESS)
        {
            // Handle the error
        }
    
        // Start the RTOS scheduler
        tx_kernel_enter();
    
        return 0;
    }
    
    VOID thread_entry(ULONG initial_input)
    {
        while (1)
        {
            // Perform some tasks
    
            // Sleep for 2 ticks
            UINT sleep_status = tx_thread_sleep(2);
    
            // Check if the sleep was successful
            if (sleep_status != TX_SUCCESS)
            {
                // Handle the error
            }
        }
    }
    
    

    Verify that the system tick is configured correctly and the tick timer is running properly. Also verify the thread priorities. I would also like to review interrupt handlers and ensure that they are correctly implemented and not unintentionally waking up the sleeping thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.