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_restore Win32/Visual */
39 /* 6.1 */
40 /* AUTHOR */
41 /* */
42 /* William E. Lamie, Microsoft Corporation */
43 /* */
44 /* DESCRIPTION */
45 /* */
46 /* This function restores the interrupt context if it is processing a */
47 /* nested interrupt. If not, it returns to the interrupt thread if no */
48 /* preemption is necessary. Otherwise, if preemption is necessary or */
49 /* if no thread was running, the function returns to the scheduler. */
50 /* */
51 /* INPUT */
52 /* */
53 /* None */
54 /* */
55 /* OUTPUT */
56 /* */
57 /* None */
58 /* */
59 /* CALLS */
60 /* */
61 /* ReleaseSemaphore Win32 release semaphore */
62 /* ResumeThread Win32 resume thread */
63 /* _tx_win32_critical_section_obtain Obtain critical section */
64 /* _tx_win32_critical_section_release Release critical section */
65 /* */
66 /* CALLED BY */
67 /* */
68 /* ISRs Interrupt Service Routines */
69 /* */
70 /* RELEASE HISTORY */
71 /* */
72 /* DATE NAME DESCRIPTION */
73 /* */
74 /* 09-30-2020 William E. Lamie Initial Version 6.1 */
75 /* */
76 /**************************************************************************/
_tx_thread_context_restore(VOID)77 VOID _tx_thread_context_restore(VOID)
78 {
79
80 /* Enter critical section to ensure other threads are not playing with
81 the core ThreadX data structures. */
82 _tx_win32_critical_section_obtain(&_tx_win32_critical_section);
83
84 /* Debug entry. */
85 _tx_win32_debug_entry_insert("CONTEXT_RESTORE", __FILE__, __LINE__);
86
87 /* Decrement the nested interrupt count. */
88 _tx_thread_system_state--;
89
90 /* Determine if this is the first nested interrupt and if a ThreadX
91 application thread was running at the time. */
92 if ((!_tx_thread_system_state) && (_tx_thread_current_ptr))
93 {
94
95 /* Yes, this is the first and last interrupt processed. */
96
97 /* Check to see if preemption is required. */
98 if ((_tx_thread_preempt_disable == 0) && (_tx_thread_current_ptr != _tx_thread_execute_ptr))
99 {
100
101 /* Preempt the running application thread. We don't need to suspend the
102 application thread since that is done in the context save processing. */
103
104 /* Indicate that this thread was suspended asynchronously. */
105 _tx_thread_current_ptr -> tx_thread_win32_suspension_type = 1;
106
107 /* Save the remaining time-slice and disable it. */
108 if (_tx_timer_time_slice)
109 {
110
111 _tx_thread_current_ptr -> tx_thread_time_slice = _tx_timer_time_slice;
112 _tx_timer_time_slice = 0;
113 }
114
115 /* Clear the current thread pointer. */
116 _tx_thread_current_ptr = TX_NULL;
117
118 /* Wakeup the system thread by setting the system semaphore. */
119 ReleaseSemaphore(_tx_win32_scheduler_semaphore, 1, NULL);
120 }
121 else
122 {
123
124 /* Since preemption is not required, resume the interrupted thread. */
125 ResumeThread(_tx_thread_current_ptr -> tx_thread_win32_thread_handle);
126 }
127 }
128
129 /* Leave Win32 critical section. */
130 _tx_win32_critical_section_release_all(&_tx_win32_critical_section);
131 }
132
133