1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** ThreadX Component                                                     */
17 /**                                                                       */
18 /**   Thread                                                              */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 #define TX_SOURCE_CODE
25 
26 
27 /* Include necessary system files.  */
28 
29 #include "tx_api.h"
30 #include "tx_thread.h"
31 #include "tx_timer.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _tx_thread_context_save                           Win32/Visual      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    William E. Lamie, Microsoft Corporation                             */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function saves the context of an executing thread in the       */
47 /*    beginning of interrupt processing.  The function also ensures that  */
48 /*    the system stack is used upon return to the calling ISR.            */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    None                                                                */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    SuspendThread                         Win32 thread suspend          */
61 /*    _tx_win32_critical_section_obtain     Obtain critical section       */
62 /*    _tx_win32_critical_section_release    Release critical section      */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    ISRs                                                                */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  09-30-2020     William E. Lamie         Initial Version 6.1           */
73 /*                                                                        */
74 /**************************************************************************/
_tx_thread_context_save(VOID)75 VOID   _tx_thread_context_save(VOID)
76 {
77 
78 TX_THREAD   *thread_ptr;
79 
80 
81     /* Enter critical section to ensure other threads are not playing with
82        the core ThreadX data structures.  */
83     _tx_win32_critical_section_obtain(&_tx_win32_critical_section);
84 
85     /* Debug entry.  */
86     _tx_win32_debug_entry_insert("CONTEXT_SAVE", __FILE__, __LINE__);
87 
88     /* Pickup the current thread pointer.  */
89     thread_ptr =  _tx_thread_current_ptr;
90 
91     /* If an application thread is running, suspend it to simulate preemption. */
92     if ((thread_ptr) && (_tx_thread_system_state == 0))
93     {
94 
95         /* Yes, this is the first interrupt and an application thread is running...
96            suspend it!  */
97 
98         /* Suspend the thread to simulate preemption.  Note that the thread is suspended BEFORE the protection get
99            flag is checked to ensure there is not a race condition between this thread and the update of that flag.  */
100         SuspendThread(thread_ptr -> tx_thread_win32_thread_handle);
101 
102         /* Debug entry.  */
103         _tx_win32_debug_entry_insert("CONTEXT_SAVE-suspend_thread", __FILE__, __LINE__);
104 
105     }
106 
107     /* Increment the nested interrupt condition.  */
108     _tx_thread_system_state++;
109 
110     /* Exit Win32 critical section.  */
111     _tx_win32_critical_section_release(&_tx_win32_critical_section);
112 }
113 
114