]> begriffs open source - freertos/blob - portable/MSVC-MingW/portmacro.h
Regression Bug Fix: Fix Incorrect Return of MSVC-MingW portYIELD_FROM_ISR (#1207)
[freertos] / portable / MSVC-MingW / portmacro.h
1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28
29 #ifndef PORTMACRO_H
30 #define PORTMACRO_H
31
32 #ifdef WIN32_LEAN_AND_MEAN
33     #include <winsock2.h>
34 #else
35     #include <winsock.h>
36 #endif
37
38 #include <windows.h>
39 #include <timeapi.h>
40 #include <mmsystem.h>
41 #include <winbase.h>
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 /******************************************************************************
48 *   Defines
49 ******************************************************************************/
50 /* Type definitions. */
51 #define portCHAR                 char
52 #define portFLOAT                float
53 #define portDOUBLE               double
54 #define portLONG                 long
55 #define portSHORT                short
56 #define portSTACK_TYPE           size_t
57 #define portPOINTER_SIZE_TYPE    size_t
58
59 typedef portSTACK_TYPE           StackType_t;
60
61 #if defined( __x86_64__ ) || defined( _M_X64 )
62     #define portBASE_TYPE    long long
63     typedef long long            BaseType_t;
64     typedef unsigned long long   UBaseType_t;
65 #else
66     #define portBASE_TYPE    long
67     typedef long                 BaseType_t;
68     typedef unsigned long        UBaseType_t;
69 #endif
70
71
72 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
73     typedef uint16_t             TickType_t;
74     #define portMAX_DELAY              ( TickType_t ) 0xffff
75 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
76     typedef uint32_t             TickType_t;
77     #define portMAX_DELAY              ( TickType_t ) 0xffffffffUL
78
79 /* 32-bit tick type on a 32/64-bit architecture, so reads of the tick
80  * count do not need to be guarded with a critical section. */
81     #define portTICK_TYPE_IS_ATOMIC    1
82 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
83     typedef uint64_t             TickType_t;
84     #define portMAX_DELAY              ( TickType_t ) 0xffffffffffffffffULL
85
86 #if defined( __x86_64__ ) || defined( _M_X64 )
87 /* 64-bit tick type on a 64-bit architecture, so reads of the tick
88  * count do not need to be guarded with a critical section. */
89     #define portTICK_TYPE_IS_ATOMIC    1
90 #endif
91 #else
92     #error configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width.
93 #endif
94
95 /* Hardware specifics. */
96 #define portSTACK_GROWTH          ( -1 )
97 #define portTICK_PERIOD_MS        ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
98 #define portINLINE                __inline
99
100 #if defined( __x86_64__ ) || defined( _M_X64 )
101     #define portBYTE_ALIGNMENT    8
102 #else
103     #define portBYTE_ALIGNMENT    4
104 #endif
105
106 #define portYIELD()    vPortGenerateSimulatedInterrupt( portINTERRUPT_YIELD )
107
108
109 extern volatile BaseType_t xInsideInterrupt;
110 #define portSOFTWARE_BARRIER()    while( xInsideInterrupt != pdFALSE )
111
112
113 /* Simulated interrupts return pdFALSE if no context switch should be performed,
114  * or a non-zero number if a context switch should be performed. */
115 #define portYIELD_FROM_ISR( x )       return x
116 #define portEND_SWITCHING_ISR( x )    portYIELD_FROM_ISR( ( x ) )
117
118 void vPortCloseRunningThread( void * pvTaskToDelete,
119                               volatile BaseType_t * pxPendYield );
120 void vPortDeleteThread( void * pvThreadToDelete );
121 #define portCLEAN_UP_TCB( pxTCB )                                  vPortDeleteThread( pxTCB )
122 #define portPRE_TASK_DELETE_HOOK( pvTaskToDelete, pxPendYield )    vPortCloseRunningThread( ( pvTaskToDelete ), ( pxPendYield ) )
123 #define portDISABLE_INTERRUPTS()                                   vPortEnterCritical()
124 #define portENABLE_INTERRUPTS()                                    vPortExitCritical()
125
126 /* Critical section handling. */
127 void vPortEnterCritical( void );
128 void vPortExitCritical( void );
129
130 #define portENTER_CRITICAL()    vPortEnterCritical()
131 #define portEXIT_CRITICAL()     vPortExitCritical()
132
133 #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION
134     #define configUSE_PORT_OPTIMISED_TASK_SELECTION    1
135 #endif
136
137 /*-----------------------------------------------------------*/
138
139 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
140
141     /* Check the configuration. */
142     #if ( configMAX_PRIORITIES > 32 )
143         #error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32.  It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice.
144     #endif
145
146     /* Store/clear the ready priorities in a bit map. */
147     #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities )    ( uxReadyPriorities ) |= ( ( ( UBaseType_t ) 1 ) << ( uxPriority ) )
148     #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities )     ( uxReadyPriorities ) &= ~( ( ( UBaseType_t ) 1 ) << ( uxPriority ) )
149
150     #ifdef __GNUC__
151
152         #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities )    \
153         __asm volatile ( "bsr %1, %0\n\t"                                       \
154                          : "=r" ( uxTopPriority )                               \
155                          : "rm" ( uxReadyPriorities )                           \
156                          : "cc" )
157
158     #else /* __GNUC__ */
159
160         /* BitScanReverse returns the bit position of the most significant '1'
161          * in the word. */
162         #if defined( __x86_64__ ) || defined( _M_X64 )
163
164             #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities )    \
165             do                                                                      \
166             {                                                                       \
167                 DWORD ulTopPriority;                                                \
168                 _BitScanReverse64( &ulTopPriority, ( uxReadyPriorities ) );         \
169                 uxTopPriority = ulTopPriority;                                      \
170             } while( 0 )
171
172         #else /* #if defined( __x86_64__ ) || defined( _M_X64 ) */
173
174             #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities )    _BitScanReverse( ( DWORD * ) &( uxTopPriority ), ( uxReadyPriorities ) )
175
176         #endif /* #if defined( __x86_64__ ) || defined( _M_X64 ) */
177
178     #endif /* __GNUC__ */
179
180 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
181
182 #ifndef __GNUC__
183     __pragma( warning( disable:4211 ) ) /* Nonstandard extension used, as extern is only nonstandard to MSVC. */
184 #endif
185
186
187 /* Task function macros as described on the FreeRTOS.org WEB site. */
188 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters )    void vFunction( void * pvParameters )
189 #define portTASK_FUNCTION( vFunction, pvParameters )          void vFunction( void * pvParameters )
190
191 #define portINTERRUPT_YIELD                        ( 0UL )
192 #define portINTERRUPT_TICK                         ( 1UL )
193 #define portINTERRUPT_APPLICATION_DEFINED_START    ( 2UL )
194
195 /*
196  * Raise a simulated interrupt represented by the bit mask in ulInterruptMask.
197  * Each bit can be used to represent an individual interrupt - with the first
198  * two bits being used for the Yield and Tick interrupts respectively.
199  */
200 void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber );
201
202 /*
203  * Raise a simulated interrupt represented by the bit mask in ulInterruptMask.
204  * Each bit can be used to represent an individual interrupt - with the first
205  * two bits being used for the Yield and Tick interrupts respectively. This function
206  * can be called in a windows thread.
207  */
208 void vPortGenerateSimulatedInterruptFromWindowsThread( uint32_t ulInterruptNumber );
209
210 /*
211  * Install an interrupt handler to be called by the simulated interrupt handler
212  * thread.  The interrupt number must be above any used by the kernel itself
213  * (at the time of writing the kernel was using interrupt numbers 0, 1, and 2
214  * as defined above).  The number must also be lower than 32.
215  *
216  * Interrupt handler functions must return a non-zero value if executing the
217  * handler resulted in a task switch being required.
218  */
219 void vPortSetInterruptHandler( uint32_t ulInterruptNumber,
220                                uint32_t ( * pvHandler )( void ) );
221
222 #ifdef __cplusplus
223 }
224 #endif
225
226 #endif /* ifndef PORTMACRO_H */