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                             Linux/GNU       */
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 /*    _tx_linux_debug_entry_insert                                        */
61 /*    tx_linux_mutex_lock                                                 */
62 /*    _tx_linux_thread_suspend                                            */
63 /*    tx_linux_mutex_unlock                                               */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    ISRs                                                                */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  09-30-2020     William E. Lamie         Initial Version 6.1           */
74 /*                                                                        */
75 /**************************************************************************/
_tx_thread_context_save(VOID)76 VOID   _tx_thread_context_save(VOID)
77 {
78 
79     /* Debug entry.  */
80     _tx_linux_debug_entry_insert("CONTEXT_SAVE", __FILE__, __LINE__);
81 
82     /* Lock mutex to ensure other threads are not playing with
83        the core ThreadX data structures.  */
84     tx_linux_mutex_lock(_tx_linux_mutex);
85 
86     /* If an application thread is running, suspend it to simulate preemption. */
87     if ((_tx_thread_current_ptr) && (_tx_thread_system_state == 0))
88     {
89 
90         /* Debug entry.  */
91         _tx_linux_debug_entry_insert("CONTEXT_SAVE-suspend_thread", __FILE__, __LINE__);
92 
93         /* Yes, this is the first interrupt and an application thread is running...
94            suspend it!  */
95         _tx_linux_thread_suspend(_tx_thread_current_ptr -> tx_thread_linux_thread_id);
96 
97         /* Indicate that this thread was suspended asynchronously.  */
98         _tx_thread_current_ptr -> tx_thread_linux_suspension_type =  1;
99     }
100 
101     /* Increment the nested interrupt condition.  */
102     _tx_thread_system_state++;
103 
104     /* Unlock linux mutex. */
105     tx_linux_mutex_unlock(_tx_linux_mutex);
106 }
107 
108