]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MSVC-MingW/port.c
ad023f1eff4bfe861b5c6baad6a22a4c23cdeef2
[freertos] / FreeRTOS / Source / portable / MSVC-MingW / port.c
1 /*\r
2     FreeRTOS V9.0.0rc1 - 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 portSIMULATED_INTERRUPTS_THREAD_PRIORITY THREAD_PRIORITY_NORMAL\r
92         #define portSIMULATED_TIMER_THREAD_PRIORITY              THREAD_PRIORITY_BELOW_NORMAL\r
93         #define portTASK_THREAD_PRIORITY                                 THREAD_PRIORITY_IDLE\r
94 #else\r
95         #define portSIMULATED_INTERRUPTS_THREAD_PRIORITY THREAD_PRIORITY_TIME_CRITICAL\r
96         #define portSIMULATED_TIMER_THREAD_PRIORITY              THREAD_PRIORITY_HIGHEST\r
97         #define portTASK_THREAD_PRIORITY                                 THREAD_PRIORITY_ABOVE_NORMAL\r
98 #endif\r
99 /*\r
100  * Created as a high priority thread, this function uses a timer to simulate\r
101  * a tick interrupt being generated on an embedded target.  In this Windows\r
102  * environment the timer does not achieve anything approaching real time\r
103  * performance though.\r
104  */\r
105 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );\r
106 \r
107 /*\r
108  * Process all the simulated interrupts - each represented by a bit in\r
109  * ulPendingInterrupts variable.\r
110  */\r
111 static void prvProcessSimulatedInterrupts( void );\r
112 \r
113 /*\r
114  * Interrupt handlers used by the kernel itself.  These are executed from the\r
115  * simulated interrupt handler thread.\r
116  */\r
117 static uint32_t prvProcessYieldInterrupt( void );\r
118 static uint32_t prvProcessTickInterrupt( void );\r
119 \r
120 /*\r
121  * Called when the process exits to let Windows know the high timer resolution\r
122  * is no longer required.\r
123  */\r
124 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType );\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 /* The WIN32 simulator runs each task in a thread.  The context switching is\r
129 managed by the threads, so the task stack does not have to be managed directly,\r
130 although the task stack is still used to hold an xThreadState structure this is\r
131 the only thing it will ever hold.  The structure indirectly maps the task handle\r
132 to a thread handle. */\r
133 typedef struct\r
134 {\r
135         /* Handle of the thread that executes the task. */\r
136         void *pvThread;\r
137 \r
138 } xThreadState;\r
139 \r
140 /* Simulated interrupts waiting to be processed.  This is a bit mask where each\r
141 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */\r
142 static volatile uint32_t ulPendingInterrupts = 0UL;\r
143 \r
144 /* An event used to inform the simulated interrupt processing thread (a high\r
145 priority thread that simulated interrupt processing) that an interrupt is\r
146 pending. */\r
147 static void *pvInterruptEvent = NULL;\r
148 \r
149 /* Mutex used to protect all the simulated interrupt variables that are accessed\r
150 by multiple threads. */\r
151 static void *pvInterruptEventMutex = NULL;\r
152 \r
153 /* The critical nesting count for the currently executing task.  This is\r
154 initialised to a non-zero value so interrupts do not become enabled during\r
155 the initialisation phase.  As each task has its own critical nesting value\r
156 ulCriticalNesting will get set to zero when the first task runs.  This\r
157 initialisation is probably not critical in this simulated environment as the\r
158 simulated interrupt handlers do not get created until the FreeRTOS scheduler is\r
159 started anyway. */\r
160 static uint32_t ulCriticalNesting = 9999UL;\r
161 \r
162 /* Handlers for all the simulated software interrupts.  The first two positions\r
163 are used for the Yield and Tick interrupts so are handled slightly differently,\r
164 all the other interrupts can be user defined. */\r
165 static uint32_t (*ulIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };\r
166 \r
167 /* Pointer to the TCB of the currently executing task. */\r
168 extern void *pxCurrentTCB;\r
169 \r
170 /* Used to ensure nothing is processed during the startup sequence. */\r
171 static BaseType_t xPortRunning = pdFALSE;\r
172 \r
173 /*-----------------------------------------------------------*/\r
174 \r
175 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )\r
176 {\r
177 TickType_t xMinimumWindowsBlockTime;\r
178 TIMECAPS xTimeCaps;\r
179 \r
180         /* Set the timer resolution to the maximum possible. */\r
181         if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )\r
182         {\r
183                 xMinimumWindowsBlockTime = ( TickType_t ) xTimeCaps.wPeriodMin;\r
184                 timeBeginPeriod( xTimeCaps.wPeriodMin );\r
185 \r
186                 /* Register an exit handler so the timeBeginPeriod() function can be\r
187                 matched with a timeEndPeriod() when the application exits. */\r
188                 SetConsoleCtrlHandler( prvEndProcess, TRUE );\r
189         }\r
190         else\r
191         {\r
192                 xMinimumWindowsBlockTime = ( TickType_t ) 20;\r
193         }\r
194 \r
195         /* Just to prevent compiler warnings. */\r
196         ( void ) lpParameter;\r
197 \r
198         for( ;; )\r
199         {\r
200                 /* Wait until the timer expires and we can access the simulated interrupt\r
201                 variables.  *NOTE* this is not a 'real time' way of generating tick\r
202                 events as the next wake time should be relative to the previous wake\r
203                 time, not the time that Sleep() is called.  It is done this way to\r
204                 prevent overruns in this very non real time simulated/emulated\r
205                 environment. */\r
206                 if( portTICK_PERIOD_MS < xMinimumWindowsBlockTime )\r
207                 {\r
208                         Sleep( xMinimumWindowsBlockTime );\r
209                 }\r
210                 else\r
211                 {\r
212                         Sleep( portTICK_PERIOD_MS );\r
213                 }\r
214 \r
215                 configASSERT( xPortRunning );\r
216 \r
217                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
218 \r
219                 /* The timer has expired, generate the simulated tick event. */\r
220                 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );\r
221 \r
222                 /* The interrupt is now pending - notify the simulated interrupt\r
223                 handler thread. */\r
224                 if( ulCriticalNesting == 0 )\r
225                 {\r
226                         SetEvent( pvInterruptEvent );\r
227                 }\r
228 \r
229                 /* Give back the mutex so the simulated interrupt handler unblocks\r
230                 and can access the interrupt handler variables. */\r
231                 ReleaseMutex( pvInterruptEventMutex );\r
232         }\r
233 \r
234         #ifdef __GNUC__\r
235                 /* Should never reach here - MingW complains if you leave this line out,\r
236                 MSVC complains if you put it in. */\r
237                 return 0;\r
238         #endif\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType )\r
243 {\r
244 TIMECAPS xTimeCaps;\r
245 \r
246         ( void ) dwCtrlType;\r
247 \r
248         if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )\r
249         {\r
250                 /* Match the call to timeBeginPeriod( xTimeCaps.wPeriodMin ) made when\r
251                 the process started with a timeEndPeriod() as the process exits. */\r
252                 timeEndPeriod( xTimeCaps.wPeriodMin );\r
253         }\r
254 \r
255         return pdPASS;\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
260 {\r
261 xThreadState *pxThreadState = NULL;\r
262 int8_t *pcTopOfStack = ( int8_t * ) pxTopOfStack;\r
263 \r
264         #ifdef portSOAK_TEST\r
265         {\r
266                 /* Ensure highest priority class is inherited. */\r
267                 if( !SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS ) )\r
268                 {\r
269                         printf( "SetPriorityClass() failed\r\n" );\r
270                 }\r
271         }\r
272         #endif\r
273 \r
274         /* In this simulated case a stack is not initialised, but instead a thread\r
275         is created that will execute the task being created.  The thread handles\r
276         the context switching itself.  The xThreadState object is placed onto\r
277         the stack that was created for the task - so the stack buffer is still\r
278         used, just not in the conventional way.  It will not be used for anything\r
279         other than holding this structure. */\r
280         pxThreadState = ( xThreadState * ) ( pcTopOfStack - sizeof( xThreadState ) );\r
281 \r
282         /* Create the thread itself. */\r
283         pxThreadState->pvThread = CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED, NULL );\r
284         configASSERT( pxThreadState->pvThread );\r
285         SetThreadAffinityMask( pxThreadState->pvThread, 0x01 );\r
286         SetThreadPriorityBoost( pxThreadState->pvThread, TRUE );\r
287         SetThreadPriority( pxThreadState->pvThread, portTASK_THREAD_PRIORITY );\r
288 \r
289         return ( StackType_t * ) pxThreadState;\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 BaseType_t xPortStartScheduler( void )\r
294 {\r
295 void *pvHandle;\r
296 int32_t lSuccess = pdPASS;\r
297 xThreadState *pxThreadState;\r
298 \r
299         /* Install the interrupt handlers used by the scheduler itself. */\r
300         vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );\r
301         vPortSetInterruptHandler( portINTERRUPT_TICK, prvProcessTickInterrupt );\r
302 \r
303         /* Create the events and mutexes that are used to synchronise all the\r
304         threads. */\r
305         pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );\r
306         pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
307 \r
308         if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) )\r
309         {\r
310                 lSuccess = pdFAIL;\r
311         }\r
312 \r
313         /* Set the priority of this thread such that it is above the priority of\r
314         the threads that run tasks.  This higher priority is required to ensure\r
315         simulated interrupts take priority over tasks. */\r
316         pvHandle = GetCurrentThread();\r
317         if( pvHandle == NULL )\r
318         {\r
319                 lSuccess = pdFAIL;\r
320         }\r
321 \r
322         if( lSuccess == pdPASS )\r
323         {\r
324                 if( SetThreadPriority( pvHandle, portSIMULATED_INTERRUPTS_THREAD_PRIORITY ) == 0 )\r
325                 {\r
326                         lSuccess = pdFAIL;\r
327                 }\r
328                 SetThreadPriorityBoost( pvHandle, TRUE );\r
329                 SetThreadAffinityMask( pvHandle, 0x01 );\r
330         }\r
331 \r
332         if( lSuccess == pdPASS )\r
333         {\r
334                 /* Start the thread that simulates the timer peripheral to generate\r
335                 tick interrupts.  The priority is set below that of the simulated\r
336                 interrupt handler so the interrupt event mutex is used for the\r
337                 handshake / overrun protection. */\r
338                 pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, CREATE_SUSPENDED, NULL );\r
339                 if( pvHandle != NULL )\r
340                 {\r
341                         SetThreadPriority( pvHandle, portSIMULATED_TIMER_THREAD_PRIORITY );\r
342                         SetThreadPriorityBoost( pvHandle, TRUE );\r
343                         SetThreadAffinityMask( pvHandle, 0x01 );\r
344                         ResumeThread( pvHandle );\r
345                 }\r
346 \r
347                 /* Start the highest priority task by obtaining its associated thread\r
348                 state structure, in which is stored the thread handle. */\r
349                 pxThreadState = ( xThreadState * ) *( ( size_t * ) pxCurrentTCB );\r
350                 ulCriticalNesting = portNO_CRITICAL_NESTING;\r
351 \r
352                 /* Bump up the priority of the thread that is going to run, in the\r
353                 hope that this will assist in getting the Windows thread scheduler to\r
354                 behave as an embedded engineer might expect. */\r
355                 ResumeThread( pxThreadState->pvThread );\r
356 \r
357                 /* Handle all simulated interrupts - including yield requests and\r
358                 simulated ticks. */\r
359                 prvProcessSimulatedInterrupts();\r
360         }\r
361 \r
362         /* Would not expect to return from prvProcessSimulatedInterrupts(), so should\r
363         not get here. */\r
364         return 0;\r
365 }\r
366 /*-----------------------------------------------------------*/\r
367 \r
368 static uint32_t prvProcessYieldInterrupt( void )\r
369 {\r
370         return pdTRUE;\r
371 }\r
372 /*-----------------------------------------------------------*/\r
373 \r
374 static uint32_t prvProcessTickInterrupt( void )\r
375 {\r
376 uint32_t ulSwitchRequired;\r
377 \r
378         /* Process the tick itself. */\r
379         configASSERT( xPortRunning );\r
380         ulSwitchRequired = ( uint32_t ) xTaskIncrementTick();\r
381 \r
382         return ulSwitchRequired;\r
383 }\r
384 /*-----------------------------------------------------------*/\r
385 \r
386 static void prvProcessSimulatedInterrupts( void )\r
387 {\r
388 uint32_t ulSwitchRequired, i;\r
389 xThreadState *pxThreadState;\r
390 void *pvObjectList[ 2 ];\r
391 CONTEXT xContext;\r
392 \r
393         /* Going to block on the mutex that ensured exclusive access to the simulated\r
394         interrupt objects, and the event that signals that a simulated interrupt\r
395         should be processed. */\r
396         pvObjectList[ 0 ] = pvInterruptEventMutex;\r
397         pvObjectList[ 1 ] = pvInterruptEvent;\r
398 \r
399         /* Create a pending tick to ensure the first task is started as soon as\r
400         this thread pends. */\r
401         ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );\r
402         SetEvent( pvInterruptEvent );\r
403 \r
404         xPortRunning = pdTRUE;\r
405 \r
406         for(;;)\r
407         {\r
408                 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );\r
409 \r
410                 /* Used to indicate whether the simulated interrupt processing has\r
411                 necessitated a context switch to another task/thread. */\r
412                 ulSwitchRequired = pdFALSE;\r
413 \r
414                 /* For each interrupt we are interested in processing, each of which is\r
415                 represented by a bit in the 32bit ulPendingInterrupts variable. */\r
416                 for( i = 0; i < portMAX_INTERRUPTS; i++ )\r
417                 {\r
418                         /* Is the simulated interrupt pending? */\r
419                         if( ulPendingInterrupts & ( 1UL << i ) )\r
420                         {\r
421                                 /* Is a handler installed? */\r
422                                 if( ulIsrHandler[ i ] != NULL )\r
423                                 {\r
424                                         /* Run the actual handler. */\r
425                                         if( ulIsrHandler[ i ]() != pdFALSE )\r
426                                         {\r
427                                                 ulSwitchRequired |= ( 1 << i );\r
428                                         }\r
429                                 }\r
430 \r
431                                 /* Clear the interrupt pending bit. */\r
432                                 ulPendingInterrupts &= ~( 1UL << i );\r
433                         }\r
434                 }\r
435 \r
436                 if( ulSwitchRequired != pdFALSE )\r
437                 {\r
438                         void *pvOldCurrentTCB;\r
439 \r
440                         pvOldCurrentTCB = pxCurrentTCB;\r
441 \r
442                         /* Select the next task to run. */\r
443                         vTaskSwitchContext();\r
444 \r
445                         /* If the task selected to enter the running state is not the task\r
446                         that is already in the running state. */\r
447                         if( pvOldCurrentTCB != pxCurrentTCB )\r
448                         {\r
449                                 /* Suspend the old thread. */\r
450                                 pxThreadState = ( xThreadState *) *( ( size_t * ) pvOldCurrentTCB );\r
451                                 SuspendThread( pxThreadState->pvThread );\r
452 \r
453                                 /* Ensure the thread is actually suspended by performing a\r
454                                 synchronous operation that can only complete when the thread is\r
455                                 actually suspended.  The below code asks for dummy register\r
456                                 data. */\r
457                                 xContext.ContextFlags = CONTEXT_INTEGER;\r
458                                 ( void ) GetThreadContext( pxThreadState->pvThread, &xContext );\r
459 \r
460                                 /* Obtain the state of the task now selected to enter the\r
461                                 Running state. */\r
462                                 pxThreadState = ( xThreadState * ) ( *( size_t *) pxCurrentTCB );\r
463                                 ResumeThread( pxThreadState->pvThread );\r
464                         }\r
465                 }\r
466 \r
467                 ReleaseMutex( pvInterruptEventMutex );\r
468         }\r
469 }\r
470 /*-----------------------------------------------------------*/\r
471 \r
472 void vPortDeleteThread( void *pvTaskToDelete )\r
473 {\r
474 xThreadState *pxThreadState;\r
475 uint32_t ulErrorCode;\r
476 \r
477         /* Remove compiler warnings if configASSERT() is not defined. */\r
478         ( void ) ulErrorCode;\r
479 \r
480         /* Find the handle of the thread being deleted. */\r
481         pxThreadState = ( xThreadState * ) ( *( size_t *) pvTaskToDelete );\r
482 \r
483         /* Check that the thread is still valid, it might have been closed by\r
484         vPortCloseRunningThread() - which will be the case if the task associated\r
485         with the thread originally deleted itself rather than being deleted by a\r
486         different task. */\r
487         if( pxThreadState->pvThread != NULL )\r
488         {\r
489                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
490 \r
491                 ulErrorCode = TerminateThread( pxThreadState->pvThread, 0 );\r
492                 configASSERT( ulErrorCode );\r
493 \r
494                 ulErrorCode = CloseHandle( pxThreadState->pvThread );\r
495                 configASSERT( ulErrorCode );\r
496 \r
497                 ReleaseMutex( pvInterruptEventMutex );\r
498         }\r
499 }\r
500 /*-----------------------------------------------------------*/\r
501 \r
502 void vPortCloseRunningThread( void *pvTaskToDelete, volatile BaseType_t *pxPendYield )\r
503 {\r
504 xThreadState *pxThreadState;\r
505 void *pvThread;\r
506 uint32_t ulErrorCode;\r
507 \r
508         /* Remove compiler warnings if configASSERT() is not defined. */\r
509         ( void ) ulErrorCode;\r
510 \r
511         /* Find the handle of the thread being deleted. */\r
512         pxThreadState = ( xThreadState * ) ( *( size_t *) pvTaskToDelete );\r
513         pvThread = pxThreadState->pvThread;\r
514 \r
515         /* Raise the Windows priority of the thread to ensure the FreeRTOS scheduler\r
516         does not run and swap it out before it is closed.  If that were to happen\r
517         the thread would never run again and effectively be a thread handle and\r
518         memory leak. */\r
519         SetThreadPriority( pvThread, THREAD_PRIORITY_HIGHEST );\r
520 \r
521         /* This function will not return, therefore a yield is set as pending to\r
522         ensure a context switch occurs away from this thread on the next tick. */\r
523         *pxPendYield = pdTRUE;\r
524 \r
525         /* Mark the thread associated with this task as invalid so\r
526         vPortDeleteThread() does not try to terminate it. */\r
527         pxThreadState->pvThread = NULL;\r
528 \r
529         /* Close the thread. */\r
530         ulErrorCode = CloseHandle( pvThread );\r
531         configASSERT( ulErrorCode );\r
532 \r
533         ExitThread( 0 );\r
534 }\r
535 /*-----------------------------------------------------------*/\r
536 \r
537 void vPortEndScheduler( void )\r
538 {\r
539         /* This function IS NOT TESTED! */\r
540         TerminateProcess( GetCurrentProcess(), 0 );\r
541 }\r
542 /*-----------------------------------------------------------*/\r
543 \r
544 void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber )\r
545 {\r
546         configASSERT( xPortRunning );\r
547 \r
548         if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )\r
549         {\r
550                 /* Yield interrupts are processed even when critical nesting is\r
551                 non-zero. */\r
552                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
553                 ulPendingInterrupts |= ( 1 << ulInterruptNumber );\r
554 \r
555                 /* The simulated interrupt is now held pending, but don't actually\r
556                 process it yet if this call is within a critical section.  It is\r
557                 possible for this to be in a critical section as calls to wait for\r
558                 mutexes are accumulative. */\r
559                 if( ulCriticalNesting == 0 )\r
560                 {\r
561                         SetEvent( pvInterruptEvent );\r
562                 }\r
563 \r
564                 ReleaseMutex( pvInterruptEventMutex );\r
565         }\r
566 }\r
567 /*-----------------------------------------------------------*/\r
568 \r
569 void vPortSetInterruptHandler( uint32_t ulInterruptNumber, uint32_t (*pvHandler)( void ) )\r
570 {\r
571         if( ulInterruptNumber < portMAX_INTERRUPTS )\r
572         {\r
573                 if( pvInterruptEventMutex != NULL )\r
574                 {\r
575                         WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
576                         ulIsrHandler[ ulInterruptNumber ] = pvHandler;\r
577                         ReleaseMutex( pvInterruptEventMutex );\r
578                 }\r
579                 else\r
580                 {\r
581                         ulIsrHandler[ ulInterruptNumber ] = pvHandler;\r
582                 }\r
583         }\r
584 }\r
585 /*-----------------------------------------------------------*/\r
586 \r
587 void vPortEnterCritical( void )\r
588 {\r
589         if( xPortRunning == pdTRUE )\r
590         {\r
591                 /* The interrupt event mutex is held for the entire critical section,\r
592                 effectively disabling (simulated) interrupts. */\r
593                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
594                 ulCriticalNesting++;\r
595         }\r
596         else\r
597         {\r
598                 ulCriticalNesting++;\r
599         }\r
600 }\r
601 /*-----------------------------------------------------------*/\r
602 \r
603 void vPortExitCritical( void )\r
604 {\r
605 int32_t lMutexNeedsReleasing;\r
606 \r
607         /* The interrupt event mutex should already be held by this thread as it was\r
608         obtained on entry to the critical section. */\r
609 \r
610         lMutexNeedsReleasing = pdTRUE;\r
611 \r
612         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
613         {\r
614                 if( ulCriticalNesting == ( portNO_CRITICAL_NESTING + 1 ) )\r
615                 {\r
616                         ulCriticalNesting--;\r
617 \r
618                         /* Were any interrupts set to pending while interrupts were\r
619                         (simulated) disabled? */\r
620                         if( ulPendingInterrupts != 0UL )\r
621                         {\r
622                                 configASSERT( xPortRunning );\r
623                                 SetEvent( pvInterruptEvent );\r
624 \r
625                                 /* Mutex will be released now, so does not require releasing\r
626                                 on function exit. */\r
627                                 lMutexNeedsReleasing = pdFALSE;\r
628                                 ReleaseMutex( pvInterruptEventMutex );\r
629                         }\r
630                 }\r
631                 else\r
632                 {\r
633                         /* Tick interrupts will still not be processed as the critical\r
634                         nesting depth will not be zero. */\r
635                         ulCriticalNesting--;\r
636                 }\r
637         }\r
638 \r
639         if( pvInterruptEventMutex != NULL )\r
640         {\r
641                 if( lMutexNeedsReleasing == pdTRUE )\r
642                 {\r
643                         configASSERT( xPortRunning );\r
644                         ReleaseMutex( pvInterruptEventMutex );\r
645                 }\r
646         }\r
647 }\r
648 /*-----------------------------------------------------------*/\r
649 \r