]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MSVC-MingW/port.c
Prepare for V9.0.0 release:
[freertos] / FreeRTOS / Source / portable / MSVC-MingW / port.c
1 /*\r
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /* Standard includes. */\r
71 #include <stdio.h>\r
72 \r
73 /* Scheduler includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 \r
77 #ifdef __GNUC__\r
78         #include "mmsystem.h"\r
79 #else\r
80         #pragma comment(lib, "winmm.lib")\r
81 #endif\r
82 \r
83 #define portMAX_INTERRUPTS                              ( ( uint32_t ) sizeof( uint32_t ) * 8UL ) /* The number of bits in an uint32_t. */\r
84 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
85 \r
86 /* The priorities at which the various components of the simulation execute.\r
87 Priorities are higher when a soak test is performed to lessen the effect of\r
88 Windows interfering with the timing. */\r
89 #define portSOAK_TEST\r
90 #ifndef portSOAK_TEST\r
91         #define portDELETE_SELF_THREAD_PRIORITY                  THREAD_PRIORITY_HIGHEST /* Must be highest. */\r
92         #define portSIMULATED_INTERRUPTS_THREAD_PRIORITY THREAD_PRIORITY_NORMAL\r
93         #define portSIMULATED_TIMER_THREAD_PRIORITY              THREAD_PRIORITY_BELOW_NORMAL\r
94         #define portTASK_THREAD_PRIORITY                                 THREAD_PRIORITY_IDLE\r
95 #else\r
96         #define portDELETE_SELF_THREAD_PRIORITY                  THREAD_PRIORITY_TIME_CRITICAL /* Must be highest. */\r
97         #define portSIMULATED_INTERRUPTS_THREAD_PRIORITY THREAD_PRIORITY_HIGHEST\r
98         #define portSIMULATED_TIMER_THREAD_PRIORITY              THREAD_PRIORITY_ABOVE_NORMAL\r
99         #define portTASK_THREAD_PRIORITY                                 THREAD_PRIORITY_NORMAL\r
100 #endif\r
101 /*\r
102  * Created as a high priority thread, this function uses a timer to simulate\r
103  * a tick interrupt being generated on an embedded target.  In this Windows\r
104  * environment the timer does not achieve anything approaching real time\r
105  * performance though.\r
106  */\r
107 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );\r
108 \r
109 /*\r
110  * Process all the simulated interrupts - each represented by a bit in\r
111  * ulPendingInterrupts variable.\r
112  */\r
113 static void prvProcessSimulatedInterrupts( void );\r
114 \r
115 /*\r
116  * Interrupt handlers used by the kernel itself.  These are executed from the\r
117  * simulated interrupt handler thread.\r
118  */\r
119 static uint32_t prvProcessYieldInterrupt( void );\r
120 static uint32_t prvProcessTickInterrupt( void );\r
121 \r
122 /*\r
123  * Called when the process exits to let Windows know the high timer resolution\r
124  * is no longer required.\r
125  */\r
126 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType );\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /* The WIN32 simulator runs each task in a thread.  The context switching is\r
131 managed by the threads, so the task stack does not have to be managed directly,\r
132 although the task stack is still used to hold an xThreadState structure this is\r
133 the only thing it will ever hold.  The structure indirectly maps the task handle\r
134 to a thread handle. */\r
135 typedef struct\r
136 {\r
137         /* Handle of the thread that executes the task. */\r
138         void *pvThread;\r
139 \r
140 } xThreadState;\r
141 \r
142 /* Simulated interrupts waiting to be processed.  This is a bit mask where each\r
143 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */\r
144 static volatile uint32_t ulPendingInterrupts = 0UL;\r
145 \r
146 /* An event used to inform the simulated interrupt processing thread (a high\r
147 priority thread that simulated interrupt processing) that an interrupt is\r
148 pending. */\r
149 static void *pvInterruptEvent = NULL;\r
150 \r
151 /* Mutex used to protect all the simulated interrupt variables that are accessed\r
152 by multiple threads. */\r
153 static void *pvInterruptEventMutex = NULL;\r
154 \r
155 /* The critical nesting count for the currently executing task.  This is\r
156 initialised to a non-zero value so interrupts do not become enabled during\r
157 the initialisation phase.  As each task has its own critical nesting value\r
158 ulCriticalNesting will get set to zero when the first task runs.  This\r
159 initialisation is probably not critical in this simulated environment as the\r
160 simulated interrupt handlers do not get created until the FreeRTOS scheduler is\r
161 started anyway. */\r
162 static uint32_t ulCriticalNesting = 9999UL;\r
163 \r
164 /* Handlers for all the simulated software interrupts.  The first two positions\r
165 are used for the Yield and Tick interrupts so are handled slightly differently,\r
166 all the other interrupts can be user defined. */\r
167 static uint32_t (*ulIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };\r
168 \r
169 /* Pointer to the TCB of the currently executing task. */\r
170 extern void *pxCurrentTCB;\r
171 \r
172 /* Used to ensure nothing is processed during the startup sequence. */\r
173 static BaseType_t xPortRunning = pdFALSE;\r
174 \r
175 /*-----------------------------------------------------------*/\r
176 \r
177 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )\r
178 {\r
179 TickType_t xMinimumWindowsBlockTime;\r
180 TIMECAPS xTimeCaps;\r
181 \r
182         /* Set the timer resolution to the maximum possible. */\r
183         if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )\r
184         {\r
185                 xMinimumWindowsBlockTime = ( TickType_t ) xTimeCaps.wPeriodMin;\r
186                 timeBeginPeriod( xTimeCaps.wPeriodMin );\r
187 \r
188                 /* Register an exit handler so the timeBeginPeriod() function can be\r
189                 matched with a timeEndPeriod() when the application exits. */\r
190                 SetConsoleCtrlHandler( prvEndProcess, TRUE );\r
191         }\r
192         else\r
193         {\r
194                 xMinimumWindowsBlockTime = ( TickType_t ) 20;\r
195         }\r
196 \r
197         /* Just to prevent compiler warnings. */\r
198         ( void ) lpParameter;\r
199 \r
200         for( ;; )\r
201         {\r
202                 /* Wait until the timer expires and we can access the simulated interrupt\r
203                 variables.  *NOTE* this is not a 'real time' way of generating tick\r
204                 events as the next wake time should be relative to the previous wake\r
205                 time, not the time that Sleep() is called.  It is done this way to\r
206                 prevent overruns in this very non real time simulated/emulated\r
207                 environment. */\r
208                 if( portTICK_PERIOD_MS < xMinimumWindowsBlockTime )\r
209                 {\r
210                         Sleep( xMinimumWindowsBlockTime );\r
211                 }\r
212                 else\r
213                 {\r
214                         Sleep( portTICK_PERIOD_MS );\r
215                 }\r
216 \r
217                 configASSERT( xPortRunning );\r
218 \r
219                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
220 \r
221                 /* The timer has expired, generate the simulated tick event. */\r
222                 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );\r
223 \r
224                 /* The interrupt is now pending - notify the simulated interrupt\r
225                 handler thread. */\r
226                 if( ulCriticalNesting == 0 )\r
227                 {\r
228                         SetEvent( pvInterruptEvent );\r
229                 }\r
230 \r
231                 /* Give back the mutex so the simulated interrupt handler unblocks\r
232                 and can access the interrupt handler variables. */\r
233                 ReleaseMutex( pvInterruptEventMutex );\r
234         }\r
235 \r
236         #ifdef __GNUC__\r
237                 /* Should never reach here - MingW complains if you leave this line out,\r
238                 MSVC complains if you put it in. */\r
239                 return 0;\r
240         #endif\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType )\r
245 {\r
246 TIMECAPS xTimeCaps;\r
247 \r
248         ( void ) dwCtrlType;\r
249 \r
250         if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )\r
251         {\r
252                 /* Match the call to timeBeginPeriod( xTimeCaps.wPeriodMin ) made when\r
253                 the process started with a timeEndPeriod() as the process exits. */\r
254                 timeEndPeriod( xTimeCaps.wPeriodMin );\r
255         }\r
256 \r
257         return pdPASS;\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
262 {\r
263 xThreadState *pxThreadState = NULL;\r
264 int8_t *pcTopOfStack = ( int8_t * ) pxTopOfStack;\r
265 const SIZE_T xStackSize = 1024; /* Set the size to a small number which will get rounded up to the minimum possible. */\r
266 \r
267         #ifdef portSOAK_TEST\r
268         {\r
269                 /* Ensure highest priority class is inherited. */\r
270                 if( !SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS ) )\r
271                 {\r
272                         printf( "SetPriorityClass() failed\r\n" );\r
273                 }\r
274         }\r
275         #endif\r
276 \r
277         /* In this simulated case a stack is not initialised, but instead a thread\r
278         is created that will execute the task being created.  The thread handles\r
279         the context switching itself.  The xThreadState object is placed onto\r
280         the stack that was created for the task - so the stack buffer is still\r
281         used, just not in the conventional way.  It will not be used for anything\r
282         other than holding this structure. */\r
283         pxThreadState = ( xThreadState * ) ( pcTopOfStack - sizeof( xThreadState ) );\r
284 \r
285         /* Create the thread itself. */\r
286         pxThreadState->pvThread = CreateThread( NULL, xStackSize, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION, NULL );\r
287         configASSERT( pxThreadState->pvThread ); /* See comment where TerminateThread() is called. */\r
288         SetThreadAffinityMask( pxThreadState->pvThread, 0x01 );\r
289         SetThreadPriorityBoost( pxThreadState->pvThread, TRUE );\r
290         SetThreadPriority( pxThreadState->pvThread, portTASK_THREAD_PRIORITY );\r
291 \r
292         return ( StackType_t * ) pxThreadState;\r
293 }\r
294 /*-----------------------------------------------------------*/\r
295 \r
296 BaseType_t xPortStartScheduler( void )\r
297 {\r
298 void *pvHandle;\r
299 int32_t lSuccess = pdPASS;\r
300 xThreadState *pxThreadState;\r
301 \r
302         /* Install the interrupt handlers used by the scheduler itself. */\r
303         vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );\r
304         vPortSetInterruptHandler( portINTERRUPT_TICK, prvProcessTickInterrupt );\r
305 \r
306         /* Create the events and mutexes that are used to synchronise all the\r
307         threads. */\r
308         pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );\r
309         pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
310 \r
311         if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) )\r
312         {\r
313                 lSuccess = pdFAIL;\r
314         }\r
315 \r
316         /* Set the priority of this thread such that it is above the priority of\r
317         the threads that run tasks.  This higher priority is required to ensure\r
318         simulated interrupts take priority over tasks. */\r
319         pvHandle = GetCurrentThread();\r
320         if( pvHandle == NULL )\r
321         {\r
322                 lSuccess = pdFAIL;\r
323         }\r
324 \r
325         if( lSuccess == pdPASS )\r
326         {\r
327                 if( SetThreadPriority( pvHandle, portSIMULATED_INTERRUPTS_THREAD_PRIORITY ) == 0 )\r
328                 {\r
329                         lSuccess = pdFAIL;\r
330                 }\r
331                 SetThreadPriorityBoost( pvHandle, TRUE );\r
332                 SetThreadAffinityMask( pvHandle, 0x01 );\r
333         }\r
334 \r
335         if( lSuccess == pdPASS )\r
336         {\r
337                 /* Start the thread that simulates the timer peripheral to generate\r
338                 tick interrupts.  The priority is set below that of the simulated\r
339                 interrupt handler so the interrupt event mutex is used for the\r
340                 handshake / overrun protection. */\r
341                 pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, CREATE_SUSPENDED, NULL );\r
342                 if( pvHandle != NULL )\r
343                 {\r
344                         SetThreadPriority( pvHandle, portSIMULATED_TIMER_THREAD_PRIORITY );\r
345                         SetThreadPriorityBoost( pvHandle, TRUE );\r
346                         SetThreadAffinityMask( pvHandle, 0x01 );\r
347                         ResumeThread( pvHandle );\r
348                 }\r
349 \r
350                 /* Start the highest priority task by obtaining its associated thread\r
351                 state structure, in which is stored the thread handle. */\r
352                 pxThreadState = ( xThreadState * ) *( ( size_t * ) pxCurrentTCB );\r
353                 ulCriticalNesting = portNO_CRITICAL_NESTING;\r
354 \r
355                 /* Bump up the priority of the thread that is going to run, in the\r
356                 hope that this will assist in getting the Windows thread scheduler to\r
357                 behave as an embedded engineer might expect. */\r
358                 ResumeThread( pxThreadState->pvThread );\r
359 \r
360                 /* Handle all simulated interrupts - including yield requests and\r
361                 simulated ticks. */\r
362                 prvProcessSimulatedInterrupts();\r
363         }\r
364 \r
365         /* Would not expect to return from prvProcessSimulatedInterrupts(), so should\r
366         not get here. */\r
367         return 0;\r
368 }\r
369 /*-----------------------------------------------------------*/\r
370 \r
371 static uint32_t prvProcessYieldInterrupt( void )\r
372 {\r
373         return pdTRUE;\r
374 }\r
375 /*-----------------------------------------------------------*/\r
376 \r
377 static uint32_t prvProcessTickInterrupt( void )\r
378 {\r
379 uint32_t ulSwitchRequired;\r
380 \r
381         /* Process the tick itself. */\r
382         configASSERT( xPortRunning );\r
383         ulSwitchRequired = ( uint32_t ) xTaskIncrementTick();\r
384 \r
385         return ulSwitchRequired;\r
386 }\r
387 /*-----------------------------------------------------------*/\r
388 \r
389 static void prvProcessSimulatedInterrupts( void )\r
390 {\r
391 uint32_t ulSwitchRequired, i;\r
392 xThreadState *pxThreadState;\r
393 void *pvObjectList[ 2 ];\r
394 CONTEXT xContext;\r
395 \r
396         /* Going to block on the mutex that ensured exclusive access to the simulated\r
397         interrupt objects, and the event that signals that a simulated interrupt\r
398         should be processed. */\r
399         pvObjectList[ 0 ] = pvInterruptEventMutex;\r
400         pvObjectList[ 1 ] = pvInterruptEvent;\r
401 \r
402         /* Create a pending tick to ensure the first task is started as soon as\r
403         this thread pends. */\r
404         ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );\r
405         SetEvent( pvInterruptEvent );\r
406 \r
407         xPortRunning = pdTRUE;\r
408 \r
409         for(;;)\r
410         {\r
411                 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );\r
412 \r
413                 /* Used to indicate whether the simulated interrupt processing has\r
414                 necessitated a context switch to another task/thread. */\r
415                 ulSwitchRequired = pdFALSE;\r
416 \r
417                 /* For each interrupt we are interested in processing, each of which is\r
418                 represented by a bit in the 32bit ulPendingInterrupts variable. */\r
419                 for( i = 0; i < portMAX_INTERRUPTS; i++ )\r
420                 {\r
421                         /* Is the simulated interrupt pending? */\r
422                         if( ulPendingInterrupts & ( 1UL << i ) )\r
423                         {\r
424                                 /* Is a handler installed? */\r
425                                 if( ulIsrHandler[ i ] != NULL )\r
426                                 {\r
427                                         /* Run the actual handler. */\r
428                                         if( ulIsrHandler[ i ]() != pdFALSE )\r
429                                         {\r
430                                                 ulSwitchRequired |= ( 1 << i );\r
431                                         }\r
432                                 }\r
433 \r
434                                 /* Clear the interrupt pending bit. */\r
435                                 ulPendingInterrupts &= ~( 1UL << i );\r
436                         }\r
437                 }\r
438 \r
439                 if( ulSwitchRequired != pdFALSE )\r
440                 {\r
441                         void *pvOldCurrentTCB;\r
442 \r
443                         pvOldCurrentTCB = pxCurrentTCB;\r
444 \r
445                         /* Select the next task to run. */\r
446                         vTaskSwitchContext();\r
447 \r
448                         /* If the task selected to enter the running state is not the task\r
449                         that is already in the running state. */\r
450                         if( pvOldCurrentTCB != pxCurrentTCB )\r
451                         {\r
452                                 /* Suspend the old thread. */\r
453                                 pxThreadState = ( xThreadState *) *( ( size_t * ) pvOldCurrentTCB );\r
454                                 SuspendThread( pxThreadState->pvThread );\r
455 \r
456                                 /* Ensure the thread is actually suspended by performing a\r
457                                 synchronous operation that can only complete when the thread is\r
458                                 actually suspended.  The below code asks for dummy register\r
459                                 data. */\r
460                                 xContext.ContextFlags = CONTEXT_INTEGER;\r
461                                 ( void ) GetThreadContext( pxThreadState->pvThread, &xContext );\r
462 \r
463                                 /* Obtain the state of the task now selected to enter the\r
464                                 Running state. */\r
465                                 pxThreadState = ( xThreadState * ) ( *( size_t *) pxCurrentTCB );\r
466                                 ResumeThread( pxThreadState->pvThread );\r
467                         }\r
468                 }\r
469 \r
470                 ReleaseMutex( pvInterruptEventMutex );\r
471         }\r
472 }\r
473 /*-----------------------------------------------------------*/\r
474 \r
475 void vPortDeleteThread( void *pvTaskToDelete )\r
476 {\r
477 xThreadState *pxThreadState;\r
478 uint32_t ulErrorCode;\r
479 \r
480         /* Remove compiler warnings if configASSERT() is not defined. */\r
481         ( void ) ulErrorCode;\r
482 \r
483         /* Find the handle of the thread being deleted. */\r
484         pxThreadState = ( xThreadState * ) ( *( size_t *) pvTaskToDelete );\r
485 \r
486         /* Check that the thread is still valid, it might have been closed by\r
487         vPortCloseRunningThread() - which will be the case if the task associated\r
488         with the thread originally deleted itself rather than being deleted by a\r
489         different task. */\r
490         if( pxThreadState->pvThread != NULL )\r
491         {\r
492                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
493 \r
494                 /* !!! This is not a nice way to terminate a thread, and will eventually\r
495                 result in resources being depleted if tasks frequently delete other\r
496                 tasks (rather than deleting themselves) as the task stacks will not be\r
497                 freed. */\r
498                 ulErrorCode = TerminateThread( pxThreadState->pvThread, 0 );\r
499                 configASSERT( ulErrorCode );\r
500 \r
501                 ulErrorCode = CloseHandle( pxThreadState->pvThread );\r
502                 configASSERT( ulErrorCode );\r
503 \r
504                 ReleaseMutex( pvInterruptEventMutex );\r
505         }\r
506 }\r
507 /*-----------------------------------------------------------*/\r
508 \r
509 void vPortCloseRunningThread( void *pvTaskToDelete, volatile BaseType_t *pxPendYield )\r
510 {\r
511 xThreadState *pxThreadState;\r
512 void *pvThread;\r
513 uint32_t ulErrorCode;\r
514 \r
515         /* Remove compiler warnings if configASSERT() is not defined. */\r
516         ( void ) ulErrorCode;\r
517 \r
518         /* Find the handle of the thread being deleted. */\r
519         pxThreadState = ( xThreadState * ) ( *( size_t *) pvTaskToDelete );\r
520         pvThread = pxThreadState->pvThread;\r
521 \r
522         /* Raise the Windows priority of the thread to ensure the FreeRTOS scheduler\r
523         does not run and swap it out before it is closed.  If that were to happen\r
524         the thread would never run again and effectively be a thread handle and\r
525         memory leak. */\r
526         SetThreadPriority( pvThread, portDELETE_SELF_THREAD_PRIORITY );\r
527 \r
528         /* This function will not return, therefore a yield is set as pending to\r
529         ensure a context switch occurs away from this thread on the next tick. */\r
530         *pxPendYield = pdTRUE;\r
531 \r
532         /* Mark the thread associated with this task as invalid so\r
533         vPortDeleteThread() does not try to terminate it. */\r
534         pxThreadState->pvThread = NULL;\r
535 \r
536         /* Close the thread. */\r
537         ulErrorCode = CloseHandle( pvThread );\r
538         configASSERT( ulErrorCode );\r
539 \r
540         /* This is called from a critical section, which must be exited before the\r
541         thread stops. */\r
542         taskEXIT_CRITICAL();\r
543 \r
544         ExitThread( 0 );\r
545 }\r
546 /*-----------------------------------------------------------*/\r
547 \r
548 void vPortEndScheduler( void )\r
549 {\r
550         /* This function IS NOT TESTED! */\r
551         TerminateProcess( GetCurrentProcess(), 0 );\r
552 }\r
553 /*-----------------------------------------------------------*/\r
554 \r
555 void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber )\r
556 {\r
557         configASSERT( xPortRunning );\r
558 \r
559         if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )\r
560         {\r
561                 /* Yield interrupts are processed even when critical nesting is\r
562                 non-zero. */\r
563                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
564                 ulPendingInterrupts |= ( 1 << ulInterruptNumber );\r
565 \r
566                 /* The simulated interrupt is now held pending, but don't actually\r
567                 process it yet if this call is within a critical section.  It is\r
568                 possible for this to be in a critical section as calls to wait for\r
569                 mutexes are accumulative. */\r
570                 if( ulCriticalNesting == 0 )\r
571                 {\r
572                         SetEvent( pvInterruptEvent );\r
573                 }\r
574 \r
575                 ReleaseMutex( pvInterruptEventMutex );\r
576         }\r
577 }\r
578 /*-----------------------------------------------------------*/\r
579 \r
580 void vPortSetInterruptHandler( uint32_t ulInterruptNumber, uint32_t (*pvHandler)( void ) )\r
581 {\r
582         if( ulInterruptNumber < portMAX_INTERRUPTS )\r
583         {\r
584                 if( pvInterruptEventMutex != NULL )\r
585                 {\r
586                         WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
587                         ulIsrHandler[ ulInterruptNumber ] = pvHandler;\r
588                         ReleaseMutex( pvInterruptEventMutex );\r
589                 }\r
590                 else\r
591                 {\r
592                         ulIsrHandler[ ulInterruptNumber ] = pvHandler;\r
593                 }\r
594         }\r
595 }\r
596 /*-----------------------------------------------------------*/\r
597 \r
598 void vPortEnterCritical( void )\r
599 {\r
600         if( xPortRunning == pdTRUE )\r
601         {\r
602                 /* The interrupt event mutex is held for the entire critical section,\r
603                 effectively disabling (simulated) interrupts. */\r
604                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
605                 ulCriticalNesting++;\r
606         }\r
607         else\r
608         {\r
609                 ulCriticalNesting++;\r
610         }\r
611 }\r
612 /*-----------------------------------------------------------*/\r
613 \r
614 void vPortExitCritical( void )\r
615 {\r
616 int32_t lMutexNeedsReleasing;\r
617 \r
618         /* The interrupt event mutex should already be held by this thread as it was\r
619         obtained on entry to the critical section. */\r
620 \r
621         lMutexNeedsReleasing = pdTRUE;\r
622 \r
623         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
624         {\r
625                 if( ulCriticalNesting == ( portNO_CRITICAL_NESTING + 1 ) )\r
626                 {\r
627                         ulCriticalNesting--;\r
628 \r
629                         /* Were any interrupts set to pending while interrupts were\r
630                         (simulated) disabled? */\r
631                         if( ulPendingInterrupts != 0UL )\r
632                         {\r
633                                 configASSERT( xPortRunning );\r
634                                 SetEvent( pvInterruptEvent );\r
635 \r
636                                 /* Mutex will be released now, so does not require releasing\r
637                                 on function exit. */\r
638                                 lMutexNeedsReleasing = pdFALSE;\r
639                                 ReleaseMutex( pvInterruptEventMutex );\r
640                         }\r
641                 }\r
642                 else\r
643                 {\r
644                         /* Tick interrupts will still not be processed as the critical\r
645                         nesting depth will not be zero. */\r
646                         ulCriticalNesting--;\r
647                 }\r
648         }\r
649 \r
650         if( pvInterruptEventMutex != NULL )\r
651         {\r
652                 if( lMutexNeedsReleasing == pdTRUE )\r
653                 {\r
654                         configASSERT( xPortRunning );\r
655                         ReleaseMutex( pvInterruptEventMutex );\r
656                 }\r
657         }\r
658 }\r
659 /*-----------------------------------------------------------*/\r
660 \r