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 #define TX_THREAD_SMP_SOURCE_CODE
26 
27 
28 /* Include necessary system files.  */
29 
30 #include "tx_api.h"
31 #include "tx_thread.h"
32 #include "tx_timer.h"
33 
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _tx_thread_smp_current_thread_get                 SMP/Linux/GCC     */
40 /*                                                           6.1          */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    William E. Lamie, Microsoft Corporation                             */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function is gets the current thread of the calling core.       */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    None                                                                */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Current Thread Pointer                Pointer to the current thread */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    pthread_self                          Get Linux thread ID           */
60 /*    _tx_linux_mutex_obtain                                              */
61 /*    _tx_linux_mutex_release                                             */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    ThreadX Components                                                  */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  09-30-2020     William E. Lamie         Initial Version 6.1           */
72 /*                                                                        */
73 /**************************************************************************/
_tx_thread_smp_current_thread_get(void)74 TX_THREAD  *_tx_thread_smp_current_thread_get(void)
75 {
76 UINT        core;
77 UINT        i;
78 pthread_t   thread_id;
79 TX_THREAD   *current_thread;
80 
81 
82     /* Lock Linux mutex.  */
83     _tx_linux_mutex_obtain(&_tx_linux_mutex);
84 
85     /* Default to core 0 for ISRs and initialization.  */
86     core =  0;
87 
88     /* Pickup the currently executing thread ID. */
89     thread_id =  pthread_self();
90 
91     /* Loop through mapping table to find the core running this thread ID.  */
92     for (i = 0; i < TX_THREAD_SMP_MAX_CORES; i++)
93     {
94 
95         /* Does this core match?  */
96         if (_tx_linux_virtual_cores[i].tx_thread_smp_core_mapping_linux_thread_id == thread_id)
97         {
98 
99             /* Yes, we have a match.  */
100             core =  i;
101 
102             /* Get out of loop.  */
103             break;
104         }
105     }
106 
107     /* Pickup current thread.  */
108     current_thread =  _tx_thread_current_ptr[core];
109 
110     /* Unlock linux mutex. */
111     _tx_linux_mutex_release(&_tx_linux_mutex);
112 
113     /* Now return the current thread for the core.  */
114     return(current_thread);
115 }
116 
117