]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MSVC-MingW/port.c
a755621edd7433622e6ad083339838705f18b563
[freertos] / FreeRTOS / Source / portable / MSVC-MingW / port.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Standard includes. */\r
29 #include <stdio.h>\r
30 \r
31 /* Scheduler includes. */\r
32 #include "FreeRTOS.h"\r
33 #include "task.h"\r
34 \r
35 #ifdef __GNUC__\r
36         #include "mmsystem.h"\r
37 #else\r
38         #pragma comment(lib, "winmm.lib")\r
39 #endif\r
40 \r
41 #define portMAX_INTERRUPTS                              ( ( uint32_t ) sizeof( uint32_t ) * 8UL ) /* The number of bits in an uint32_t. */\r
42 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
43 \r
44 /* The priorities at which the various components of the simulation execute. */\r
45 #define portDELETE_SELF_THREAD_PRIORITY                  THREAD_PRIORITY_TIME_CRITICAL /* Must be highest. */\r
46 #define portSIMULATED_INTERRUPTS_THREAD_PRIORITY THREAD_PRIORITY_TIME_CRITICAL\r
47 #define portSIMULATED_TIMER_THREAD_PRIORITY              THREAD_PRIORITY_HIGHEST\r
48 #define portTASK_THREAD_PRIORITY                                 THREAD_PRIORITY_ABOVE_NORMAL\r
49 \r
50 /*\r
51  * Created as a high priority thread, this function uses a timer to simulate\r
52  * a tick interrupt being generated on an embedded target.  In this Windows\r
53  * environment the timer does not achieve anything approaching real time\r
54  * performance though.\r
55  */\r
56 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );\r
57 \r
58 /*\r
59  * Process all the simulated interrupts - each represented by a bit in\r
60  * ulPendingInterrupts variable.\r
61  */\r
62 static void prvProcessSimulatedInterrupts( void );\r
63 \r
64 /*\r
65  * Interrupt handlers used by the kernel itself.  These are executed from the\r
66  * simulated interrupt handler thread.\r
67  */\r
68 static uint32_t prvProcessYieldInterrupt( void );\r
69 static uint32_t prvProcessTickInterrupt( void );\r
70 \r
71 /*\r
72  * Called when the process exits to let Windows know the high timer resolution\r
73  * is no longer required.\r
74  */\r
75 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType );\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /* The WIN32 simulator runs each task in a thread.  The context switching is\r
80 managed by the threads, so the task stack does not have to be managed directly,\r
81 although the task stack is still used to hold an xThreadState structure this is\r
82 the only thing it will ever hold.  The structure indirectly maps the task handle\r
83 to a thread handle. */\r
84 typedef struct\r
85 {\r
86         /* Handle of the thread that executes the task. */\r
87         void *pvThread;\r
88 \r
89 } xThreadState;\r
90 \r
91 /* Simulated interrupts waiting to be processed.  This is a bit mask where each\r
92 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */\r
93 static volatile uint32_t ulPendingInterrupts = 0UL;\r
94 \r
95 /* An event used to inform the simulated interrupt processing thread (a high\r
96 priority thread that simulated interrupt processing) that an interrupt is\r
97 pending. */\r
98 static void *pvInterruptEvent = NULL;\r
99 \r
100 /* Mutex used to protect all the simulated interrupt variables that are accessed\r
101 by multiple threads. */\r
102 static void *pvInterruptEventMutex = NULL;\r
103 \r
104 /* The critical nesting count for the currently executing task.  This is\r
105 initialised to a non-zero value so interrupts do not become enabled during\r
106 the initialisation phase.  As each task has its own critical nesting value\r
107 ulCriticalNesting will get set to zero when the first task runs.  This\r
108 initialisation is probably not critical in this simulated environment as the\r
109 simulated interrupt handlers do not get created until the FreeRTOS scheduler is\r
110 started anyway. */\r
111 static uint32_t ulCriticalNesting = 9999UL;\r
112 \r
113 /* Handlers for all the simulated software interrupts.  The first two positions\r
114 are used for the Yield and Tick interrupts so are handled slightly differently,\r
115 all the other interrupts can be user defined. */\r
116 static uint32_t (*ulIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };\r
117 \r
118 /* Pointer to the TCB of the currently executing task. */\r
119 extern void *pxCurrentTCB;\r
120 \r
121 /* Used to ensure nothing is processed during the startup sequence. */\r
122 static BaseType_t xPortRunning = pdFALSE;\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )\r
127 {\r
128 TickType_t xMinimumWindowsBlockTime;\r
129 TIMECAPS xTimeCaps;\r
130 \r
131         /* Set the timer resolution to the maximum possible. */\r
132         if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )\r
133         {\r
134                 xMinimumWindowsBlockTime = ( TickType_t ) xTimeCaps.wPeriodMin;\r
135                 timeBeginPeriod( xTimeCaps.wPeriodMin );\r
136 \r
137                 /* Register an exit handler so the timeBeginPeriod() function can be\r
138                 matched with a timeEndPeriod() when the application exits. */\r
139                 SetConsoleCtrlHandler( prvEndProcess, TRUE );\r
140         }\r
141         else\r
142         {\r
143                 xMinimumWindowsBlockTime = ( TickType_t ) 20;\r
144         }\r
145 \r
146         /* Just to prevent compiler warnings. */\r
147         ( void ) lpParameter;\r
148 \r
149         for( ;; )\r
150         {\r
151                 /* Wait until the timer expires and we can access the simulated interrupt\r
152                 variables.  *NOTE* this is not a 'real time' way of generating tick\r
153                 events as the next wake time should be relative to the previous wake\r
154                 time, not the time that Sleep() is called.  It is done this way to\r
155                 prevent overruns in this very non real time simulated/emulated\r
156                 environment. */\r
157                 if( portTICK_PERIOD_MS < xMinimumWindowsBlockTime )\r
158                 {\r
159                         Sleep( xMinimumWindowsBlockTime );\r
160                 }\r
161                 else\r
162                 {\r
163                         Sleep( portTICK_PERIOD_MS );\r
164                 }\r
165 \r
166                 configASSERT( xPortRunning );\r
167 \r
168                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
169 \r
170                 /* The timer has expired, generate the simulated tick event. */\r
171                 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );\r
172 \r
173                 /* The interrupt is now pending - notify the simulated interrupt\r
174                 handler thread. */\r
175                 if( ulCriticalNesting == 0 )\r
176                 {\r
177                         SetEvent( pvInterruptEvent );\r
178                 }\r
179 \r
180                 /* Give back the mutex so the simulated interrupt handler unblocks\r
181                 and can access the interrupt handler variables. */\r
182                 ReleaseMutex( pvInterruptEventMutex );\r
183         }\r
184 \r
185         #ifdef __GNUC__\r
186                 /* Should never reach here - MingW complains if you leave this line out,\r
187                 MSVC complains if you put it in. */\r
188                 return 0;\r
189         #endif\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType )\r
194 {\r
195 TIMECAPS xTimeCaps;\r
196 \r
197         ( void ) dwCtrlType;\r
198 \r
199         if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )\r
200         {\r
201                 /* Match the call to timeBeginPeriod( xTimeCaps.wPeriodMin ) made when\r
202                 the process started with a timeEndPeriod() as the process exits. */\r
203                 timeEndPeriod( xTimeCaps.wPeriodMin );\r
204         }\r
205 \r
206         return pdFALSE;\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
211 {\r
212 xThreadState *pxThreadState = NULL;\r
213 int8_t *pcTopOfStack = ( int8_t * ) pxTopOfStack;\r
214 const SIZE_T xStackSize = 1024; /* Set the size to a small number which will get rounded up to the minimum possible. */\r
215 \r
216         /* In this simulated case a stack is not initialised, but instead a thread\r
217         is created that will execute the task being created.  The thread handles\r
218         the context switching itself.  The xThreadState object is placed onto\r
219         the stack that was created for the task - so the stack buffer is still\r
220         used, just not in the conventional way.  It will not be used for anything\r
221         other than holding this structure. */\r
222         pxThreadState = ( xThreadState * ) ( pcTopOfStack - sizeof( xThreadState ) );\r
223 \r
224         /* Create the thread itself. */\r
225         pxThreadState->pvThread = CreateThread( NULL, xStackSize, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION, NULL );\r
226         configASSERT( pxThreadState->pvThread ); /* See comment where TerminateThread() is called. */\r
227         SetThreadAffinityMask( pxThreadState->pvThread, 0x01 );\r
228         SetThreadPriorityBoost( pxThreadState->pvThread, TRUE );\r
229         SetThreadPriority( pxThreadState->pvThread, portTASK_THREAD_PRIORITY );\r
230 \r
231         return ( StackType_t * ) pxThreadState;\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 BaseType_t xPortStartScheduler( void )\r
236 {\r
237 void *pvHandle = NULL;\r
238 int32_t lSuccess;\r
239 xThreadState *pxThreadState = NULL;\r
240 SYSTEM_INFO xSystemInfo;\r
241 \r
242         /* This port runs windows threads with extremely high priority.  All the\r
243         threads execute on the same core - to prevent locking up the host only start\r
244         if the host has multiple cores. */\r
245         GetSystemInfo( &xSystemInfo );\r
246         if( xSystemInfo.dwNumberOfProcessors <= 1 )\r
247         {\r
248                 printf( "This version of the FreeRTOS Windows port can only be used on multi-core hosts.\r\n" );\r
249                 lSuccess = pdFAIL;\r
250         }\r
251         else\r
252         {\r
253                 lSuccess = pdPASS;\r
254 \r
255                 /* The highest priority class is used to [try to] prevent other Windows\r
256                 activity interfering with FreeRTOS timing too much. */\r
257                 if( SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS ) == 0 )\r
258                 {\r
259                         printf( "SetPriorityClass() failed\r\n" );\r
260                 }\r
261 \r
262                 /* Install the interrupt handlers used by the scheduler itself. */\r
263                 vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );\r
264                 vPortSetInterruptHandler( portINTERRUPT_TICK, prvProcessTickInterrupt );\r
265 \r
266                 /* Create the events and mutexes that are used to synchronise all the\r
267                 threads. */\r
268                 pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );\r
269                 pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
270 \r
271                 if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) )\r
272                 {\r
273                         lSuccess = pdFAIL;\r
274                 }\r
275 \r
276                 /* Set the priority of this thread such that it is above the priority of\r
277                 the threads that run tasks.  This higher priority is required to ensure\r
278                 simulated interrupts take priority over tasks. */\r
279                 pvHandle = GetCurrentThread();\r
280                 if( pvHandle == NULL )\r
281                 {\r
282                         lSuccess = pdFAIL;\r
283                 }\r
284         }\r
285 \r
286         if( lSuccess == pdPASS )\r
287         {\r
288                 if( SetThreadPriority( pvHandle, portSIMULATED_INTERRUPTS_THREAD_PRIORITY ) == 0 )\r
289                 {\r
290                         lSuccess = pdFAIL;\r
291                 }\r
292                 SetThreadPriorityBoost( pvHandle, TRUE );\r
293                 SetThreadAffinityMask( pvHandle, 0x01 );\r
294         }\r
295 \r
296         if( lSuccess == pdPASS )\r
297         {\r
298                 /* Start the thread that simulates the timer peripheral to generate\r
299                 tick interrupts.  The priority is set below that of the simulated\r
300                 interrupt handler so the interrupt event mutex is used for the\r
301                 handshake / overrun protection. */\r
302                 pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, CREATE_SUSPENDED, NULL );\r
303                 if( pvHandle != NULL )\r
304                 {\r
305                         SetThreadPriority( pvHandle, portSIMULATED_TIMER_THREAD_PRIORITY );\r
306                         SetThreadPriorityBoost( pvHandle, TRUE );\r
307                         SetThreadAffinityMask( pvHandle, 0x01 );\r
308                         ResumeThread( pvHandle );\r
309                 }\r
310 \r
311                 /* Start the highest priority task by obtaining its associated thread\r
312                 state structure, in which is stored the thread handle. */\r
313                 pxThreadState = ( xThreadState * ) *( ( size_t * ) pxCurrentTCB );\r
314                 ulCriticalNesting = portNO_CRITICAL_NESTING;\r
315 \r
316                 /* Bump up the priority of the thread that is going to run, in the\r
317                 hope that this will assist in getting the Windows thread scheduler to\r
318                 behave as an embedded engineer might expect. */\r
319                 ResumeThread( pxThreadState->pvThread );\r
320 \r
321                 /* Handle all simulated interrupts - including yield requests and\r
322                 simulated ticks. */\r
323                 prvProcessSimulatedInterrupts();\r
324         }\r
325 \r
326         /* Would not expect to return from prvProcessSimulatedInterrupts(), so should\r
327         not get here. */\r
328         return 0;\r
329 }\r
330 /*-----------------------------------------------------------*/\r
331 \r
332 static uint32_t prvProcessYieldInterrupt( void )\r
333 {\r
334         return pdTRUE;\r
335 }\r
336 /*-----------------------------------------------------------*/\r
337 \r
338 static uint32_t prvProcessTickInterrupt( void )\r
339 {\r
340 uint32_t ulSwitchRequired;\r
341 \r
342         /* Process the tick itself. */\r
343         configASSERT( xPortRunning );\r
344         ulSwitchRequired = ( uint32_t ) xTaskIncrementTick();\r
345 \r
346         return ulSwitchRequired;\r
347 }\r
348 /*-----------------------------------------------------------*/\r
349 \r
350 static void prvProcessSimulatedInterrupts( void )\r
351 {\r
352 uint32_t ulSwitchRequired, i;\r
353 xThreadState *pxThreadState;\r
354 void *pvObjectList[ 2 ];\r
355 CONTEXT xContext;\r
356 \r
357         /* Going to block on the mutex that ensured exclusive access to the simulated\r
358         interrupt objects, and the event that signals that a simulated interrupt\r
359         should be processed. */\r
360         pvObjectList[ 0 ] = pvInterruptEventMutex;\r
361         pvObjectList[ 1 ] = pvInterruptEvent;\r
362 \r
363         /* Create a pending tick to ensure the first task is started as soon as\r
364         this thread pends. */\r
365         ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );\r
366         SetEvent( pvInterruptEvent );\r
367 \r
368         xPortRunning = pdTRUE;\r
369 \r
370         for(;;)\r
371         {\r
372                 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );\r
373 \r
374                 /* Used to indicate whether the simulated interrupt processing has\r
375                 necessitated a context switch to another task/thread. */\r
376                 ulSwitchRequired = pdFALSE;\r
377 \r
378                 /* For each interrupt we are interested in processing, each of which is\r
379                 represented by a bit in the 32bit ulPendingInterrupts variable. */\r
380                 for( i = 0; i < portMAX_INTERRUPTS; i++ )\r
381                 {\r
382                         /* Is the simulated interrupt pending? */\r
383                         if( ulPendingInterrupts & ( 1UL << i ) )\r
384                         {\r
385                                 /* Is a handler installed? */\r
386                                 if( ulIsrHandler[ i ] != NULL )\r
387                                 {\r
388                                         /* Run the actual handler. */\r
389                                         if( ulIsrHandler[ i ]() != pdFALSE )\r
390                                         {\r
391                                                 ulSwitchRequired |= ( 1 << i );\r
392                                         }\r
393                                 }\r
394 \r
395                                 /* Clear the interrupt pending bit. */\r
396                                 ulPendingInterrupts &= ~( 1UL << i );\r
397                         }\r
398                 }\r
399 \r
400                 if( ulSwitchRequired != pdFALSE )\r
401                 {\r
402                         void *pvOldCurrentTCB;\r
403 \r
404                         pvOldCurrentTCB = pxCurrentTCB;\r
405 \r
406                         /* Select the next task to run. */\r
407                         vTaskSwitchContext();\r
408 \r
409                         /* If the task selected to enter the running state is not the task\r
410                         that is already in the running state. */\r
411                         if( pvOldCurrentTCB != pxCurrentTCB )\r
412                         {\r
413                                 /* Suspend the old thread. */\r
414                                 pxThreadState = ( xThreadState *) *( ( size_t * ) pvOldCurrentTCB );\r
415                                 SuspendThread( pxThreadState->pvThread );\r
416 \r
417                                 /* Ensure the thread is actually suspended by performing a\r
418                                 synchronous operation that can only complete when the thread is\r
419                                 actually suspended.  The below code asks for dummy register\r
420                                 data. */\r
421                                 xContext.ContextFlags = CONTEXT_INTEGER;\r
422                                 ( void ) GetThreadContext( pxThreadState->pvThread, &xContext );\r
423 \r
424                                 /* Obtain the state of the task now selected to enter the\r
425                                 Running state. */\r
426                                 pxThreadState = ( xThreadState * ) ( *( size_t *) pxCurrentTCB );\r
427                                 ResumeThread( pxThreadState->pvThread );\r
428                         }\r
429                 }\r
430 \r
431                 ReleaseMutex( pvInterruptEventMutex );\r
432         }\r
433 }\r
434 /*-----------------------------------------------------------*/\r
435 \r
436 void vPortDeleteThread( void *pvTaskToDelete )\r
437 {\r
438 xThreadState *pxThreadState;\r
439 uint32_t ulErrorCode;\r
440 \r
441         /* Remove compiler warnings if configASSERT() is not defined. */\r
442         ( void ) ulErrorCode;\r
443 \r
444         /* Find the handle of the thread being deleted. */\r
445         pxThreadState = ( xThreadState * ) ( *( size_t *) pvTaskToDelete );\r
446 \r
447         /* Check that the thread is still valid, it might have been closed by\r
448         vPortCloseRunningThread() - which will be the case if the task associated\r
449         with the thread originally deleted itself rather than being deleted by a\r
450         different task. */\r
451         if( pxThreadState->pvThread != NULL )\r
452         {\r
453                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
454 \r
455                 /* !!! This is not a nice way to terminate a thread, and will eventually\r
456                 result in resources being depleted if tasks frequently delete other\r
457                 tasks (rather than deleting themselves) as the task stacks will not be\r
458                 freed. */\r
459                 ulErrorCode = TerminateThread( pxThreadState->pvThread, 0 );\r
460                 configASSERT( ulErrorCode );\r
461 \r
462                 ulErrorCode = CloseHandle( pxThreadState->pvThread );\r
463                 configASSERT( ulErrorCode );\r
464 \r
465                 ReleaseMutex( pvInterruptEventMutex );\r
466         }\r
467 }\r
468 /*-----------------------------------------------------------*/\r
469 \r
470 void vPortCloseRunningThread( void *pvTaskToDelete, volatile BaseType_t *pxPendYield )\r
471 {\r
472 xThreadState *pxThreadState;\r
473 void *pvThread;\r
474 uint32_t ulErrorCode;\r
475 \r
476         /* Remove compiler warnings if configASSERT() is not defined. */\r
477         ( void ) ulErrorCode;\r
478 \r
479         /* Find the handle of the thread being deleted. */\r
480         pxThreadState = ( xThreadState * ) ( *( size_t *) pvTaskToDelete );\r
481         pvThread = pxThreadState->pvThread;\r
482 \r
483         /* Raise the Windows priority of the thread to ensure the FreeRTOS scheduler\r
484         does not run and swap it out before it is closed.  If that were to happen\r
485         the thread would never run again and effectively be a thread handle and\r
486         memory leak. */\r
487         SetThreadPriority( pvThread, portDELETE_SELF_THREAD_PRIORITY );\r
488 \r
489         /* This function will not return, therefore a yield is set as pending to\r
490         ensure a context switch occurs away from this thread on the next tick. */\r
491         *pxPendYield = pdTRUE;\r
492 \r
493         /* Mark the thread associated with this task as invalid so\r
494         vPortDeleteThread() does not try to terminate it. */\r
495         pxThreadState->pvThread = NULL;\r
496 \r
497         /* Close the thread. */\r
498         ulErrorCode = CloseHandle( pvThread );\r
499         configASSERT( ulErrorCode );\r
500 \r
501         /* This is called from a critical section, which must be exited before the\r
502         thread stops. */\r
503         taskEXIT_CRITICAL();\r
504 \r
505         ExitThread( 0 );\r
506 }\r
507 /*-----------------------------------------------------------*/\r
508 \r
509 void vPortEndScheduler( void )\r
510 {\r
511         exit( 0 );\r
512 }\r
513 /*-----------------------------------------------------------*/\r
514 \r
515 void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber )\r
516 {\r
517         configASSERT( xPortRunning );\r
518 \r
519         if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )\r
520         {\r
521                 /* Yield interrupts are processed even when critical nesting is\r
522                 non-zero. */\r
523                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
524                 ulPendingInterrupts |= ( 1 << ulInterruptNumber );\r
525 \r
526                 /* The simulated interrupt is now held pending, but don't actually\r
527                 process it yet if this call is within a critical section.  It is\r
528                 possible for this to be in a critical section as calls to wait for\r
529                 mutexes are accumulative. */\r
530                 if( ulCriticalNesting == 0 )\r
531                 {\r
532                         SetEvent( pvInterruptEvent );\r
533                 }\r
534 \r
535                 ReleaseMutex( pvInterruptEventMutex );\r
536         }\r
537 }\r
538 /*-----------------------------------------------------------*/\r
539 \r
540 void vPortSetInterruptHandler( uint32_t ulInterruptNumber, uint32_t (*pvHandler)( void ) )\r
541 {\r
542         if( ulInterruptNumber < portMAX_INTERRUPTS )\r
543         {\r
544                 if( pvInterruptEventMutex != NULL )\r
545                 {\r
546                         WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
547                         ulIsrHandler[ ulInterruptNumber ] = pvHandler;\r
548                         ReleaseMutex( pvInterruptEventMutex );\r
549                 }\r
550                 else\r
551                 {\r
552                         ulIsrHandler[ ulInterruptNumber ] = pvHandler;\r
553                 }\r
554         }\r
555 }\r
556 /*-----------------------------------------------------------*/\r
557 \r
558 void vPortEnterCritical( void )\r
559 {\r
560         if( xPortRunning == pdTRUE )\r
561         {\r
562                 /* The interrupt event mutex is held for the entire critical section,\r
563                 effectively disabling (simulated) interrupts. */\r
564                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
565                 ulCriticalNesting++;\r
566         }\r
567         else\r
568         {\r
569                 ulCriticalNesting++;\r
570         }\r
571 }\r
572 /*-----------------------------------------------------------*/\r
573 \r
574 void vPortExitCritical( void )\r
575 {\r
576 int32_t lMutexNeedsReleasing;\r
577 \r
578         /* The interrupt event mutex should already be held by this thread as it was\r
579         obtained on entry to the critical section. */\r
580 \r
581         lMutexNeedsReleasing = pdTRUE;\r
582 \r
583         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
584         {\r
585                 if( ulCriticalNesting == ( portNO_CRITICAL_NESTING + 1 ) )\r
586                 {\r
587                         ulCriticalNesting--;\r
588 \r
589                         /* Were any interrupts set to pending while interrupts were\r
590                         (simulated) disabled? */\r
591                         if( ulPendingInterrupts != 0UL )\r
592                         {\r
593                                 configASSERT( xPortRunning );\r
594                                 SetEvent( pvInterruptEvent );\r
595 \r
596                                 /* Mutex will be released now, so does not require releasing\r
597                                 on function exit. */\r
598                                 lMutexNeedsReleasing = pdFALSE;\r
599                                 ReleaseMutex( pvInterruptEventMutex );\r
600                         }\r
601                 }\r
602                 else\r
603                 {\r
604                         /* Tick interrupts will still not be processed as the critical\r
605                         nesting depth will not be zero. */\r
606                         ulCriticalNesting--;\r
607                 }\r
608         }\r
609 \r
610         if( pvInterruptEventMutex != NULL )\r
611         {\r
612                 if( lMutexNeedsReleasing == pdTRUE )\r
613                 {\r
614                         configASSERT( xPortRunning );\r
615                         ReleaseMutex( pvInterruptEventMutex );\r
616                 }\r
617         }\r
618 }\r
619 /*-----------------------------------------------------------*/\r
620 \r