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