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