2 * FreeRTOS Kernel V10.4.4
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
29 /* Standard includes. */
\r
32 /* Scheduler includes. */
\r
33 #include "FreeRTOS.h"
\r
37 #include "mmsystem.h"
\r
39 #pragma comment(lib, "winmm.lib")
\r
42 #define portMAX_INTERRUPTS ( ( uint32_t ) sizeof( uint32_t ) * 8UL ) /* The number of bits in an uint32_t. */
\r
43 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
\r
45 /* The priorities at which the various components of the simulation execute. */
\r
46 #define portDELETE_SELF_THREAD_PRIORITY THREAD_PRIORITY_TIME_CRITICAL /* Must be highest. */
\r
47 #define portSIMULATED_INTERRUPTS_THREAD_PRIORITY THREAD_PRIORITY_TIME_CRITICAL
\r
48 #define portSIMULATED_TIMER_THREAD_PRIORITY THREAD_PRIORITY_HIGHEST
\r
49 #define portTASK_THREAD_PRIORITY THREAD_PRIORITY_ABOVE_NORMAL
\r
52 * Created as a high priority thread, this function uses a timer to simulate
\r
53 * a tick interrupt being generated on an embedded target. In this Windows
\r
54 * environment the timer does not achieve anything approaching real time
\r
55 * performance though.
\r
57 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );
\r
60 * Process all the simulated interrupts - each represented by a bit in
\r
61 * ulPendingInterrupts variable.
\r
63 static void prvProcessSimulatedInterrupts( void );
\r
66 * Interrupt handlers used by the kernel itself. These are executed from the
\r
67 * simulated interrupt handler thread.
\r
69 static uint32_t prvProcessYieldInterrupt( void );
\r
70 static uint32_t prvProcessTickInterrupt( void );
\r
73 * Exiting a critical section will cause the calling task to block on yield
\r
74 * event to wait for an interrupt to process if an interrupt was pended while
\r
75 * inside the critical section. This variable protects against a recursive
\r
76 * attempt to obtain pvInterruptEventMutex if a critical section is used inside
\r
77 * an interrupt handler itself.
\r
79 volatile BaseType_t xInsideInterrupt = pdFALSE;
\r
82 * Called when the process exits to let Windows know the high timer resolution
\r
83 * is no longer required.
\r
85 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType );
\r
87 /*-----------------------------------------------------------*/
\r
89 /* The WIN32 simulator runs each task in a thread. The context switching is
\r
90 managed by the threads, so the task stack does not have to be managed directly,
\r
91 although the task stack is still used to hold an xThreadState structure this is
\r
92 the only thing it will ever hold. The structure indirectly maps the task handle
\r
93 to a thread handle. */
\r
96 /* Handle of the thread that executes the task. */
\r
99 /* Event used to make sure the thread does not execute past a yield point
\r
100 between the call to SuspendThread() to suspend the thread and the
\r
101 asynchronous SuspendThread() operation actually being performed. */
\r
102 void *pvYieldEvent;
\r
105 /* Simulated interrupts waiting to be processed. This is a bit mask where each
\r
106 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */
\r
107 static volatile uint32_t ulPendingInterrupts = 0UL;
\r
109 /* An event used to inform the simulated interrupt processing thread (a high
\r
110 priority thread that simulated interrupt processing) that an interrupt is
\r
112 static void *pvInterruptEvent = NULL;
\r
114 /* Mutex used to protect all the simulated interrupt variables that are accessed
\r
115 by multiple threads. */
\r
116 static void *pvInterruptEventMutex = NULL;
\r
118 /* The critical nesting count for the currently executing task. This is
\r
119 initialised to a non-zero value so interrupts do not become enabled during
\r
120 the initialisation phase. As each task has its own critical nesting value
\r
121 ulCriticalNesting will get set to zero when the first task runs. This
\r
122 initialisation is probably not critical in this simulated environment as the
\r
123 simulated interrupt handlers do not get created until the FreeRTOS scheduler is
\r
125 static volatile uint32_t ulCriticalNesting = 9999UL;
\r
127 /* Handlers for all the simulated software interrupts. The first two positions
\r
128 are used for the Yield and Tick interrupts so are handled slightly differently,
\r
129 all the other interrupts can be user defined. */
\r
130 static uint32_t (*ulIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };
\r
132 /* Pointer to the TCB of the currently executing task. */
\r
133 extern void * volatile pxCurrentTCB;
\r
135 /* Used to ensure nothing is processed during the startup sequence. */
\r
136 static BaseType_t xPortRunning = pdFALSE;
\r
138 /*-----------------------------------------------------------*/
\r
140 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
\r
142 TickType_t xMinimumWindowsBlockTime;
\r
143 TIMECAPS xTimeCaps;
\r
145 /* Set the timer resolution to the maximum possible. */
\r
146 if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )
\r
148 xMinimumWindowsBlockTime = ( TickType_t ) xTimeCaps.wPeriodMin;
\r
149 timeBeginPeriod( xTimeCaps.wPeriodMin );
\r
151 /* Register an exit handler so the timeBeginPeriod() function can be
\r
152 matched with a timeEndPeriod() when the application exits. */
\r
153 SetConsoleCtrlHandler( prvEndProcess, TRUE );
\r
157 xMinimumWindowsBlockTime = ( TickType_t ) 20;
\r
160 /* Just to prevent compiler warnings. */
\r
161 ( void ) lpParameter;
\r
165 /* Wait until the timer expires and we can access the simulated interrupt
\r
166 variables. *NOTE* this is not a 'real time' way of generating tick
\r
167 events as the next wake time should be relative to the previous wake
\r
168 time, not the time that Sleep() is called. It is done this way to
\r
169 prevent overruns in this very non real time simulated/emulated
\r
171 if( portTICK_PERIOD_MS < xMinimumWindowsBlockTime )
\r
173 Sleep( xMinimumWindowsBlockTime );
\r
177 Sleep( portTICK_PERIOD_MS );
\r
180 configASSERT( xPortRunning );
\r
182 /* Can't proceed if in a critical section as pvInterruptEventMutex won't
\r
184 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
186 /* The timer has expired, generate the simulated tick event. */
\r
187 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );
\r
189 /* The interrupt is now pending - notify the simulated interrupt
\r
190 handler thread. Must be outside of a critical section to get here so
\r
191 the handler thread can execute immediately pvInterruptEventMutex is
\r
193 configASSERT( ulCriticalNesting == 0UL );
\r
194 SetEvent( pvInterruptEvent );
\r
196 /* Give back the mutex so the simulated interrupt handler unblocks
\r
197 and can access the interrupt handler variables. */
\r
198 ReleaseMutex( pvInterruptEventMutex );
\r
202 /* Should never reach here - MingW complains if you leave this line out,
\r
203 MSVC complains if you put it in. */
\r
207 /*-----------------------------------------------------------*/
\r
209 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType )
\r
211 TIMECAPS xTimeCaps;
\r
213 ( void ) dwCtrlType;
\r
215 if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )
\r
217 /* Match the call to timeBeginPeriod( xTimeCaps.wPeriodMin ) made when
\r
218 the process started with a timeEndPeriod() as the process exits. */
\r
219 timeEndPeriod( xTimeCaps.wPeriodMin );
\r
224 /*-----------------------------------------------------------*/
\r
226 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
228 ThreadState_t *pxThreadState = NULL;
\r
229 int8_t *pcTopOfStack = ( int8_t * ) pxTopOfStack;
\r
230 const SIZE_T xStackSize = 1024; /* Set the size to a small number which will get rounded up to the minimum possible. */
\r
232 /* In this simulated case a stack is not initialised, but instead a thread
\r
233 is created that will execute the task being created. The thread handles
\r
234 the context switching itself. The ThreadState_t object is placed onto
\r
235 the stack that was created for the task - so the stack buffer is still
\r
236 used, just not in the conventional way. It will not be used for anything
\r
237 other than holding this structure. */
\r
238 pxThreadState = ( ThreadState_t * ) ( pcTopOfStack - sizeof( ThreadState_t ) );
\r
240 /* Create the event used to prevent the thread from executing past its yield
\r
241 point if the SuspendThread() call that suspends the thread does not take
\r
242 effect immediately (it is an asynchronous call). */
\r
243 pxThreadState->pvYieldEvent = CreateEvent( NULL, /* Default security attributes. */
\r
244 FALSE, /* Auto reset. */
\r
245 FALSE, /* Start not signalled. */
\r
246 NULL );/* No name. */
\r
248 /* Create the thread itself. */
\r
249 pxThreadState->pvThread = CreateThread( NULL, xStackSize, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION, NULL );
\r
250 configASSERT( pxThreadState->pvThread ); /* See comment where TerminateThread() is called. */
\r
251 SetThreadAffinityMask( pxThreadState->pvThread, 0x01 );
\r
252 SetThreadPriorityBoost( pxThreadState->pvThread, TRUE );
\r
253 SetThreadPriority( pxThreadState->pvThread, portTASK_THREAD_PRIORITY );
\r
255 return ( StackType_t * ) pxThreadState;
\r
257 /*-----------------------------------------------------------*/
\r
259 BaseType_t xPortStartScheduler( void )
\r
261 void *pvHandle = NULL;
\r
263 ThreadState_t *pxThreadState = NULL;
\r
264 SYSTEM_INFO xSystemInfo;
\r
266 /* This port runs windows threads with extremely high priority. All the
\r
267 threads execute on the same core - to prevent locking up the host only start
\r
268 if the host has multiple cores. */
\r
269 GetSystemInfo( &xSystemInfo );
\r
270 if( xSystemInfo.dwNumberOfProcessors <= 1 )
\r
272 printf( "This version of the FreeRTOS Windows port can only be used on multi-core hosts.\r\n" );
\r
279 /* The highest priority class is used to [try to] prevent other Windows
\r
280 activity interfering with FreeRTOS timing too much. */
\r
281 if( SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS ) == 0 )
\r
283 printf( "SetPriorityClass() failed\r\n" );
\r
286 /* Install the interrupt handlers used by the scheduler itself. */
\r
287 vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );
\r
288 vPortSetInterruptHandler( portINTERRUPT_TICK, prvProcessTickInterrupt );
\r
290 /* Create the events and mutexes that are used to synchronise all the
\r
292 pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );
\r
293 pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
\r
295 if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) )
\r
300 /* Set the priority of this thread such that it is above the priority of
\r
301 the threads that run tasks. This higher priority is required to ensure
\r
302 simulated interrupts take priority over tasks. */
\r
303 pvHandle = GetCurrentThread();
\r
304 if( pvHandle == NULL )
\r
310 if( lSuccess == pdPASS )
\r
312 if( SetThreadPriority( pvHandle, portSIMULATED_INTERRUPTS_THREAD_PRIORITY ) == 0 )
\r
316 SetThreadPriorityBoost( pvHandle, TRUE );
\r
317 SetThreadAffinityMask( pvHandle, 0x01 );
\r
320 if( lSuccess == pdPASS )
\r
322 /* Start the thread that simulates the timer peripheral to generate
\r
323 tick interrupts. The priority is set below that of the simulated
\r
324 interrupt handler so the interrupt event mutex is used for the
\r
325 handshake / overrun protection. */
\r
326 pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, CREATE_SUSPENDED, NULL );
\r
327 if( pvHandle != NULL )
\r
329 SetThreadPriority( pvHandle, portSIMULATED_TIMER_THREAD_PRIORITY );
\r
330 SetThreadPriorityBoost( pvHandle, TRUE );
\r
331 SetThreadAffinityMask( pvHandle, 0x01 );
\r
332 ResumeThread( pvHandle );
\r
335 /* Start the highest priority task by obtaining its associated thread
\r
336 state structure, in which is stored the thread handle. */
\r
337 pxThreadState = ( ThreadState_t * ) *( ( size_t * ) pxCurrentTCB );
\r
338 ulCriticalNesting = portNO_CRITICAL_NESTING;
\r
340 /* Start the first task. */
\r
341 ResumeThread( pxThreadState->pvThread );
\r
343 /* Handle all simulated interrupts - including yield requests and
\r
344 simulated ticks. */
\r
345 prvProcessSimulatedInterrupts();
\r
348 /* Would not expect to return from prvProcessSimulatedInterrupts(), so should
\r
352 /*-----------------------------------------------------------*/
\r
354 static uint32_t prvProcessYieldInterrupt( void )
\r
356 /* Always return true as this is a yield. */
\r
359 /*-----------------------------------------------------------*/
\r
361 static uint32_t prvProcessTickInterrupt( void )
\r
363 uint32_t ulSwitchRequired;
\r
365 /* Process the tick itself. */
\r
366 configASSERT( xPortRunning );
\r
367 ulSwitchRequired = ( uint32_t ) xTaskIncrementTick();
\r
369 return ulSwitchRequired;
\r
371 /*-----------------------------------------------------------*/
\r
373 static void prvProcessSimulatedInterrupts( void )
\r
375 uint32_t ulSwitchRequired, i;
\r
376 ThreadState_t *pxThreadState;
\r
377 void *pvObjectList[ 2 ];
\r
380 /* Going to block on the mutex that ensured exclusive access to the simulated
\r
381 interrupt objects, and the event that signals that a simulated interrupt
\r
382 should be processed. */
\r
383 pvObjectList[ 0 ] = pvInterruptEventMutex;
\r
384 pvObjectList[ 1 ] = pvInterruptEvent;
\r
386 /* Create a pending tick to ensure the first task is started as soon as
\r
387 this thread pends. */
\r
388 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );
\r
389 SetEvent( pvInterruptEvent );
\r
391 xPortRunning = pdTRUE;
\r
395 xInsideInterrupt = pdFALSE;
\r
396 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );
\r
398 /* Cannot be in a critical section to get here. Tasks that exit a
\r
399 critical section will block on a yield mutex to wait for an interrupt to
\r
400 process if an interrupt was set pending while the task was inside the
\r
401 critical section. xInsideInterrupt prevents interrupts that contain
\r
402 critical sections from doing the same. */
\r
403 xInsideInterrupt = pdTRUE;
\r
405 /* Used to indicate whether the simulated interrupt processing has
\r
406 necessitated a context switch to another task/thread. */
\r
407 ulSwitchRequired = pdFALSE;
\r
409 /* For each interrupt we are interested in processing, each of which is
\r
410 represented by a bit in the 32bit ulPendingInterrupts variable. */
\r
411 for( i = 0; i < portMAX_INTERRUPTS; i++ )
\r
413 /* Is the simulated interrupt pending? */
\r
414 if( ( ulPendingInterrupts & ( 1UL << i ) ) != 0 )
\r
416 /* Is a handler installed? */
\r
417 if( ulIsrHandler[ i ] != NULL )
\r
419 /* Run the actual handler. Handlers return pdTRUE if they
\r
420 necessitate a context switch. */
\r
421 if( ulIsrHandler[ i ]() != pdFALSE )
\r
423 /* A bit mask is used purely to help debugging. */
\r
424 ulSwitchRequired |= ( 1 << i );
\r
428 /* Clear the interrupt pending bit. */
\r
429 ulPendingInterrupts &= ~( 1UL << i );
\r
433 if( ulSwitchRequired != pdFALSE )
\r
435 void *pvOldCurrentTCB;
\r
437 pvOldCurrentTCB = pxCurrentTCB;
\r
439 /* Select the next task to run. */
\r
440 vTaskSwitchContext();
\r
442 /* If the task selected to enter the running state is not the task
\r
443 that is already in the running state. */
\r
444 if( pvOldCurrentTCB != pxCurrentTCB )
\r
446 /* Suspend the old thread. In the cases where the (simulated)
\r
447 interrupt is asynchronous (tick event swapping a task out rather
\r
448 than a task blocking or yielding) it doesn't matter if the
\r
449 'suspend' operation doesn't take effect immediately - if it
\r
450 doesn't it would just be like the interrupt occurring slightly
\r
451 later. In cases where the yield was caused by a task blocking
\r
452 or yielding then the task will block on a yield event after the
\r
453 yield operation in case the 'suspend' operation doesn't take
\r
454 effect immediately. */
\r
455 pxThreadState = ( ThreadState_t *) *( ( size_t * ) pvOldCurrentTCB );
\r
456 SuspendThread( pxThreadState->pvThread );
\r
458 /* Ensure the thread is actually suspended by performing a
\r
459 synchronous operation that can only complete when the thread is
\r
460 actually suspended. The below code asks for dummy register
\r
461 data. Experimentation shows that these two lines don't appear
\r
462 to do anything now, but according to
\r
463 https://devblogs.microsoft.com/oldnewthing/20150205-00/?p=44743
\r
464 they do - so as they do not harm (slight run-time hit). */
\r
465 xContext.ContextFlags = CONTEXT_INTEGER;
\r
466 ( void ) GetThreadContext( pxThreadState->pvThread, &xContext );
\r
468 /* Obtain the state of the task now selected to enter the
\r
470 pxThreadState = ( ThreadState_t * ) ( *( size_t *) pxCurrentTCB );
\r
472 /* pxThreadState->pvThread can be NULL if the task deleted
\r
473 itself - but a deleted task should never be resumed here. */
\r
474 configASSERT( pxThreadState->pvThread != NULL );
\r
475 ResumeThread( pxThreadState->pvThread );
\r
479 /* If the thread that is about to be resumed stopped running
\r
480 because it yielded then it will wait on an event when it resumed
\r
481 (to ensure it does not continue running after the call to
\r
482 SuspendThread() above as SuspendThread() is asynchronous).
\r
483 Signal the event to ensure the thread can proceed now it is
\r
484 valid for it to do so. Signaling the event is benign in the case that
\r
485 the task was switched out asynchronously by an interrupt as the event
\r
486 is reset before the task blocks on it. */
\r
487 pxThreadState = ( ThreadState_t * ) ( *( size_t *) pxCurrentTCB );
\r
488 SetEvent( pxThreadState->pvYieldEvent );
\r
489 ReleaseMutex( pvInterruptEventMutex );
\r
492 /*-----------------------------------------------------------*/
\r
494 void vPortDeleteThread( void *pvTaskToDelete )
\r
496 ThreadState_t *pxThreadState;
\r
497 uint32_t ulErrorCode;
\r
499 /* Remove compiler warnings if configASSERT() is not defined. */
\r
500 ( void ) ulErrorCode;
\r
502 /* Find the handle of the thread being deleted. */
\r
503 pxThreadState = ( ThreadState_t * ) ( *( size_t *) pvTaskToDelete );
\r
505 /* Check that the thread is still valid, it might have been closed by
\r
506 vPortCloseRunningThread() - which will be the case if the task associated
\r
507 with the thread originally deleted itself rather than being deleted by a
\r
509 if( pxThreadState->pvThread != NULL )
\r
511 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
513 /* !!! This is not a nice way to terminate a thread, and will eventually
\r
514 result in resources being depleted if tasks frequently delete other
\r
515 tasks (rather than deleting themselves) as the task stacks will not be
\r
517 ulErrorCode = TerminateThread( pxThreadState->pvThread, 0 );
\r
518 configASSERT( ulErrorCode );
\r
520 ulErrorCode = CloseHandle( pxThreadState->pvThread );
\r
521 configASSERT( ulErrorCode );
\r
523 ReleaseMutex( pvInterruptEventMutex );
\r
526 /*-----------------------------------------------------------*/
\r
528 void vPortCloseRunningThread( void *pvTaskToDelete, volatile BaseType_t *pxPendYield )
\r
530 ThreadState_t *pxThreadState;
\r
532 uint32_t ulErrorCode;
\r
534 /* Remove compiler warnings if configASSERT() is not defined. */
\r
535 ( void ) ulErrorCode;
\r
537 /* Find the handle of the thread being deleted. */
\r
538 pxThreadState = ( ThreadState_t * ) ( *( size_t *) pvTaskToDelete );
\r
539 pvThread = pxThreadState->pvThread;
\r
541 /* Raise the Windows priority of the thread to ensure the FreeRTOS scheduler
\r
542 does not run and swap it out before it is closed. If that were to happen
\r
543 the thread would never run again and effectively be a thread handle and
\r
545 SetThreadPriority( pvThread, portDELETE_SELF_THREAD_PRIORITY );
\r
547 /* This function will not return, therefore a yield is set as pending to
\r
548 ensure a context switch occurs away from this thread on the next tick. */
\r
549 *pxPendYield = pdTRUE;
\r
551 /* Mark the thread associated with this task as invalid so
\r
552 vPortDeleteThread() does not try to terminate it. */
\r
553 pxThreadState->pvThread = NULL;
\r
555 /* Close the thread. */
\r
556 ulErrorCode = CloseHandle( pvThread );
\r
557 configASSERT( ulErrorCode );
\r
559 /* This is called from a critical section, which must be exited before the
\r
561 taskEXIT_CRITICAL();
\r
562 CloseHandle( pxThreadState->pvYieldEvent );
\r
565 /*-----------------------------------------------------------*/
\r
567 void vPortEndScheduler( void )
\r
571 /*-----------------------------------------------------------*/
\r
573 void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber )
\r
575 ThreadState_t *pxThreadState = ( ThreadState_t *) *( ( size_t * ) pxCurrentTCB );
\r
577 configASSERT( xPortRunning );
\r
579 if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )
\r
581 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
582 ulPendingInterrupts |= ( 1 << ulInterruptNumber );
\r
584 /* The simulated interrupt is now held pending, but don't actually
\r
585 process it yet if this call is within a critical section. It is
\r
586 possible for this to be in a critical section as calls to wait for
\r
587 mutexes are accumulative. If in a critical section then the event
\r
588 will get set when the critical section nesting count is wound back
\r
590 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
592 SetEvent( pvInterruptEvent );
\r
594 /* Going to wait for an event - make sure the event is not already
\r
596 ResetEvent( pxThreadState->pvYieldEvent );
\r
599 ReleaseMutex( pvInterruptEventMutex );
\r
600 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
602 /* An interrupt was pended so ensure to block to allow it to
\r
603 execute. In most cases the (simulated) interrupt will have
\r
604 executed before the next line is reached - so this is just to make
\r
606 WaitForSingleObject( pxThreadState->pvYieldEvent, INFINITE );
\r
610 /*-----------------------------------------------------------*/
\r
612 void vPortSetInterruptHandler( uint32_t ulInterruptNumber, uint32_t (*pvHandler)( void ) )
\r
614 if( ulInterruptNumber < portMAX_INTERRUPTS )
\r
616 if( pvInterruptEventMutex != NULL )
\r
618 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
619 ulIsrHandler[ ulInterruptNumber ] = pvHandler;
\r
620 ReleaseMutex( pvInterruptEventMutex );
\r
624 ulIsrHandler[ ulInterruptNumber ] = pvHandler;
\r
628 /*-----------------------------------------------------------*/
\r
630 void vPortEnterCritical( void )
\r
632 if( xPortRunning == pdTRUE )
\r
634 /* The interrupt event mutex is held for the entire critical section,
\r
635 effectively disabling (simulated) interrupts. */
\r
636 WaitForSingleObject( pvInterruptEventMutex, INFINITE );
\r
639 ulCriticalNesting++;
\r
641 /*-----------------------------------------------------------*/
\r
643 void vPortExitCritical( void )
\r
645 int32_t lMutexNeedsReleasing;
\r
647 /* The interrupt event mutex should already be held by this thread as it was
\r
648 obtained on entry to the critical section. */
\r
649 lMutexNeedsReleasing = pdTRUE;
\r
651 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
653 ulCriticalNesting--;
\r
655 /* Don't need to wait for any pending interrupts to execute if the
\r
656 critical section was exited from inside an interrupt. */
\r
657 if( ( ulCriticalNesting == portNO_CRITICAL_NESTING ) && ( xInsideInterrupt == pdFALSE ) )
\r
659 /* Were any interrupts set to pending while interrupts were
\r
660 (simulated) disabled? */
\r
661 if( ulPendingInterrupts != 0UL )
\r
663 ThreadState_t *pxThreadState = ( ThreadState_t *) *( ( size_t * ) pxCurrentTCB );
\r
665 configASSERT( xPortRunning );
\r
667 /* The interrupt won't actually executed until
\r
668 pvInterruptEventMutex is released as it waits on both
\r
669 pvInterruptEventMutex and pvInterruptEvent.
\r
670 pvInterruptEvent is only set when the simulated
\r
671 interrupt is pended if the interrupt is pended
\r
672 from outside a critical section - hence it is set
\r
674 SetEvent( pvInterruptEvent );
\r
675 /* The calling task is going to wait for an event to ensure the
\r
676 interrupt that is pending executes immediately after the
\r
677 critical section is exited - so make sure the event is not
\r
678 already signaled. */
\r
679 ResetEvent( pxThreadState->pvYieldEvent );
\r
681 /* Mutex will be released now so the (simulated) interrupt can
\r
682 execute, so does not require releasing on function exit. */
\r
683 lMutexNeedsReleasing = pdFALSE;
\r
684 ReleaseMutex( pvInterruptEventMutex );
\r
685 WaitForSingleObject( pxThreadState->pvYieldEvent, INFINITE );
\r
690 if( pvInterruptEventMutex != NULL )
\r
692 if( lMutexNeedsReleasing == pdTRUE )
\r
694 configASSERT( xPortRunning );
\r
695 ReleaseMutex( pvInterruptEventMutex );
\r
699 /*-----------------------------------------------------------*/
\r