]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MSVC-MingW/port.c
c2e2a82acdf4d24fcf9f00bb90fa1e5d82a790fe
[freertos] / FreeRTOS / Source / portable / MSVC-MingW / port.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Standard includes. */\r
29 #include <stdio.h>\r
30 \r
31 /* Scheduler includes. */\r
32 #include "FreeRTOS.h"\r
33 #include "task.h"\r
34 \r
35 #ifdef __GNUC__\r
36         #include "mmsystem.h"\r
37 #else\r
38         #pragma comment(lib, "winmm.lib")\r
39 #endif\r
40 \r
41 #define portMAX_INTERRUPTS                              ( ( uint32_t ) sizeof( uint32_t ) * 8UL ) /* The number of bits in an uint32_t. */\r
42 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
43 \r
44 /* The priorities at which the various components of the simulation execute. */\r
45 #define portDELETE_SELF_THREAD_PRIORITY                  THREAD_PRIORITY_TIME_CRITICAL /* Must be highest. */\r
46 #define portSIMULATED_INTERRUPTS_THREAD_PRIORITY THREAD_PRIORITY_TIME_CRITICAL\r
47 #define portSIMULATED_TIMER_THREAD_PRIORITY              THREAD_PRIORITY_HIGHEST\r
48 #define portTASK_THREAD_PRIORITY                                 THREAD_PRIORITY_ABOVE_NORMAL\r
49 \r
50 /*\r
51  * Created as a high priority thread, this function uses a timer to simulate\r
52  * a tick interrupt being generated on an embedded target.  In this Windows\r
53  * environment the timer does not achieve anything approaching real time\r
54  * performance though.\r
55  */\r
56 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );\r
57 \r
58 /*\r
59  * Process all the simulated interrupts - each represented by a bit in\r
60  * ulPendingInterrupts variable.\r
61  */\r
62 static void prvProcessSimulatedInterrupts( void );\r
63 \r
64 /*\r
65  * Interrupt handlers used by the kernel itself.  These are executed from the\r
66  * simulated interrupt handler thread.\r
67  */\r
68 static uint32_t prvProcessYieldInterrupt( void );\r
69 static uint32_t prvProcessTickInterrupt( void );\r
70 \r
71 /*\r
72  * Called when the process exits to let Windows know the high timer resolution\r
73  * is no longer required.\r
74  */\r
75 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType );\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /* The WIN32 simulator runs each task in a thread.  The context switching is\r
80 managed by the threads, so the task stack does not have to be managed directly,\r
81 although the task stack is still used to hold an xThreadState structure this is\r
82 the only thing it will ever hold.  The structure indirectly maps the task handle\r
83 to a thread handle. */\r
84 typedef struct\r
85 {\r
86         /* Handle of the thread that executes the task. */\r
87         void *pvThread;\r
88 \r
89         /* 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
92         void *pvYieldEvent;\r
93 } ThreadState_t;\r
94 \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
98 \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
101 pending. */\r
102 static void *pvInterruptEvent = NULL;\r
103 \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
107 \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
114 started anyway. */\r
115 static volatile uint32_t ulCriticalNesting = 9999UL;\r
116 \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
121 \r
122 /* Pointer to the TCB of the currently executing task. */\r
123 extern void * volatile pxCurrentTCB;\r
124 \r
125 /* Used to ensure nothing is processed during the startup sequence. */\r
126 static BaseType_t xPortRunning = pdFALSE;\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )\r
131 {\r
132 TickType_t xMinimumWindowsBlockTime;\r
133 TIMECAPS xTimeCaps;\r
134 \r
135         /* Set the timer resolution to the maximum possible. */\r
136         if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )\r
137         {\r
138                 xMinimumWindowsBlockTime = ( TickType_t ) xTimeCaps.wPeriodMin;\r
139                 timeBeginPeriod( xTimeCaps.wPeriodMin );\r
140 \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
144         }\r
145         else\r
146         {\r
147                 xMinimumWindowsBlockTime = ( TickType_t ) 20;\r
148         }\r
149 \r
150         /* Just to prevent compiler warnings. */\r
151         ( void ) lpParameter;\r
152 \r
153         for( ;; )\r
154         {\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
160                 environment. */\r
161                 if( portTICK_PERIOD_MS < xMinimumWindowsBlockTime )\r
162                 {\r
163                         Sleep( xMinimumWindowsBlockTime );\r
164                 }\r
165                 else\r
166                 {\r
167                         Sleep( portTICK_PERIOD_MS );\r
168                 }\r
169 \r
170                 configASSERT( xPortRunning );\r
171 \r
172                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
173 \r
174                 /* The timer has expired, generate the simulated tick event. */\r
175                 ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );\r
176 \r
177                 /* The interrupt is now pending - notify the simulated interrupt\r
178                 handler thread. */\r
179                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
180                 {\r
181                         SetEvent( pvInterruptEvent );\r
182                 }\r
183 \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
187         }\r
188 \r
189         #ifdef __GNUC__\r
190                 /* Should never reach here - MingW complains if you leave this line out,\r
191                 MSVC complains if you put it in. */\r
192                 return 0;\r
193         #endif\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 static BOOL WINAPI prvEndProcess( DWORD dwCtrlType )\r
198 {\r
199 TIMECAPS xTimeCaps;\r
200 \r
201         ( void ) dwCtrlType;\r
202 \r
203         if( timeGetDevCaps( &xTimeCaps, sizeof( xTimeCaps ) ) == MMSYSERR_NOERROR )\r
204         {\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
208         }\r
209 \r
210         return pdFALSE;\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
215 {\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
219 \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
227 \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
235 \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
242 \r
243         return ( StackType_t * ) pxThreadState;\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 BaseType_t xPortStartScheduler( void )\r
248 {\r
249 void *pvHandle = NULL;\r
250 int32_t lSuccess;\r
251 ThreadState_t *pxThreadState = NULL;\r
252 SYSTEM_INFO xSystemInfo;\r
253 \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
259         {\r
260                 printf( "This version of the FreeRTOS Windows port can only be used on multi-core hosts.\r\n" );\r
261                 lSuccess = pdFAIL;\r
262         }\r
263         else\r
264         {\r
265                 lSuccess = pdPASS;\r
266 \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
270                 {\r
271                         printf( "SetPriorityClass() failed\r\n" );\r
272                 }\r
273 \r
274                 /* Install the interrupt handlers used by the scheduler itself. */\r
275                 vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );\r
276                 vPortSetInterruptHandler( portINTERRUPT_TICK, prvProcessTickInterrupt );\r
277 \r
278                 /* Create the events and mutexes that are used to synchronise all the\r
279                 threads. */\r
280                 pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );\r
281                 pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
282 \r
283                 if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) )\r
284                 {\r
285                         lSuccess = pdFAIL;\r
286                 }\r
287 \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
293                 {\r
294                         lSuccess = pdFAIL;\r
295                 }\r
296         }\r
297 \r
298         if( lSuccess == pdPASS )\r
299         {\r
300                 if( SetThreadPriority( pvHandle, portSIMULATED_INTERRUPTS_THREAD_PRIORITY ) == 0 )\r
301                 {\r
302                         lSuccess = pdFAIL;\r
303                 }\r
304                 SetThreadPriorityBoost( pvHandle, TRUE );\r
305                 SetThreadAffinityMask( pvHandle, 0x01 );\r
306         }\r
307 \r
308         if( lSuccess == pdPASS )\r
309         {\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
316                 {\r
317                         SetThreadPriority( pvHandle, portSIMULATED_TIMER_THREAD_PRIORITY );\r
318                         SetThreadPriorityBoost( pvHandle, TRUE );\r
319                         SetThreadAffinityMask( pvHandle, 0x01 );\r
320                         ResumeThread( pvHandle );\r
321                 }\r
322 \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
327 \r
328                 /* Start the first task. */\r
329                 ResumeThread( pxThreadState->pvThread );\r
330 \r
331                 /* Handle all simulated interrupts - including yield requests and\r
332                 simulated ticks. */\r
333                 prvProcessSimulatedInterrupts();\r
334         }\r
335 \r
336         /* Would not expect to return from prvProcessSimulatedInterrupts(), so should\r
337         not get here. */\r
338         return 0;\r
339 }\r
340 /*-----------------------------------------------------------*/\r
341 \r
342 static uint32_t prvProcessYieldInterrupt( void )\r
343 {\r
344         return pdTRUE;\r
345 }\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 static uint32_t prvProcessTickInterrupt( void )\r
349 {\r
350 uint32_t ulSwitchRequired;\r
351 \r
352         /* Process the tick itself. */\r
353         configASSERT( xPortRunning );\r
354         ulSwitchRequired = ( uint32_t ) xTaskIncrementTick();\r
355 \r
356         return ulSwitchRequired;\r
357 }\r
358 /*-----------------------------------------------------------*/\r
359 \r
360 static void prvProcessSimulatedInterrupts( void )\r
361 {\r
362 uint32_t ulSwitchRequired, i;\r
363 ThreadState_t *pxThreadState;\r
364 void *pvObjectList[ 2 ];\r
365 CONTEXT xContext;\r
366 \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
372 \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
377 \r
378         xPortRunning = pdTRUE;\r
379 \r
380         for(;;)\r
381         {\r
382                 WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );\r
383 \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
387 \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
391                 {\r
392                         /* Is the simulated interrupt pending? */\r
393                         if( ulPendingInterrupts & ( 1UL << i ) )\r
394                         {\r
395                                 /* Is a handler installed? */\r
396                                 if( ulIsrHandler[ i ] != NULL )\r
397                                 {\r
398                                         /* Run the actual handler. */\r
399                                         if( ulIsrHandler[ i ]() != pdFALSE )\r
400                                         {\r
401                                                 ulSwitchRequired |= ( 1 << i );\r
402                                         }\r
403                                 }\r
404 \r
405                                 /* Clear the interrupt pending bit. */\r
406                                 ulPendingInterrupts &= ~( 1UL << i );\r
407                         }\r
408                 }\r
409 \r
410                 if( ulSwitchRequired != pdFALSE )\r
411                 {\r
412                         void *pvOldCurrentTCB;\r
413 \r
414                         pvOldCurrentTCB = pxCurrentTCB;\r
415 \r
416                         /* Select the next task to run. */\r
417                         vTaskSwitchContext();\r
418 \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
422                         {\r
423                                 /* Suspend the old thread. */\r
424                                 pxThreadState = ( ThreadState_t *) *( ( size_t * ) pvOldCurrentTCB );\r
425                                 SuspendThread( pxThreadState->pvThread );\r
426 \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
434                                 been left it. */\r
435                                 xContext.ContextFlags = CONTEXT_INTEGER;\r
436                                 ( void ) GetThreadContext( pxThreadState->pvThread, &xContext );\r
437 \r
438                                 /* Obtain the state of the task now selected to enter the\r
439                                 Running state. */\r
440                                 pxThreadState = ( ThreadState_t * ) ( *( size_t *) pxCurrentTCB );\r
441 \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
446                         }\r
447                 }\r
448 \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
458         }\r
459 }\r
460 /*-----------------------------------------------------------*/\r
461 \r
462 void vPortDeleteThread( void *pvTaskToDelete )\r
463 {\r
464 ThreadState_t *pxThreadState;\r
465 uint32_t ulErrorCode;\r
466 \r
467         /* Remove compiler warnings if configASSERT() is not defined. */\r
468         ( void ) ulErrorCode;\r
469 \r
470         /* Find the handle of the thread being deleted. */\r
471         pxThreadState = ( ThreadState_t * ) ( *( size_t *) pvTaskToDelete );\r
472 \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
476         different task. */\r
477         if( pxThreadState->pvThread != NULL )\r
478         {\r
479                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
480 \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
484                 freed. */\r
485                 ulErrorCode = TerminateThread( pxThreadState->pvThread, 0 );\r
486                 configASSERT( ulErrorCode );\r
487 \r
488                 ulErrorCode = CloseHandle( pxThreadState->pvThread );\r
489                 configASSERT( ulErrorCode );\r
490 \r
491                 ReleaseMutex( pvInterruptEventMutex );\r
492         }\r
493 }\r
494 /*-----------------------------------------------------------*/\r
495 \r
496 void vPortCloseRunningThread( void *pvTaskToDelete, volatile BaseType_t *pxPendYield )\r
497 {\r
498 ThreadState_t *pxThreadState;\r
499 void *pvThread;\r
500 uint32_t ulErrorCode;\r
501 \r
502         /* Remove compiler warnings if configASSERT() is not defined. */\r
503         ( void ) ulErrorCode;\r
504 \r
505         /* Find the handle of the thread being deleted. */\r
506         pxThreadState = ( ThreadState_t * ) ( *( size_t *) pvTaskToDelete );\r
507         pvThread = pxThreadState->pvThread;\r
508 \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
512         memory leak. */\r
513         SetThreadPriority( pvThread, portDELETE_SELF_THREAD_PRIORITY );\r
514 \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
518 \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
522 \r
523         /* Close the thread. */\r
524         ulErrorCode = CloseHandle( pvThread );\r
525         configASSERT( ulErrorCode );\r
526 \r
527         /* This is called from a critical section, which must be exited before the\r
528         thread stops. */\r
529         taskEXIT_CRITICAL();\r
530 \r
531         ExitThread( 0 );\r
532 }\r
533 /*-----------------------------------------------------------*/\r
534 \r
535 void vPortEndScheduler( void )\r
536 {\r
537         exit( 0 );\r
538 }\r
539 /*-----------------------------------------------------------*/\r
540 \r
541 void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber )\r
542 {\r
543 ThreadState_t *pxThreadState = ( ThreadState_t *) *( ( size_t * ) pxCurrentTCB );\r
544 \r
545         configASSERT( xPortRunning );\r
546 \r
547         if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )\r
548         {\r
549                 /* Yield interrupts are processed even when critical nesting is\r
550                 non-zero. */\r
551                 WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
552                 ulPendingInterrupts |= ( 1 << ulInterruptNumber );\r
553 \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
559                 {\r
560                         SetEvent( pvInterruptEvent );\r
561 \r
562                         if( ulInterruptNumber == portINTERRUPT_YIELD )\r
563                         {\r
564                                 /* Going to wait for an event - make sure the event is not already\r
565                                 signaled. */\r
566                                 ResetEvent( pxThreadState->pvYieldEvent );\r
567                         }\r
568                 }\r
569 \r
570                 ReleaseMutex( pvInterruptEventMutex );\r
571 \r
572                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
573                 {\r
574                         if( ulInterruptNumber == portINTERRUPT_YIELD )\r
575                         {\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
581                         }\r
582                 }\r
583         }\r
584 }\r
585 /*-----------------------------------------------------------*/\r
586 \r
587 void vPortSetInterruptHandler( uint32_t ulInterruptNumber, uint32_t (*pvHandler)( void ) )\r
588 {\r
589         if( ulInterruptNumber < portMAX_INTERRUPTS )\r
590         {\r
591                 if( pvInterruptEventMutex != NULL )\r
592                 {\r
593                         WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
594                         ulIsrHandler[ ulInterruptNumber ] = pvHandler;\r
595                         ReleaseMutex( pvInterruptEventMutex );\r
596                 }\r
597                 else\r
598                 {\r
599                         ulIsrHandler[ ulInterruptNumber ] = pvHandler;\r
600                 }\r
601         }\r
602 }\r
603 /*-----------------------------------------------------------*/\r
604 \r
605 void vPortEnterCritical( void )\r
606 {\r
607         if( xPortRunning == pdTRUE )\r
608         {\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
612         }\r
613 \r
614         ulCriticalNesting++;\r
615 }\r
616 /*-----------------------------------------------------------*/\r
617 \r
618 void vPortExitCritical( void )\r
619 {\r
620 int32_t lMutexNeedsReleasing, lWaitForYield = pdFALSE;\r
621 \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
624 \r
625         lMutexNeedsReleasing = pdTRUE;\r
626 \r
627         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
628         {\r
629                 if( ulCriticalNesting == ( portNO_CRITICAL_NESTING + 1 ) )\r
630                 {\r
631                         ulCriticalNesting--;\r
632 \r
633                         /* Were any interrupts set to pending while interrupts were\r
634                         (simulated) disabled? */\r
635                         if( ulPendingInterrupts != 0UL )\r
636                         {\r
637                                 ThreadState_t *pxThreadState = ( ThreadState_t *) *( ( size_t * ) pxCurrentTCB );\r
638 \r
639                                 configASSERT( xPortRunning );\r
640                                 SetEvent( pvInterruptEvent );\r
641 \r
642                                 if( ( ulPendingInterrupts & ( 1 << portINTERRUPT_YIELD ) ) != 0 )\r
643                                 {\r
644                                         /* Going to wait for an event - make sure the event is not already\r
645                                         signaled. */\r
646                                         ResetEvent( pxThreadState->pvYieldEvent );\r
647                                         lWaitForYield = pdTRUE;\r
648                                 }\r
649 \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
654 \r
655                                 if( lWaitForYield != pdFALSE )\r
656                                 {\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
662                                 }\r
663                         }\r
664                 }\r
665                 else\r
666                 {\r
667                         /* Tick interrupts will still not be processed as the critical\r
668                         nesting depth will not be zero. */\r
669                         ulCriticalNesting--;\r
670                 }\r
671         }\r
672 \r
673         if( pvInterruptEventMutex != NULL )\r
674         {\r
675                 if( lMutexNeedsReleasing == pdTRUE )\r
676                 {\r
677                         configASSERT( xPortRunning );\r
678                         ReleaseMutex( pvInterruptEventMutex );\r
679                 }\r
680         }\r
681 }\r
682 /*-----------------------------------------------------------*/\r
683 \r