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 /** POSIX wrapper for THREADX                                             */
17 /**                                                                       */
18 /**                                                                       */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 /* Include necessary system files.  */
24 
25 #include "tx_api.h"     /* Threadx API */
26 #include "pthread.h"    /* Posix API */
27 #include "px_int.h"     /* Posix helper functions */
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    posix_in_thread_context                              PORTABLE C     */
35 /*                                                           6.1.7        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    William E. Lamie, Microsoft Corporation                             */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function determines if the system is currently in "thread"     */
43 /*      context, i.e. not timer routine, not ISR, not idling.             */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    None                                                                */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    TX_TRUE | TX_FALSE                                                  */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLED BY                                                             */
58 /*                                                                        */
59 /*    posix internal code                                                 */
60 /*                                                                        */
61 /*  RELEASE HISTORY                                                       */
62 /*                                                                        */
63 /*    DATE              NAME                      DESCRIPTION             */
64 /*                                                                        */
65 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
66 /*                                                                        */
67 /**************************************************************************/
posix_in_thread_context(VOID)68 ULONG  posix_in_thread_context(VOID)
69 {
70     /* External variables, defined within ThreadX, needed here. */
71     extern    TX_THREAD *     _tx_thread_current_ptr;
72     extern    ULONG           _tx_thread_system_state;
73 #ifndef TX_TIMER_PROCESS_IN_ISR
74     extern    TX_THREAD       _tx_timer_thread;
75 #endif
76 
77     /*     Return TX_FALSE if any of the following are true:
78          - we are in the scheduling loop (not in a thread);
79          - we are in an ISR
80          - we are in a timer routine
81            Return TX_TRUE otherwise (we are in a thread.)
82    */
83 
84     if (_tx_thread_system_state == TX_INITIALIZE_IN_PROGRESS)
85     {
86 
87         /* We are calling from initialization, return TRUE.  */
88         return(TX_TRUE);
89     }
90     else if ((!_tx_thread_current_ptr)                  /* Not in a thread */
91              || (_tx_thread_system_state)               /* In an ISR       */
92 #ifndef TX_TIMER_PROCESS_IN_ISR
93                                                         /* Timer routine   */
94              || (_tx_thread_current_ptr == &_tx_timer_thread)
95 #endif
96             )
97 
98     {
99         /* We are NOT in thread (thread) context.  */
100         return (TX_FALSE);
101     }
102     else
103     {
104         /* We ARE in thread (thread) context.  */
105         return (TX_TRUE);
106     }
107 }
108