AzureMXChip/MXCHIPAZ3166/core/lib/threadx/ports/win32/vs_2019/src/tx_thread_schedule.c
VOID   _tx_thread_schedule(VOID)
{


    /* Loop forever.  */
    while(1)
    {

        /* Wait for a thread to execute and all ISRs to complete.  */
        while(1)
        {


            /* Enter Win32 critical section.  */
            _tx_win32_critical_section_obtain(&_tx_win32_critical_section);

            /* Debug entry.  */
            _tx_win32_debug_entry_insert("SCHEDULE-wake_up", __FILE__, __LINE__);

            /* Determine if there is a thread ready to execute AND all ISRs 
               are complete.  */
            if ((_tx_thread_execute_ptr != TX_NULL) && (_tx_thread_system_state == 0))
            {

                /* Get out of this loop and schedule the thread!  */
                break;
            }
            else
            {

                /* Leave the critical section.  */
                _tx_win32_critical_section_release(&_tx_win32_critical_section);

                /* Now sleep so we don't block forever.  */
                Sleep(2);
            }
        }
        
        /* Yes! We have a thread to execute. Note that the critical section is already 
           active from the scheduling loop above.  */

        /* Setup the current thread pointer.  */
        _tx_thread_current_ptr =  _tx_thread_execute_ptr;

        /* Increment the run count for this thread.  */
        _tx_thread_current_ptr -> tx_thread_run_count++;

        /* Setup time-slice, if present.  */
        _tx_timer_time_slice =  _tx_thread_current_ptr -> tx_thread_time_slice;

        /* Determine how the thread was suspended.  */
        if (_tx_thread_current_ptr -> tx_thread_win32_suspension_type)
        {

            /* Debug entry.  */
            _tx_win32_debug_entry_insert("SCHEDULE-resume_thread", __FILE__, __LINE__);

            /* Pseudo interrupt suspension.  The thread is not waiting on
               its run semaphore.  */
            ResumeThread(_tx_thread_current_ptr -> tx_thread_win32_thread_handle);
        }
        else
        {

            /* Debug entry.  */
            _tx_win32_debug_entry_insert("SCHEDULE-release_sem", __FILE__, __LINE__);

            /* Let the thread run again by releasing its run semaphore.  */
            ReleaseSemaphore(_tx_thread_current_ptr -> tx_thread_win32_thread_run_semaphore, 1, NULL);
        }

        /* Debug entry.  */
        _tx_win32_debug_entry_insert("SCHEDULE-self_suspend_sem", __FILE__, __LINE__);

        /* Exit Win32 critical section.  */
        _tx_win32_critical_section_release(&_tx_win32_critical_section);

        /* Now suspend the main thread so the application thread can run.  */
        WaitForSingleObject(_tx_win32_scheduler_semaphore, INFINITE);
    }
}
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_thread_schedule                               Win32/Visual      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function waits for a thread control block pointer to appear in */
/*    the _tx_thread_execute_ptr variable.  Once a thread pointer appears */
/*    in the variable, the corresponding thread is resumed.               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    ReleaseSemaphore                      Win32 release semaphore       */
/*    ResumeThread                          Win32 resume thread           */
/*    Sleep                                 Win32 thread sleep            */
/*    WaitForSingleObject                   Win32 wait on a semaphore     */
/*    _tx_win32_critical_section_obtain     Obtain critical section       */
/*    _tx_win32_critical_section_release    Release critical section      */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _tx_initialize_kernel_enter          ThreadX entry function         */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  09-30-2020     William E. Lamie         Initial Version 6.1           */
/*                                                                        */
/**************************************************************************/
FUNCTION                                               RELEASE



William E. Lamie, Microsoft Corporation

DESCRIPTION

This function waits for a thread control block pointer to appear in
the _tx_thread_execute_ptr variable.  Once a thread pointer appears
in the variable, the corresponding thread is resumed.

INPUT

None

OUTPUT

None

CALLS

ReleaseSemaphore                      Win32 release semaphore
ResumeThread                          Win32 resume thread
Sleep                                 Win32 thread sleep
WaitForSingleObject                   Win32 wait on a semaphore
_tx_win32_critical_section_obtain     Obtain critical section
_tx_win32_critical_section_release    Release critical section

CALLED BY



RELEASE HISTORY

DATE              NAME                      DESCRIPTION


FUNCTION RELEASE William E . Lamie Microsoft Corporation DESCRIPTION This function waits for a thread control block pointer to appear in the _tx_thread_execute_ptr variable . Once a thread pointer appears in the variable the corresponding thread is resumed . INPUT None OUTPUT None CALLS ReleaseSemaphore Win32 release semaphore ResumeThread Win32 resume thread Sleep Win32 thread sleep WaitForSingleObject Win32 wait on a semaphore _tx_win32_critical_section_obtain Obtain critical section _tx_win32_critical_section_release Release critical section CALLED BY RELEASE HISTORY DATE NAME DESCRIPTION
VOID   _tx_thread_schedule(VOID)
{
    while(1)
    {
        while(1)
        {
            _tx_win32_critical_section_obtain(&_tx_win32_critical_section);
            _tx_win32_debug_entry_insert("SCHEDULE-wake_up", __FILE__, __LINE__);
            if ((_tx_thread_execute_ptr != TX_NULL) && (_tx_thread_system_state == 0))
            {
                break;
            }
            else
            {
                _tx_win32_critical_section_release(&_tx_win32_critical_section);
                Sleep(2);
            }
        }
        _tx_thread_current_ptr =  _tx_thread_execute_ptr;
        _tx_thread_current_ptr -> tx_thread_run_count++;
        _tx_timer_time_slice =  _tx_thread_current_ptr -> tx_thread_time_slice;
        if (_tx_thread_current_ptr -> tx_thread_win32_suspension_type)
        {
            _tx_win32_debug_entry_insert("SCHEDULE-resume_thread", __FILE__, __LINE__);
            ResumeThread(_tx_thread_current_ptr -> tx_thread_win32_thread_handle);
        }
        else
        {
            _tx_win32_debug_entry_insert("SCHEDULE-release_sem", __FILE__, __LINE__);
            ReleaseSemaphore(_tx_thread_current_ptr -> tx_thread_win32_thread_run_semaphore, 1, NULL);
        }
        _tx_win32_debug_entry_insert("SCHEDULE-self_suspend_sem", __FILE__, __LINE__);
        _tx_win32_critical_section_release(&_tx_win32_critical_section);
        WaitForSingleObject(_tx_win32_scheduler_semaphore, INFINITE);
    }
}
VOID _tx_thread_schedule ( VOID ) { while ( 1 ) { while ( 1 ) { _tx_win32_critical_section_obtain ( & _tx_win32_critical_section ) ; _tx_win32_debug_entry_insert ( " " , __FILE__ , __LINE__ ) ; if ( ( _tx_thread_execute_ptr != TX_NULL ) && ( _tx_thread_system_state == 0 ) ) { break ; } else { _tx_win32_critical_section_release ( & _tx_win32_critical_section ) ; Sleep ( 2 ) ; } } _tx_thread_current_ptr = _tx_thread_execute_ptr ; _tx_thread_current_ptr -> tx_thread_run_count ++ ; _tx_timer_time_slice = _tx_thread_current_ptr -> tx_thread_time_slice ; if ( _tx_thread_current_ptr -> tx_thread_win32_suspension_type ) { _tx_win32_debug_entry_insert ( " " , __FILE__ , __LINE__ ) ; ResumeThread ( _tx_thread_current_ptr -> tx_thread_win32_thread_handle ) ; } else { _tx_win32_debug_entry_insert ( " " , __FILE__ , __LINE__ ) ; ReleaseSemaphore ( _tx_thread_current_ptr -> tx_thread_win32_thread_run_semaphore , 1 , NULL ) ; } _tx_win32_debug_entry_insert ( " " , __FILE__ , __LINE__ ) ; _tx_win32_critical_section_release ( & _tx_win32_critical_section ) ; WaitForSingleObject ( _tx_win32_scheduler_semaphore , INFINITE ) ; } }
FUNCTION                                               RELEASE
FUNCTION RELEASE
/* Loop forever.  */ /* Wait for a thread to execute and all ISRs to complete.  */ /* Enter Win32 critical section.  */ /* Debug entry.  */ /* Determine if there is a thread ready to execute AND all ISRs 
               are complete.  */ /* Get out of this loop and schedule the thread!  */ /* Leave the critical section.  */ /* Now sleep so we don't block forever.  */ /* Yes! We have a thread to execute. Note that the critical section is already 
           active from the scheduling loop above.  */ /* Setup the current thread pointer.  */ /* Increment the run count for this thread.  */ /* Setup time-slice, if present.  */ /* Determine how the thread was suspended.  */ /* Debug entry.  */ /* Pseudo interrupt suspension.  The thread is not waiting on
               its run semaphore.  */ /* Debug entry.  */ /* Let the thread run again by releasing its run semaphore.  */ /* Debug entry.  */ /* Exit Win32 critical section.  */ /* Now suspend the main thread so the application thread can run.  */