2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Standard includes. */
\r
31 /* Scheduler includes. */
\r
32 #include "FreeRTOS.h"
\r
36 #include "mmsystem.h"
\r
38 #pragma comment(lib, "winmm.lib")
\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
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
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
56 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );
\r
59 * Process all the simulated interrupts - each represented by a bit in
\r
60 * ulPendingInterrupts variable.
\r
62 static void prvProcessSimulatedInterrupts( void );
\r
65 * Interrupt handlers used by the kernel itself. These are executed from the
\r
66 * simulated interrupt handler thread.
\r
68 static uint32_t prvProcessYieldInterrupt( void );
\r
69 static uint32_t prvProcessTickInterrupt( void );
\r
72 * Called when the process exits to let Windows know the high timer resolution
\r
73 * is no longer required.
\r
75 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType );
\r
77 /*-----------------------------------------------------------*/
\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
86 /* Handle of the thread that executes the task. */
\r
89 /* Event used to makes sure the thread does not execute past a yield point
\r
90 between the call to SuspendThread() to suspend the thread and the
\r
91 asynchronous SuspendThread() operation actually being performed. */
\r
95 /* Simulated interrupts waiting to be processed. This is a bit mask where each
\r
96 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */
\r
97 static volatile uint32_t ulPendingInterrupts = 0UL;
\r
99 /* An event used to inform the simulated interrupt processing thread (a high
\r
100 priority thread that simulated interrupt processing) that an interrupt is
\r
102 static void *pvInterruptEvent = NULL;
\r
104 /* Mutex used to protect all the simulated interrupt variables that are accessed
\r
105 by multiple threads. */
\r
106 static void *pvInterruptEventMutex = NULL;
\r
108 /* The critical nesting count for the currently executing task. This is
\r
109 initialised to a non-zero value so interrupts do not become enabled during
\r
110 the initialisation phase. As each task has its own critical nesting value
\r
111 ulCriticalNesting will get set to zero when the first task runs. This
\r
112 initialisation is probably not critical in this simulated environment as the
\r
113 simulated interrupt handlers do not get created until the FreeRTOS scheduler is
\r
115 static volatile uint32_t ulCriticalNesting = 9999UL;
\r
117 /* Handlers for all the simulated software interrupts. The first two positions
\r
118 are used for the Yield and Tick interrupts so are handled slightly differently,
\r
119 all the other interrupts can be user defined. */
\r
120 static uint32_t (*ulIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };
\r
122 /* Pointer to the TCB of the currently executing task. */
\r
123 extern void * volatile pxCurrentTCB;
\r
125 /* Used to ensure nothing is processed during the startup sequence. */
\r
126 static BaseType_t xPortRunning = pdFALSE;
\r
128 /*-----------------------------------------------------------*/
\r
130 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
\r
132 TickType_t xMinimumWindowsBlockTime;
\r
133 TIMECAPS xTimeCaps;
\r
135 /* Set the timer resolution to the maximum possible. */
\r
136 if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )
\r
138 xMinimumWindowsBlockTime = ( TickType_t ) xTimeCaps.wPeriodMin;
\r
139 timeBeginPeriod( xTimeCaps.wPeriodMin );
\r
141 /* Register an exit handler so the timeBeginPeriod() function can be
\r
142 matched with a timeEndPeriod() when the application exits. */
\r
143 SetConsoleCtrlHandler( prvEndProcess, TRUE );
\r
147 xMinimumWindowsBlockTime = ( TickType_t ) 20;
\r
150 /* Just to prevent compiler warnings. */
\r
151 ( void ) lpParameter;
\r
155 /* Wait until the timer expires and we can access the simulated interrupt
\r
156 variables. *NOTE* this is not a 'real time' way of generating tick
\r
157 events as the next wake time should be relative to the previous wake
\r
158 time, not the time that Sleep() is called. It is done this way to
\r
159 prevent overruns in this very non real time simulated/emulated
\r
161 if( portTICK_PERIOD_MS < xMinimumWindowsBlockTime )
\r
163 Sleep( xMinimumWindowsBlockTime );
\r
167 Sleep( portTICK_PERIOD_MS );
\r
170 configASSERT( xPortRunning );
\r
172 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
174 /* The timer has expired, generate the simulated tick event. */
\r
175 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );
\r
177 /* The interrupt is now pending - notify the simulated interrupt
\r
179 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
181 SetEvent( pvInterruptEvent );
\r
184 /* Give back the mutex so the simulated interrupt handler unblocks
\r
185 and can access the interrupt handler variables. */
\r
186 ReleaseMutex( pvInterruptEventMutex );
\r
190 /* Should never reach here - MingW complains if you leave this line out,
\r
191 MSVC complains if you put it in. */
\r
195 /*-----------------------------------------------------------*/
\r
197 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType )
\r
199 TIMECAPS xTimeCaps;
\r
201 ( void ) dwCtrlType;
\r
203 if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )
\r
205 /* Match the call to timeBeginPeriod( xTimeCaps.wPeriodMin ) made when
\r
206 the process started with a timeEndPeriod() as the process exits. */
\r
207 timeEndPeriod( xTimeCaps.wPeriodMin );
\r
212 /*-----------------------------------------------------------*/
\r
214 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
216 ThreadState_t *pxThreadState = NULL;
\r
217 int8_t *pcTopOfStack = ( int8_t * ) pxTopOfStack;
\r
218 const SIZE_T xStackSize = 1024; /* Set the size to a small number which will get rounded up to the minimum possible. */
\r
220 /* In this simulated case a stack is not initialised, but instead a thread
\r
221 is created that will execute the task being created. The thread handles
\r
222 the context switching itself. The ThreadState_t object is placed onto
\r
223 the stack that was created for the task - so the stack buffer is still
\r
224 used, just not in the conventional way. It will not be used for anything
\r
225 other than holding this structure. */
\r
226 pxThreadState = ( ThreadState_t * ) ( pcTopOfStack - sizeof( ThreadState_t ) );
\r
228 /* Create the event used to prevent the thread from executing past its yield
\r
229 point if the SuspendThread() call that suspends the thread does not take
\r
230 effect immediately (it is an asynchronous call). */
\r
231 pxThreadState->pvYieldEvent = CreateEvent( NULL, /* Default security attributes. */
\r
232 FALSE, /* Auto reset. */
\r
233 FALSE, /* Start not signalled. */
\r
234 NULL );/* No name. */
\r
236 /* Create the thread itself. */
\r
237 pxThreadState->pvThread = CreateThread( NULL, xStackSize, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION, NULL );
\r
238 configASSERT( pxThreadState->pvThread ); /* See comment where TerminateThread() is called. */
\r
239 SetThreadAffinityMask( pxThreadState->pvThread, 0x01 );
\r
240 SetThreadPriorityBoost( pxThreadState->pvThread, TRUE );
\r
241 SetThreadPriority( pxThreadState->pvThread, portTASK_THREAD_PRIORITY );
\r
243 return ( StackType_t * ) pxThreadState;
\r
245 /*-----------------------------------------------------------*/
\r
247 BaseType_t xPortStartScheduler( void )
\r
249 void *pvHandle = NULL;
\r
251 ThreadState_t *pxThreadState = NULL;
\r
252 SYSTEM_INFO xSystemInfo;
\r
254 /* This port runs windows threads with extremely high priority. All the
\r
255 threads execute on the same core - to prevent locking up the host only start
\r
256 if the host has multiple cores. */
\r
257 GetSystemInfo( &xSystemInfo );
\r
258 if( xSystemInfo.dwNumberOfProcessors <= 1 )
\r
260 printf( "This version of the FreeRTOS Windows port can only be used on multi-core hosts.\r\n" );
\r
267 /* The highest priority class is used to [try to] prevent other Windows
\r
268 activity interfering with FreeRTOS timing too much. */
\r
269 if( SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS ) == 0 )
\r
271 printf( "SetPriorityClass() failed\r\n" );
\r
274 /* Install the interrupt handlers used by the scheduler itself. */
\r
275 vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );
\r
276 vPortSetInterruptHandler( portINTERRUPT_TICK, prvProcessTickInterrupt );
\r
278 /* Create the events and mutexes that are used to synchronise all the
\r
280 pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );
\r
281 pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
\r
283 if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) )
\r
288 /* Set the priority of this thread such that it is above the priority of
\r
289 the threads that run tasks. This higher priority is required to ensure
\r
290 simulated interrupts take priority over tasks. */
\r
291 pvHandle = GetCurrentThread();
\r
292 if( pvHandle == NULL )
\r
298 if( lSuccess == pdPASS )
\r
300 if( SetThreadPriority( pvHandle, portSIMULATED_INTERRUPTS_THREAD_PRIORITY ) == 0 )
\r
304 SetThreadPriorityBoost( pvHandle, TRUE );
\r
305 SetThreadAffinityMask( pvHandle, 0x01 );
\r
308 if( lSuccess == pdPASS )
\r
310 /* Start the thread that simulates the timer peripheral to generate
\r
311 tick interrupts. The priority is set below that of the simulated
\r
312 interrupt handler so the interrupt event mutex is used for the
\r
313 handshake / overrun protection. */
\r
314 pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, CREATE_SUSPENDED, NULL );
\r
315 if( pvHandle != NULL )
\r
317 SetThreadPriority( pvHandle, portSIMULATED_TIMER_THREAD_PRIORITY );
\r
318 SetThreadPriorityBoost( pvHandle, TRUE );
\r
319 SetThreadAffinityMask( pvHandle, 0x01 );
\r
320 ResumeThread( pvHandle );
\r
323 /* Start the highest priority task by obtaining its associated thread
\r
324 state structure, in which is stored the thread handle. */
\r
325 pxThreadState = ( ThreadState_t * ) *( ( size_t * ) pxCurrentTCB );
\r
326 ulCriticalNesting = portNO_CRITICAL_NESTING;
\r
328 /* Start the first task. */
\r
329 ResumeThread( pxThreadState->pvThread );
\r
331 /* Handle all simulated interrupts - including yield requests and
\r
332 simulated ticks. */
\r
333 prvProcessSimulatedInterrupts();
\r
336 /* Would not expect to return from prvProcessSimulatedInterrupts(), so should
\r
340 /*-----------------------------------------------------------*/
\r
342 static uint32_t prvProcessYieldInterrupt( void )
\r
346 /*-----------------------------------------------------------*/
\r
348 static uint32_t prvProcessTickInterrupt( void )
\r
350 uint32_t ulSwitchRequired;
\r
352 /* Process the tick itself. */
\r
353 configASSERT( xPortRunning );
\r
354 ulSwitchRequired = ( uint32_t ) xTaskIncrementTick();
\r
356 return ulSwitchRequired;
\r
358 /*-----------------------------------------------------------*/
\r
360 static void prvProcessSimulatedInterrupts( void )
\r
362 uint32_t ulSwitchRequired, i;
\r
363 ThreadState_t *pxThreadState;
\r
364 void *pvObjectList[ 2 ];
\r
367 /* Going to block on the mutex that ensured exclusive access to the simulated
\r
368 interrupt objects, and the event that signals that a simulated interrupt
\r
369 should be processed. */
\r
370 pvObjectList[ 0 ] = pvInterruptEventMutex;
\r
371 pvObjectList[ 1 ] = pvInterruptEvent;
\r
373 /* Create a pending tick to ensure the first task is started as soon as
\r
374 this thread pends. */
\r
375 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );
\r
376 SetEvent( pvInterruptEvent );
\r
378 xPortRunning = pdTRUE;
\r
382 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );
\r
384 /* Used to indicate whether the simulated interrupt processing has
\r
385 necessitated a context switch to another task/thread. */
\r
386 ulSwitchRequired = pdFALSE;
\r
388 /* For each interrupt we are interested in processing, each of which is
\r
389 represented by a bit in the 32bit ulPendingInterrupts variable. */
\r
390 for( i = 0; i < portMAX_INTERRUPTS; i++ )
\r
392 /* Is the simulated interrupt pending? */
\r
393 if( ulPendingInterrupts & ( 1UL << i ) )
\r
395 /* Is a handler installed? */
\r
396 if( ulIsrHandler[ i ] != NULL )
\r
398 /* Run the actual handler. */
\r
399 if( ulIsrHandler[ i ]() != pdFALSE )
\r
401 ulSwitchRequired |= ( 1 << i );
\r
405 /* Clear the interrupt pending bit. */
\r
406 ulPendingInterrupts &= ~( 1UL << i );
\r
410 if( ulSwitchRequired != pdFALSE )
\r
412 void *pvOldCurrentTCB;
\r
414 pvOldCurrentTCB = pxCurrentTCB;
\r
416 /* Select the next task to run. */
\r
417 vTaskSwitchContext();
\r
419 /* If the task selected to enter the running state is not the task
\r
420 that is already in the running state. */
\r
421 if( pvOldCurrentTCB != pxCurrentTCB )
\r
423 /* Suspend the old thread. */
\r
424 pxThreadState = ( ThreadState_t *) *( ( size_t * ) pvOldCurrentTCB );
\r
425 SuspendThread( pxThreadState->pvThread );
\r
427 /* Ensure the thread is actually suspended by performing a
\r
428 synchronous operation that can only complete when the thread is
\r
429 actually suspended. The below code asks for dummy register
\r
430 data. Experimentation shows that these two lines don't appear
\r
431 to do anything now, but according to
\r
432 https://devblogs.microsoft.com/oldnewthing/20150205-00/?p=44743
\r
433 they do - so as they do not harm (slight run-time hit) they have
\r
435 xContext.ContextFlags = CONTEXT_INTEGER;
\r
436 ( void ) GetThreadContext( pxThreadState->pvThread, &xContext );
\r
438 /* Obtain the state of the task now selected to enter the
\r
440 pxThreadState = ( ThreadState_t * ) ( *( size_t *) pxCurrentTCB );
\r
442 /* pxThreadState->pvThread can be NULL if the task deleted
\r
443 itself - but a deleted task should never be resumed here. */
\r
444 configASSERT( pxThreadState->pvThread != NULL );
\r
445 ResumeThread( pxThreadState->pvThread );
\r
449 /* If the thread that is about to be resumed stopped running
\r
450 because it yielded then it will wait on an event when it resumed
\r
451 (to ensure it does not continue running after the call to
\r
452 SuspendThread() above as SuspendThread() is asynchronous).
\r
453 Signal the event to ensure the thread can proceed now it is
\r
454 valid for it to do so. */
\r
455 pxThreadState = ( ThreadState_t * ) ( *( size_t *) pxCurrentTCB );
\r
456 SetEvent( pxThreadState->pvYieldEvent );
\r
457 ReleaseMutex( pvInterruptEventMutex );
\r
460 /*-----------------------------------------------------------*/
\r
462 void vPortDeleteThread( void *pvTaskToDelete )
\r
464 ThreadState_t *pxThreadState;
\r
465 uint32_t ulErrorCode;
\r
467 /* Remove compiler warnings if configASSERT() is not defined. */
\r
468 ( void ) ulErrorCode;
\r
470 /* Find the handle of the thread being deleted. */
\r
471 pxThreadState = ( ThreadState_t * ) ( *( size_t *) pvTaskToDelete );
\r
473 /* Check that the thread is still valid, it might have been closed by
\r
474 vPortCloseRunningThread() - which will be the case if the task associated
\r
475 with the thread originally deleted itself rather than being deleted by a
\r
477 if( pxThreadState->pvThread != NULL )
\r
479 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
481 /* !!! This is not a nice way to terminate a thread, and will eventually
\r
482 result in resources being depleted if tasks frequently delete other
\r
483 tasks (rather than deleting themselves) as the task stacks will not be
\r
485 ulErrorCode = TerminateThread( pxThreadState->pvThread, 0 );
\r
486 configASSERT( ulErrorCode );
\r
488 ulErrorCode = CloseHandle( pxThreadState->pvThread );
\r
489 configASSERT( ulErrorCode );
\r
491 ReleaseMutex( pvInterruptEventMutex );
\r
494 /*-----------------------------------------------------------*/
\r
496 void vPortCloseRunningThread( void *pvTaskToDelete, volatile BaseType_t *pxPendYield )
\r
498 ThreadState_t *pxThreadState;
\r
500 uint32_t ulErrorCode;
\r
502 /* Remove compiler warnings if configASSERT() is not defined. */
\r
503 ( void ) ulErrorCode;
\r
505 /* Find the handle of the thread being deleted. */
\r
506 pxThreadState = ( ThreadState_t * ) ( *( size_t *) pvTaskToDelete );
\r
507 pvThread = pxThreadState->pvThread;
\r
509 /* Raise the Windows priority of the thread to ensure the FreeRTOS scheduler
\r
510 does not run and swap it out before it is closed. If that were to happen
\r
511 the thread would never run again and effectively be a thread handle and
\r
513 SetThreadPriority( pvThread, portDELETE_SELF_THREAD_PRIORITY );
\r
515 /* This function will not return, therefore a yield is set as pending to
\r
516 ensure a context switch occurs away from this thread on the next tick. */
\r
517 *pxPendYield = pdTRUE;
\r
519 /* Mark the thread associated with this task as invalid so
\r
520 vPortDeleteThread() does not try to terminate it. */
\r
521 pxThreadState->pvThread = NULL;
\r
523 /* Close the thread. */
\r
524 ulErrorCode = CloseHandle( pvThread );
\r
525 configASSERT( ulErrorCode );
\r
527 /* This is called from a critical section, which must be exited before the
\r
529 taskEXIT_CRITICAL();
\r
533 /*-----------------------------------------------------------*/
\r
535 void vPortEndScheduler( void )
\r
539 /*-----------------------------------------------------------*/
\r
541 void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber )
\r
543 ThreadState_t *pxThreadState = ( ThreadState_t *) *( ( size_t * ) pxCurrentTCB );
\r
545 configASSERT( xPortRunning );
\r
547 if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )
\r
549 /* Yield interrupts are processed even when critical nesting is
\r
551 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
552 ulPendingInterrupts |= ( 1 << ulInterruptNumber );
\r
554 /* The simulated interrupt is now held pending, but don't actually
\r
555 process it yet if this call is within a critical section. It is
\r
556 possible for this to be in a critical section as calls to wait for
\r
557 mutexes are accumulative. */
\r
558 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
560 SetEvent( pvInterruptEvent );
\r
562 if( ulInterruptNumber == portINTERRUPT_YIELD )
\r
564 /* Going to wait for an event - make sure the event is not already
\r
566 ResetEvent( pxThreadState->pvYieldEvent );
\r
570 ReleaseMutex( pvInterruptEventMutex );
\r
572 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
574 if( ulInterruptNumber == portINTERRUPT_YIELD )
\r
576 /* The task was yielding so will be suspended however the
\r
577 SuspendThread() function is asynchronous so this event is used to
\r
578 prevent the task executing further if SuspendThread() does not take
\r
579 effect immediately. */
\r
580 WaitForSingleObject( pxThreadState->pvYieldEvent, INFINITE );
\r
585 /*-----------------------------------------------------------*/
\r
587 void vPortSetInterruptHandler( uint32_t ulInterruptNumber, uint32_t (*pvHandler)( void ) )
\r
589 if( ulInterruptNumber < portMAX_INTERRUPTS )
\r
591 if( pvInterruptEventMutex != NULL )
\r
593 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
594 ulIsrHandler[ ulInterruptNumber ] = pvHandler;
\r
595 ReleaseMutex( pvInterruptEventMutex );
\r
599 ulIsrHandler[ ulInterruptNumber ] = pvHandler;
\r
603 /*-----------------------------------------------------------*/
\r
605 void vPortEnterCritical( void )
\r
607 if( xPortRunning == pdTRUE )
\r
609 /* The interrupt event mutex is held for the entire critical section,
\r
610 effectively disabling (simulated) interrupts. */
\r
611 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
614 ulCriticalNesting++;
\r
616 /*-----------------------------------------------------------*/
\r
618 void vPortExitCritical( void )
\r
620 int32_t lMutexNeedsReleasing, lWaitForYield = pdFALSE;
\r
622 /* The interrupt event mutex should already be held by this thread as it was
\r
623 obtained on entry to the critical section. */
\r
625 lMutexNeedsReleasing = pdTRUE;
\r
627 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
629 if( ulCriticalNesting == ( portNO_CRITICAL_NESTING + 1 ) )
\r
631 ulCriticalNesting--;
\r
633 /* Were any interrupts set to pending while interrupts were
\r
634 (simulated) disabled? */
\r
635 if( ulPendingInterrupts != 0UL )
\r
637 ThreadState_t *pxThreadState = ( ThreadState_t *) *( ( size_t * ) pxCurrentTCB );
\r
639 configASSERT( xPortRunning );
\r
640 SetEvent( pvInterruptEvent );
\r
642 if( ( ulPendingInterrupts & ( 1 << portINTERRUPT_YIELD ) ) != 0 )
\r
644 /* Going to wait for an event - make sure the event is not already
\r
646 ResetEvent( pxThreadState->pvYieldEvent );
\r
647 lWaitForYield = pdTRUE;
\r
650 /* Mutex will be released now, so does not require releasing
\r
651 on function exit. */
\r
652 lMutexNeedsReleasing = pdFALSE;
\r
653 ReleaseMutex( pvInterruptEventMutex );
\r
655 if( lWaitForYield != pdFALSE )
\r
657 /* The task was yielding so will be suspended however the
\r
658 SuspendThread() function is asynchronous so this event is
\r
659 used to prevent the task executing further if
\r
660 SuspendThread() does not take effect immediately. */
\r
661 WaitForSingleObject( pxThreadState->pvYieldEvent, INFINITE );
\r
667 /* Tick interrupts will still not be processed as the critical
\r
668 nesting depth will not be zero. */
\r
669 ulCriticalNesting--;
\r
673 if( pvInterruptEventMutex != NULL )
\r
675 if( lMutexNeedsReleasing == pdTRUE )
\r
677 configASSERT( xPortRunning );
\r
678 ReleaseMutex( pvInterruptEventMutex );
\r
682 /*-----------------------------------------------------------*/
\r