]> git.sur5r.net Git - freertos/blob - Source/portable/MSVC-MingW/port.c
Updated Win32 port layer so that end of interrupt events are only sent to threads...
[freertos] / Source / portable / MSVC-MingW / port.c
1 /*\r
2     FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Scheduler includes. */\r
55 #include "FreeRTOS.h"\r
56 #include "task.h"\r
57 #include <stdio.h>\r
58 \r
59 //FILE *pfTraceFile = NULL;\r
60 //#define vPortTrace( x ) if( pfTraceFile == NULL ) pfTraceFile = fopen( "c:/temp/trace.txt", "w" ); if( pfTraceFile != NULL ) fprintf( pfTraceFile, x )\r
61 #define vPortTrace( x ) ( void ) x\r
62 \r
63 #define portMAX_INTERRUPTS                              ( ( unsigned long ) sizeof( unsigned long ) * 8UL ) /* The number of bits in an unsigned long. */\r
64 #define portNO_CRITICAL_NESTING                 ( ( unsigned long ) 0 )\r
65 \r
66 /*\r
67  * Created as a high priority thread, this function uses a timer to simulate\r
68  * a tick interrupt being generated on an embedded target.  In this Windows\r
69  * environment the timer does not achieve real time performance though.\r
70  */\r
71 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );\r
72 \r
73 /* \r
74  * Process all the simulated interrupts - each represented by a bit in \r
75  * ulPendingInterrupts variable.\r
76  */\r
77 static void prvProcessEvents( void );\r
78 \r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /* The WIN32 simulator runs each task in a thread.  The context switching is\r
82 managed by the threads, so the task stack does not have to be managed directly,\r
83 although the task stack is still used to hold an xThreadState structure this is\r
84 the only thing it will ever hold.  The structure indirectly maps the task handle \r
85 to a thread handle. */\r
86 typedef struct\r
87 {\r
88         /* Set to true for tasks that call the generate psuedo interrupt function,\r
89         as the event handler needs to know whether to signal the interrupt ack\r
90         event when the task next runs. */\r
91         long lWaitingInterruptAck;                      \r
92 \r
93         /* Critical nesting count of the task - each task has its own. */\r
94         portSTACK_TYPE ulCriticalNesting;\r
95 \r
96         /* Handle of the thread that executes the task. */\r
97         void * pvThread;                                        \r
98 } xThreadState;\r
99 \r
100 /* Pseudo interrupts waiting to be processed.  This is a bit mask where each\r
101 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */\r
102 static volatile unsigned long ulPendingInterrupts = 0UL;\r
103 \r
104 /* An event used to inform the interrupt dispatch thread (a high priority thread\r
105 that simulated interrupt processing) that an IRQ or SWI type interrupt is\r
106 pending. */\r
107 static void *pvInterruptEvent = NULL;\r
108 \r
109 /* Mutex used to protect all the pseudo interrupt variables that are accessed by\r
110 multiple threads. */\r
111 static void *pvInterruptEventMutex = NULL;\r
112 \r
113 /* The main thread, which also acts as the pseudo interrupt handler. */\r
114 static void *pvMainThreadAndInterrupHandler;\r
115 \r
116 /* Events used to manage sequencing. */\r
117 static void *pvTickAcknowledgeEvent = NULL, *pvInterruptAcknowledgeEvent = NULL;\r
118 \r
119 /* The critical nesting count for the currently executing task.  This is \r
120 initialised to a non-zero value so interrupts do not become enabled during \r
121 the initialisation phase.  As each task has its own critical nesting value \r
122 ulCriticalNesting will get set to zero when the first task runs.  This \r
123 initialisation is probably not critical in this simulated environment as the\r
124 pseudo interrupt handlers/dispatchers do not get created until the FreeRTOS\r
125 scheduler is started. */\r
126 static unsigned portLONG ulCriticalNesting = 9999UL;\r
127 \r
128 /* Handlers for all the simulated software interrupts.  The first two positions\r
129 are used for the Yield and Tick interrupts so are handled slightly differently,\r
130 all the other interrupts can be user defined. */\r
131 static void (*vIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };\r
132 \r
133 /* Pointer to the TCB of the currently executing task. */\r
134 extern void *pxCurrentTCB;\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )\r
139 {\r
140 void *pvTimer;\r
141 LARGE_INTEGER liDueTime;\r
142 void *pvObjectList[ 2 ];\r
143 const long long ll_ms_In_100ns_units = ( long long ) -1000;\r
144 \r
145         /* Just to prevent compiler warnings. */\r
146         ( void ) lpParameter;\r
147 \r
148         /* The timer is created as a one shot timer even though we want it to repeat\r
149         at a given frequency.  This is because Windows is not a real time environment,\r
150         and attempting to set a high frequency periodic timer will result in event\r
151         overruns.  Therefore the timer is just reset after each time the pseudo \r
152         interrupt handler has processed each tick event. */\r
153         pvTimer = CreateWaitableTimer( NULL, TRUE, NULL );\r
154         \r
155         liDueTime.QuadPart = ( long long ) portTICK_RATE_MS * ll_ms_In_100ns_units;\r
156 \r
157         /* Block on the timer itself and the event mutex that grants access to the \r
158         interrupt variables. */\r
159         pvObjectList[ 0 ] = pvInterruptEventMutex;\r
160         pvObjectList[ 1 ] = pvTimer;\r
161 \r
162         for(;;)\r
163         {\r
164                 /* The timer is reset on each itteration of this loop rather than being set\r
165                 to function periodicallys - this is for the reasons stated in the comments\r
166                 where the timer is created. */\r
167                 vPortTrace( "prvSimulatedPeripheralTimer: Tick acked, setting new tick timer\r\n" );\r
168                 SetWaitableTimer( pvTimer, &liDueTime, 0, NULL, NULL, TRUE );\r
169 \r
170                 /* Wait until the timer expires and we can access the pseudo interrupt \r
171                 variables. */\r
172                 //WaitForMultipleObjects( ( sizeof( pvObjectList ) / sizeof( void * ) ), pvObjectList, TRUE, INFINITE );\r
173                 WaitForSingleObject( pvTimer, INFINITE );\r
174                 vPortTrace( "prvSimulatedPeripheralTimer: Timer signalled, waiting interrupt event mutex\r\n" );\r
175                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
176                 vPortTrace( "prvSimulatedPeripheralTimer: Got interrupt event mutex\r\n" );\r
177                 \r
178                 /* The timer has expired, generate the simulated tick event. */\r
179                 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );\r
180                 if( pvInterruptEvent != NULL )\r
181                 {\r
182                         vPortTrace( "prvSimulatedPeripheralTimer: Setting interrupt event to signal tick\r\n" );\r
183                         SetEvent( pvInterruptEvent );\r
184                 }\r
185 \r
186                 /* Give back the mutex so the pseudo interrupt handler unblocks and can\r
187                 access the interrupt handler variables.  This high priority task will then\r
188                 loop back round to wait for the lower priority psuedo interrupt handler \r
189                 thread to acknowledge the tick. */\r
190                 if( pvInterruptEventMutex != NULL )\r
191                 {\r
192                         vPortTrace( "prvSimulatedPeripheralTimer: Releasing interrupt event mutex so tick can be processed\r\n" );\r
193                         ReleaseMutex( pvInterruptEventMutex );\r
194                 }\r
195 \r
196                 /* Wait for the previous tick to be acknowledged before resetting the timer.\r
197                 As mentioned above this is done to prevent timer overruns in the non real-\r
198                 time environment.  THIS IS NOT HOW A REAL PORT SHOULD USE TIMERS! */\r
199                 WaitForSingleObject( pvTickAcknowledgeEvent, INFINITE );\r
200         }\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
205 {\r
206 xThreadState *pxThreadState = NULL;\r
207 \r
208         /* In this simulated case a stack is not initialised, but instead a thread\r
209         is created that will execute the task being created.  The thread handles\r
210         the context switching itself.  The xThreadState object is placed onto\r
211         the stack that was created for the task - so the stack buffer is still\r
212         used, just not in the conventional way.  It will not be used for anything\r
213         other than holding this structure. */\r
214         pxThreadState = ( xThreadState * ) ( pxTopOfStack - sizeof( xThreadState ) );\r
215 \r
216                 /* Create the thread itself. */\r
217                 pxThreadState->pvThread = ( void * ) CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED, NULL );\r
218                 pxThreadState->ulCriticalNesting = portNO_CRITICAL_NESTING;\r
219                 pxThreadState->lWaitingInterruptAck = pdFALSE;\r
220                 SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_IDLE );\r
221         \r
222         return ( portSTACK_TYPE * ) pxThreadState;\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 portBASE_TYPE xPortStartScheduler( void )\r
227 {\r
228 void *pvHandle;\r
229 long lSuccess = pdPASS;\r
230 xThreadState *pxThreadState;\r
231 \r
232         /* Set the priority of this thread such that it is above the priority of the\r
233         threads that run tasks, but below the priority of the thread that generates\r
234         the pseudo tick interrupts.  This priority is chosen because this is the\r
235         thread that actually handles the psuedo interrupts. */\r
236         pvHandle = GetCurrentThread();\r
237         if( pvHandle == NULL )\r
238         {\r
239                 lSuccess = pdFAIL;\r
240         }\r
241         \r
242         if( lSuccess == pdPASS )\r
243         {\r
244                 if( SetThreadPriority( pvHandle, THREAD_PRIORITY_BELOW_NORMAL ) == 0 )\r
245                 {\r
246                         lSuccess = pdFAIL;\r
247                 }\r
248         }\r
249 \r
250         if( lSuccess == pdPASS )\r
251         {\r
252                 /* Create the events and mutexes that are used to synchronise all the\r
253                 threads. */\r
254                 pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );\r
255                 pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
256                 pvTickAcknowledgeEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
257                 pvInterruptAcknowledgeEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
258 \r
259                 /* Start the thread that simulates the timer peripheral to generate\r
260                 tick interrupts. */\r
261                 pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, 0, NULL );\r
262                 if( pvHandle != NULL )\r
263                 {\r
264                         SetThreadPriority( pvHandle, THREAD_PRIORITY_ABOVE_NORMAL );\r
265                 }\r
266                 \r
267                 /* Start the highest priority task by obtaining its associated thread state\r
268                 structure, in which is stored the thread handle. */\r
269                 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );\r
270                 ulCriticalNesting = portNO_CRITICAL_NESTING;\r
271 \r
272                 vPortTrace( "Created system threads, starting task" );\r
273 \r
274                 ResumeThread( pxThreadState->pvThread );\r
275         }       \r
276         \r
277         /* Handle all pseudo interrupts - including yield requests and simulated ticks. */\r
278         prvProcessEvents();\r
279 \r
280         /* Would not expect to return from prvProcessEvents(), so should not get here. */\r
281         return 0;\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 static void prvProcessEvents( void )\r
286 {\r
287 long lSwitchRequired;\r
288 xThreadState *pxThreadState;\r
289 void *pvObjectList[ 2 ];\r
290 unsigned long i;\r
291 //char cTraceBuffer[ 256 ];\r
292 \r
293         vPortTrace( "Entering prvProcessEvents\r\n" );\r
294 \r
295         /* Going to block on the mutex that ensured exclusive access to the pdeudo \r
296         interrupt objects, and the event that signals that an interrupt is waiting\r
297         to be processed. */\r
298         pvObjectList[ 0 ] = pvInterruptEventMutex;\r
299         pvObjectList[ 1 ] = pvInterruptEvent;\r
300 \r
301         for(;;)\r
302         {\r
303                 vPortTrace( "prvProcessEvents: Waiting for next interrupt event\r\n" );\r
304                 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );\r
305                 vPortTrace( "prvProcessEvents: Got interrupt event and mutex\r\n" );\r
306                 //vPortTrace( "prvProcessEvents: Waiting for next interrupt event\r\n" );\r
307                 //WaitForSingleObject( pvInterruptEvent, INFINITE );\r
308                 //vPortTrace( "prvProcessEvents: Waiting interrupt event mutex to access interrupt data\r\n" );\r
309                 //WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
310 \r
311                 lSwitchRequired = pdFALSE;\r
312 \r
313                 /* For each interrupt we are interested in processing, each of which is \r
314                 represented by a bit in the 32bit ulPendingInterrupts variable. */\r
315                 for( i = 0; i < portMAX_INTERRUPTS; i++ )\r
316                 {\r
317                         /* Is the pseudo interrupt pending? */\r
318                         if( ulPendingInterrupts & ( 1UL << i ) )\r
319                         {\r
320                                 switch( i )\r
321                                 {\r
322                                         case portINTERRUPT_YIELD:\r
323 \r
324                                                 vPortTrace( "prvProcessEvents: Processing Yield\r\n" );\r
325                                                 /* Yield interrupts occur no matter what the critical nesting count. */\r
326                                                 lSwitchRequired = pdTRUE;\r
327 \r
328                                                 /* Clear the interrupt pending bit. */\r
329                                                 ulPendingInterrupts &= ~( 1UL << portINTERRUPT_YIELD );\r
330                                                 break;\r
331 \r
332                                         case portINTERRUPT_TICK:\r
333                                         \r
334                                                 /* Tick interrupts should only be processed if the critical nesting count\r
335                                                 is zero.  The critical nesting count represents the interrupt mask on \r
336                                                 real target hardware. */\r
337                                                 vPortTrace( "prvProcessEvents: Processing tick event\r\n" );\r
338                                                 if( ulCriticalNesting == 0 )\r
339                                                 {\r
340                                                         /* Process the tick itself. */\r
341                                                         vPortTrace( "prvProcessEvents: Incrementing tick\r\n" );\r
342                                                         vTaskIncrementTick();\r
343                                                         #if( configUSE_PREEMPTION != 0 )\r
344                                                         {\r
345                                                                 /* A context switch is only automatically performed from the tick\r
346                                                                 interrupt if the pre-emptive scheduler is being used. */\r
347                                                                 lSwitchRequired = pdTRUE;\r
348                                                         }\r
349                                                         #endif\r
350                                                         \r
351                                                         vPortTrace( "prvProcessEvents: Acking tick\r\n" );\r
352                                                         SetEvent( pvTickAcknowledgeEvent );\r
353 \r
354                                                         /* Clear the interrupt pending bit. */\r
355                                                         ulPendingInterrupts &= ~( 1UL << portINTERRUPT_TICK );\r
356                                                 }\r
357                                                 break;\r
358 \r
359                                         default:\r
360 \r
361                                                 /* Is a handler installed? */\r
362                                                 if( vIsrHandler[ i ] != NULL )\r
363                                                 {\r
364                                                         lSwitchRequired = pdTRUE;\r
365 \r
366                                                         /* Run the actual handler. */\r
367                                                         vIsrHandler[ i ]();\r
368 \r
369                                                         /* Clear the interrupt pending bit. */\r
370                                                         ulPendingInterrupts &= ~( 1UL << i );\r
371 \r
372                                                         /* TODO:  Need to have some sort of handshake event here for non-tick\r
373                                                         and none yield interrupts. */\r
374                                                 }\r
375                                                 break;\r
376                                 }\r
377                         }\r
378                 }\r
379 \r
380                 if( lSwitchRequired != pdFALSE )\r
381                 {\r
382                         void *pvOldCurrentTCB;\r
383 \r
384                         pvOldCurrentTCB = pxCurrentTCB;\r
385 \r
386                         /* Save the state of the current thread before suspending it. */\r
387                         pxThreadState = ( xThreadState *) *( ( unsigned long * ) pxCurrentTCB );\r
388                         pxThreadState->ulCriticalNesting = ulCriticalNesting ;\r
389                         \r
390                         /* Select the next task to run. */\r
391                         vTaskSwitchContext();\r
392                         \r
393                         /* If the task selected to enter the running state is not the task\r
394                         that is already in the running state. */\r
395                         if( pvOldCurrentTCB != pxCurrentTCB )\r
396                         {\r
397                                 /* Suspend the old thread. */\r
398                                 SuspendThread( pxThreadState->pvThread );\r
399                                 //sprintf( cTraceBuffer, "Event processor: suspending %s, resuming %s\r\n", ((xTCB*)pvOldCurrentTCB)->pcTaskName, ((xTCB*)pxCurrentTCB)->pcTaskName );\r
400                                 //vPortTrace( cTraceBuffer );\r
401 \r
402                                 /* Obtain the state of the task now selected to enter the Running state. */\r
403                                 pxThreadState = ( xThreadState * ) ( *( unsigned long *) pxCurrentTCB );\r
404                                 ulCriticalNesting = pxThreadState->ulCriticalNesting;\r
405                                 ResumeThread( pxThreadState->pvThread );\r
406 \r
407                                 if( pxThreadState->lWaitingInterruptAck == pdTRUE )\r
408                                 {\r
409                                         pxThreadState->lWaitingInterruptAck = pdFALSE;\r
410                                         vPortTrace( "prvProcessEvents: Acking interrupt\r\n" );\r
411                                         SetEvent( pvInterruptAcknowledgeEvent );\r
412                                 }\r
413                         }\r
414                 }\r
415 \r
416                 ReleaseMutex( pvInterruptEventMutex );\r
417         }\r
418 }\r
419 /*-----------------------------------------------------------*/\r
420 \r
421 void vPortEndScheduler( void )\r
422 {\r
423 }\r
424 /*-----------------------------------------------------------*/\r
425 \r
426 void vPortGeneratePseudoInterrupt( unsigned long ulInterruptNumber )\r
427 {\r
428 xThreadState *pxThreadState;\r
429 \r
430         if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )\r
431         {\r
432                 /* Yield interrupts are processed even when critical nesting is non-zero. */\r
433                 if( ( ulCriticalNesting == 0 ) || ( ulInterruptNumber == portINTERRUPT_YIELD ) )\r
434                 {\r
435                         WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
436                         ulPendingInterrupts |= ( 1 << ulInterruptNumber );\r
437 \r
438                         /* The event handler needs to know to signal the interrupt acknowledge event\r
439                         the next time this task runs. */\r
440                         pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );\r
441                         pxThreadState->lWaitingInterruptAck = pdTRUE;\r
442 \r
443                         vPortTrace( "vPortGeneratePseudoInterrupt: Got interrupt mutex, about to signal interrupt event\r\n" );\r
444                         SetEvent( pvInterruptEvent );\r
445                         vPortTrace( "vPortGeneratePseudoInterrupt: About to release interrupt event mutex\r\n" );\r
446                         ReleaseMutex( pvInterruptEventMutex );\r
447                         vPortTrace( "vPortGeneratePseudoInterrupt: Interrupt event mutex released, going to wait for interrupt ack\r\n" );\r
448 \r
449                         WaitForSingleObject( pvInterruptAcknowledgeEvent, INFINITE );\r
450                         vPortTrace( "vPortGeneratePseudoInterrupt: Interrupt acknowledged, leaving vPortGeneratePseudoInterrupt()\r\n" );\r
451                 }\r
452         }\r
453 }\r
454 /*-----------------------------------------------------------*/\r
455 \r
456 void vPortSetInterruptHandler( unsigned long ulInterruptNumber, void (*pvHandler)( void ) )\r
457 {\r
458         if( ulInterruptNumber < portMAX_INTERRUPTS )\r
459         {\r
460                 if( pvInterruptEventMutex != NULL )\r
461                 {\r
462                         WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
463                         vIsrHandler[ ulInterruptNumber ] = pvHandler;\r
464                         ReleaseMutex( pvInterruptEventMutex );\r
465                 }\r
466                 else\r
467                 {\r
468                         vIsrHandler[ ulInterruptNumber ] = pvHandler;\r
469                 }\r
470         }\r
471 }\r
472 /*-----------------------------------------------------------*/\r
473 \r
474 void vPortEnterCritical( void )\r
475 {\r
476         ulCriticalNesting++;\r
477 }\r
478 /*-----------------------------------------------------------*/\r
479 \r
480 void vPortExitCritical( void )\r
481 {\r
482 xThreadState *pxThreadState;\r
483 \r
484         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
485         {\r
486                 ulCriticalNesting--;\r
487 \r
488                 if( ulCriticalNesting == 0 )\r
489                 {\r
490                         /* Were any interrupts set to pending while interrupts were \r
491                         (pseudo) disabled? */\r
492                         if( ulPendingInterrupts != 0UL )\r
493                         {\r
494                                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
495                                 vPortTrace( "vPortExitCritical:  Setting interrupt event\r\n" );\r
496                                 SetEvent( pvInterruptEvent );\r
497 \r
498                                 /* The event handler needs to know to signal the interrupt acknowledge event\r
499                                 the next time this task runs. */\r
500                                 pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );\r
501                                 pxThreadState->lWaitingInterruptAck = pdTRUE;\r
502 \r
503                                 ReleaseMutex( pvInterruptEventMutex );\r
504 \r
505                                 vPortTrace( "vPortExitCritical:  Waiting interrupt ack\r\n" );\r
506                                 WaitForSingleObject( pvInterruptAcknowledgeEvent, INFINITE );\r
507                                 vPortTrace( "vPortExitCritical: Interrupt acknowledged, leaving critical section code\r\n" );\r
508                         }\r
509                 }\r
510         }\r
511 }\r