1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2020 Cambridge Consultants Ltd.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28 
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the Posix port.
31 *
32 * Each task has a pthread which eases use of standard debuggers
33 * (allowing backtraces of tasks etc). Threads for tasks that are not
34 * running are blocked in sigwait().
35 *
36 * Task switch is done by resuming the thread for the next task by
37 * signaling the condition variable and then waiting on a condition variable
38 * with the current thread.
39 *
40 * The timer interrupt uses SIGALRM and care is taken to ensure that
41 * the signal handler runs only on the thread for the current task.
42 *
43 * Use of part of the standard C library requires care as some
44 * functions can take pthread mutexes internally which can result in
45 * deadlocks as the FreeRTOS kernel can switch tasks while they're
46 * holding a pthread mutex.
47 *
48 * stdio (printf() and friends) should be called from a single task
49 * only or serialized with a FreeRTOS primitive such as a binary
50 * semaphore or mutex.
51 *
52 * Note: When using LLDB (the default debugger on macOS) with this port,
53 * suppress SIGUSR1 to prevent debugger interference. This can be
54 * done by adding the following line to ~/.lldbinit:
55 * `process handle SIGUSR1 -n true -p false -s false`
56 *----------------------------------------------------------*/
57 #ifdef __linux__
58     #define _GNU_SOURCE
59 #endif
60 #include "portmacro.h"
61 #include <errno.h>
62 #include <pthread.h>
63 #include <limits.h>
64 #include <signal.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <sys/time.h>
69 #include <sys/times.h>
70 #include <time.h>
71 #include <unistd.h>
72 
73 /* Scheduler includes. */
74 #include "FreeRTOS.h"
75 #include "task.h"
76 #include "timers.h"
77 #include "utils/wait_for_event.h"
78 /*-----------------------------------------------------------*/
79 
80 #define SIG_RESUME    SIGUSR1
81 
82 typedef struct THREAD
83 {
84     pthread_t pthread;
85     TaskFunction_t pxCode;
86     void * pvParams;
87     BaseType_t xDying;
88     struct event * ev;
89 } Thread_t;
90 
91 /*
92  * The additional per-thread data is stored at the beginning of the
93  * task's stack.
94  */
prvGetThreadFromTask(TaskHandle_t xTask)95 static inline Thread_t * prvGetThreadFromTask( TaskHandle_t xTask )
96 {
97     StackType_t * pxTopOfStack = *( StackType_t ** ) xTask;
98 
99     return ( Thread_t * ) ( pxTopOfStack + 1 );
100 }
101 
102 /*-----------------------------------------------------------*/
103 
104 static pthread_once_t hSigSetupThread = PTHREAD_ONCE_INIT;
105 static pthread_once_t hThreadKeyOnce = PTHREAD_ONCE_INIT;
106 static sigset_t xAllSignals;
107 static sigset_t xSchedulerOriginalSignalMask;
108 static pthread_t hMainThread = ( pthread_t ) NULL;
109 static volatile BaseType_t uxCriticalNesting;
110 static BaseType_t xSchedulerEnd = pdFALSE;
111 static pthread_t hTimerTickThread;
112 static bool xTimerTickThreadShouldRun;
113 static uint64_t prvStartTimeNs;
114 static pthread_key_t xThreadKey = 0;
115 /*-----------------------------------------------------------*/
116 
117 static void prvSetupSignalsAndSchedulerPolicy( void );
118 static void prvSetupTimerInterrupt( void );
119 static void * prvWaitForStart( void * pvParams );
120 static void prvSwitchThread( Thread_t * xThreadToResume,
121                              Thread_t * xThreadToSuspend );
122 static void prvSuspendSelf( Thread_t * thread );
123 static void prvResumeThread( Thread_t * xThreadId );
124 static void vPortSystemTickHandler( int sig );
125 static void vPortStartFirstTask( void );
126 static void prvPortYieldFromISR( void );
127 static void prvThreadKeyDestructor( void * pvData );
128 static void prvInitThreadKey( void );
129 static void prvMarkAsFreeRTOSThread( void );
130 static BaseType_t prvIsFreeRTOSThread( void );
131 static void prvDestroyThreadKey( void );
132 /*-----------------------------------------------------------*/
133 
prvThreadKeyDestructor(void * pvData)134 static void prvThreadKeyDestructor( void * pvData )
135 {
136     free( pvData );
137 }
138 /*-----------------------------------------------------------*/
139 
prvInitThreadKey(void)140 static void prvInitThreadKey( void )
141 {
142     pthread_key_create( &xThreadKey, prvThreadKeyDestructor );
143     /* Destroy xThreadKey when the process exits. */
144     atexit( prvDestroyThreadKey );
145 }
146 /*-----------------------------------------------------------*/
147 
prvMarkAsFreeRTOSThread(void)148 static void prvMarkAsFreeRTOSThread( void )
149 {
150     uint8_t * pucThreadData = NULL;
151 
152     ( void ) pthread_once( &hThreadKeyOnce, prvInitThreadKey );
153 
154     pucThreadData = malloc( 1 );
155     configASSERT( pucThreadData != NULL );
156 
157     *pucThreadData = 1;
158 
159     pthread_setspecific( xThreadKey, pucThreadData );
160 }
161 /*-----------------------------------------------------------*/
162 
prvIsFreeRTOSThread(void)163 static BaseType_t prvIsFreeRTOSThread( void )
164 {
165     uint8_t * pucThreadData = NULL;
166     BaseType_t xRet = pdFALSE;
167 
168     ( void ) pthread_once( &hThreadKeyOnce, prvInitThreadKey );
169 
170     pucThreadData = ( uint8_t * ) pthread_getspecific( xThreadKey );
171 
172     if( ( pucThreadData != NULL ) && ( *pucThreadData == 1 ) )
173     {
174         xRet = pdTRUE;
175     }
176 
177     return xRet;
178 }
179 /*-----------------------------------------------------------*/
180 
prvDestroyThreadKey(void)181 static void prvDestroyThreadKey( void )
182 {
183     pthread_key_delete( xThreadKey );
184 }
185 /*-----------------------------------------------------------*/
186 
187 static void prvFatalError( const char * pcCall,
188                            int iErrno ) __attribute__( ( __noreturn__ ) );
189 
prvFatalError(const char * pcCall,int iErrno)190 void prvFatalError( const char * pcCall,
191                     int iErrno )
192 {
193     fprintf( stderr, "%s: %s\n", pcCall, strerror( iErrno ) );
194     abort();
195 }
196 /*-----------------------------------------------------------*/
197 
prvPortSetCurrentThreadName(const char * pxThreadName)198 static void prvPortSetCurrentThreadName( const char * pxThreadName )
199 {
200     #ifdef __APPLE__
201         pthread_setname_np( pxThreadName );
202     #else
203         pthread_setname_np( pthread_self(), pxThreadName );
204     #endif
205 }
206 /*-----------------------------------------------------------*/
207 
208 /*
209  * See header file for description.
210  */
pxPortInitialiseStack(StackType_t * pxTopOfStack,StackType_t * pxEndOfStack,TaskFunction_t pxCode,void * pvParameters)211 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
212                                      StackType_t * pxEndOfStack,
213                                      TaskFunction_t pxCode,
214                                      void * pvParameters )
215 {
216     Thread_t * thread;
217     pthread_attr_t xThreadAttributes;
218     size_t ulStackSize;
219     int iRet;
220 
221     ( void ) pthread_once( &hSigSetupThread, prvSetupSignalsAndSchedulerPolicy );
222 
223     /*
224      * Store the additional thread data at the start of the stack.
225      */
226     thread = ( Thread_t * ) ( pxTopOfStack + 1 ) - 1;
227     pxTopOfStack = ( StackType_t * ) thread - 1;
228 
229     /* Ensure that there is enough space to store Thread_t on the stack. */
230     ulStackSize = ( size_t ) ( pxTopOfStack + 1 - pxEndOfStack ) * sizeof( *pxTopOfStack );
231     configASSERT( ulStackSize > sizeof( Thread_t ) );
232 
233     thread->pxCode = pxCode;
234     thread->pvParams = pvParameters;
235     thread->xDying = pdFALSE;
236 
237     pthread_attr_init( &xThreadAttributes );
238 
239     thread->ev = event_create();
240 
241     vPortEnterCritical();
242 
243     iRet = pthread_create( &thread->pthread, &xThreadAttributes,
244                            prvWaitForStart, thread );
245 
246     if( iRet != 0 )
247     {
248         prvFatalError( "pthread_create", iRet );
249     }
250 
251     vPortExitCritical();
252 
253     return pxTopOfStack;
254 }
255 /*-----------------------------------------------------------*/
256 
vPortStartFirstTask(void)257 void vPortStartFirstTask( void )
258 {
259     Thread_t * pxFirstThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
260 
261     /* Start the first task. */
262     prvResumeThread( pxFirstThread );
263 }
264 /*-----------------------------------------------------------*/
265 
266 /*
267  * See header file for description.
268  */
xPortStartScheduler(void)269 BaseType_t xPortStartScheduler( void )
270 {
271     int iSignal;
272     sigset_t xSignals;
273 
274     hMainThread = pthread_self();
275     prvPortSetCurrentThreadName( "Scheduler" );
276 
277     /* Start the timer that generates the tick ISR(SIGALRM).
278      * Interrupts are disabled here already. */
279     prvSetupTimerInterrupt();
280 
281     /*
282      * Block SIG_RESUME before starting any tasks so the main thread can sigwait on it.
283      * To sigwait on an unblocked signal is undefined.
284      * https://pubs.opengroup.org/onlinepubs/009604499/functions/sigwait.html
285      */
286     sigemptyset( &xSignals );
287     sigaddset( &xSignals, SIG_RESUME );
288     ( void ) pthread_sigmask( SIG_BLOCK, &xSignals, NULL );
289 
290     /* Start the first task. */
291     vPortStartFirstTask();
292 
293     /* Wait until signaled by vPortEndScheduler(). */
294     while( xSchedulerEnd != pdTRUE )
295     {
296         sigwait( &xSignals, &iSignal );
297     }
298 
299     /*
300      * clear out the variable that is used to end the scheduler, otherwise
301      * subsequent scheduler restarts will end immediately.
302      */
303     xSchedulerEnd = pdFALSE;
304 
305     /* Reset pthread_once_t, needed to restart the scheduler again.
306      * memset the internal struct members for MacOS/Linux Compatibility */
307     #if __APPLE__
308         hSigSetupThread.__sig = _PTHREAD_ONCE_SIG_init;
309         hThreadKeyOnce.__sig = _PTHREAD_ONCE_SIG_init;
310         memset( ( void * ) &hSigSetupThread.__opaque, 0, sizeof( hSigSetupThread.__opaque ) );
311         memset( ( void * ) &hThreadKeyOnce.__opaque, 0, sizeof( hThreadKeyOnce.__opaque ) );
312     #else /* Linux PTHREAD library*/
313         hSigSetupThread = ( pthread_once_t ) PTHREAD_ONCE_INIT;
314         hThreadKeyOnce = ( pthread_once_t ) PTHREAD_ONCE_INIT;
315     #endif /* __APPLE__*/
316 
317     /* Restore original signal mask. */
318     ( void ) pthread_sigmask( SIG_SETMASK, &xSchedulerOriginalSignalMask, NULL );
319 
320     return 0;
321 }
322 /*-----------------------------------------------------------*/
323 
vPortEndScheduler(void)324 void vPortEndScheduler( void )
325 {
326     Thread_t * pxCurrentThread;
327     BaseType_t xIsFreeRTOSThread;
328 
329     /* Stop the timer tick thread. */
330     xTimerTickThreadShouldRun = false;
331     pthread_join( hTimerTickThread, NULL );
332 
333     /* Check whether the current thread is a FreeRTOS thread.
334      * This has to happen before the scheduler is signaled to exit
335      * its loop to prevent data races on the thread key. */
336     xIsFreeRTOSThread = prvIsFreeRTOSThread();
337 
338     /* Signal the scheduler to exit its loop. */
339     xSchedulerEnd = pdTRUE;
340     ( void ) pthread_kill( hMainThread, SIG_RESUME );
341 
342     /* Waiting to be deleted here. */
343     if( xIsFreeRTOSThread == pdTRUE )
344     {
345         pxCurrentThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
346         event_wait( pxCurrentThread->ev );
347     }
348 
349     pthread_testcancel();
350 }
351 /*-----------------------------------------------------------*/
352 
vPortEnterCritical(void)353 void vPortEnterCritical( void )
354 {
355     if( uxCriticalNesting == 0 )
356     {
357         vPortDisableInterrupts();
358     }
359 
360     uxCriticalNesting++;
361 }
362 /*-----------------------------------------------------------*/
363 
vPortExitCritical(void)364 void vPortExitCritical( void )
365 {
366     uxCriticalNesting--;
367 
368     /* If we have reached 0 then re-enable the interrupts. */
369     if( uxCriticalNesting == 0 )
370     {
371         vPortEnableInterrupts();
372     }
373 }
374 /*-----------------------------------------------------------*/
375 
prvPortYieldFromISR(void)376 static void prvPortYieldFromISR( void )
377 {
378     Thread_t * xThreadToSuspend;
379     Thread_t * xThreadToResume;
380 
381     xThreadToSuspend = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
382 
383     vTaskSwitchContext();
384 
385     xThreadToResume = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
386 
387     prvSwitchThread( xThreadToResume, xThreadToSuspend );
388 }
389 /*-----------------------------------------------------------*/
390 
vPortYield(void)391 void vPortYield( void )
392 {
393     /* This must never be called from outside of a FreeRTOS-owned thread, or
394      * the thread could get stuck in a suspended state. */
395     configASSERT( prvIsFreeRTOSThread() == pdTRUE );
396 
397     vPortEnterCritical();
398 
399     prvPortYieldFromISR();
400 
401     vPortExitCritical();
402 }
403 /*-----------------------------------------------------------*/
404 
vPortDisableInterrupts(void)405 void vPortDisableInterrupts( void )
406 {
407     if( prvIsFreeRTOSThread() == pdTRUE )
408     {
409         pthread_sigmask( SIG_BLOCK, &xAllSignals, NULL );
410     }
411 }
412 /*-----------------------------------------------------------*/
413 
vPortEnableInterrupts(void)414 void vPortEnableInterrupts( void )
415 {
416     if( prvIsFreeRTOSThread() == pdTRUE )
417     {
418         pthread_sigmask( SIG_UNBLOCK, &xAllSignals, NULL );
419     }
420 }
421 /*-----------------------------------------------------------*/
422 
xPortSetInterruptMask(void)423 UBaseType_t xPortSetInterruptMask( void )
424 {
425     /* Interrupts are always disabled inside ISRs (signals
426      * handlers). */
427     return ( UBaseType_t ) 0;
428 }
429 /*-----------------------------------------------------------*/
430 
vPortClearInterruptMask(UBaseType_t uxMask)431 void vPortClearInterruptMask( UBaseType_t uxMask )
432 {
433     ( void ) uxMask;
434 }
435 /*-----------------------------------------------------------*/
436 
prvGetTimeNs(void)437 static uint64_t prvGetTimeNs( void )
438 {
439     struct timespec t;
440 
441     clock_gettime( CLOCK_MONOTONIC, &t );
442 
443     return ( uint64_t ) t.tv_sec * ( uint64_t ) 1000000000UL + ( uint64_t ) t.tv_nsec;
444 }
445 /*-----------------------------------------------------------*/
446 
447 /* commented as part of the code below in vPortSystemTickHandler,
448  * to adjust timing according to full demo requirements */
449 /* static uint64_t prvTickCount; */
450 
prvTimerTickHandler(void * arg)451 static void * prvTimerTickHandler( void * arg )
452 {
453     ( void ) arg;
454 
455     prvMarkAsFreeRTOSThread();
456 
457     prvPortSetCurrentThreadName( "Scheduler timer" );
458 
459     while( xTimerTickThreadShouldRun )
460     {
461         /*
462          * signal to the active task to cause tick handling or
463          * preemption (if enabled)
464          */
465         Thread_t * thread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
466         pthread_kill( thread->pthread, SIGALRM );
467         usleep( portTICK_RATE_MICROSECONDS );
468     }
469 
470     return NULL;
471 }
472 /*-----------------------------------------------------------*/
473 
474 /*
475  * Setup the systick timer to generate the tick interrupts at the required
476  * frequency.
477  */
prvSetupTimerInterrupt(void)478 void prvSetupTimerInterrupt( void )
479 {
480     xTimerTickThreadShouldRun = true;
481     pthread_create( &hTimerTickThread, NULL, prvTimerTickHandler, NULL );
482 
483     prvStartTimeNs = prvGetTimeNs();
484 }
485 /*-----------------------------------------------------------*/
486 
vPortSystemTickHandler(int sig)487 static void vPortSystemTickHandler( int sig )
488 {
489     if( prvIsFreeRTOSThread() == pdTRUE )
490     {
491         Thread_t * pxThreadToSuspend;
492         Thread_t * pxThreadToResume;
493 
494         ( void ) sig;
495 
496         uxCriticalNesting++; /* Signals are blocked in this signal handler. */
497 
498         pxThreadToSuspend = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
499 
500         if( xTaskIncrementTick() != pdFALSE )
501         {
502             /* Select Next Task. */
503             vTaskSwitchContext();
504 
505             pxThreadToResume = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
506 
507             prvSwitchThread( pxThreadToResume, pxThreadToSuspend );
508         }
509 
510         uxCriticalNesting--;
511     }
512     else
513     {
514         fprintf( stderr, "vPortSystemTickHandler called from non-FreeRTOS thread\n" );
515     }
516 }
517 /*-----------------------------------------------------------*/
518 
vPortThreadDying(void * pxTaskToDelete,volatile BaseType_t * pxPendYield)519 void vPortThreadDying( void * pxTaskToDelete,
520                        volatile BaseType_t * pxPendYield )
521 {
522     Thread_t * pxThread = prvGetThreadFromTask( pxTaskToDelete );
523 
524     ( void ) pxPendYield;
525 
526     pxThread->xDying = pdTRUE;
527 }
528 /*-----------------------------------------------------------*/
529 
vPortCancelThread(void * pxTaskToDelete)530 void vPortCancelThread( void * pxTaskToDelete )
531 {
532     Thread_t * pxThreadToCancel = prvGetThreadFromTask( pxTaskToDelete );
533 
534     /*
535      * The thread has already been suspended so it can be safely cancelled.
536      */
537     pthread_cancel( pxThreadToCancel->pthread );
538     event_signal( pxThreadToCancel->ev );
539     pthread_join( pxThreadToCancel->pthread, NULL );
540     event_delete( pxThreadToCancel->ev );
541 }
542 /*-----------------------------------------------------------*/
543 
prvWaitForStart(void * pvParams)544 static void * prvWaitForStart( void * pvParams )
545 {
546     Thread_t * pxThread = pvParams;
547 
548     prvMarkAsFreeRTOSThread();
549 
550     prvSuspendSelf( pxThread );
551 
552     /* Resumed for the first time, unblocks all signals. */
553     uxCriticalNesting = 0;
554     vPortEnableInterrupts();
555 
556     /* Set thread name */
557     prvPortSetCurrentThreadName( pcTaskGetName( xTaskGetCurrentTaskHandle() ) );
558 
559     /* Call the task's entry point. */
560     pxThread->pxCode( pxThread->pvParams );
561 
562     /* A function that implements a task must not exit or attempt to return to
563      * its caller as there is nothing to return to. If a task wants to exit it
564      * should instead call vTaskDelete( NULL ). Artificially force an assert()
565      * to be triggered if configASSERT() is defined, so application writers can
566      * catch the error. */
567     configASSERT( pdFALSE );
568 
569     return NULL;
570 }
571 /*-----------------------------------------------------------*/
572 
prvSwitchThread(Thread_t * pxThreadToResume,Thread_t * pxThreadToSuspend)573 static void prvSwitchThread( Thread_t * pxThreadToResume,
574                              Thread_t * pxThreadToSuspend )
575 {
576     BaseType_t uxSavedCriticalNesting;
577 
578     if( pxThreadToSuspend != pxThreadToResume )
579     {
580         /*
581          * Switch tasks.
582          *
583          * The critical section nesting is per-task, so save it on the
584          * stack of the current (suspending thread), restoring it when
585          * we switch back to this task.
586          */
587         uxSavedCriticalNesting = uxCriticalNesting;
588 
589         prvResumeThread( pxThreadToResume );
590 
591         if( pxThreadToSuspend->xDying == pdTRUE )
592         {
593             pthread_exit( NULL );
594         }
595 
596         prvSuspendSelf( pxThreadToSuspend );
597 
598         uxCriticalNesting = uxSavedCriticalNesting;
599     }
600 }
601 /*-----------------------------------------------------------*/
602 
prvSuspendSelf(Thread_t * thread)603 static void prvSuspendSelf( Thread_t * thread )
604 {
605     /*
606      * Suspend this thread by waiting for a pthread_cond_signal event.
607      *
608      * A suspended thread must not handle signals (interrupts) so
609      * all signals must be blocked by calling this from:
610      *
611      * - Inside a critical section (vPortEnterCritical() /
612      *   vPortExitCritical()).
613      *
614      * - From a signal handler that has all signals masked.
615      *
616      * - A thread with all signals blocked with pthread_sigmask().
617      */
618     event_wait( thread->ev );
619     pthread_testcancel();
620 }
621 
622 /*-----------------------------------------------------------*/
623 
prvResumeThread(Thread_t * xThreadId)624 static void prvResumeThread( Thread_t * xThreadId )
625 {
626     if( pthread_self() != xThreadId->pthread )
627     {
628         event_signal( xThreadId->ev );
629     }
630 }
631 /*-----------------------------------------------------------*/
632 
prvSetupSignalsAndSchedulerPolicy(void)633 static void prvSetupSignalsAndSchedulerPolicy( void )
634 {
635     struct sigaction sigtick;
636     int iRet;
637 
638     hMainThread = pthread_self();
639 
640     /* Initialise common signal masks. */
641     sigfillset( &xAllSignals );
642 
643     /* Don't block SIGINT so this can be used to break into GDB while
644      * in a critical section. */
645     sigdelset( &xAllSignals, SIGINT );
646 
647     /*
648      * Block all signals in this thread so all new threads
649      * inherits this mask.
650      *
651      * When a thread is resumed for the first time, all signals
652      * will be unblocked.
653      */
654     ( void ) pthread_sigmask( SIG_SETMASK,
655                               &xAllSignals,
656                               &xSchedulerOriginalSignalMask );
657 
658     sigtick.sa_flags = 0;
659     sigtick.sa_handler = vPortSystemTickHandler;
660     sigfillset( &sigtick.sa_mask );
661 
662     iRet = sigaction( SIGALRM, &sigtick, NULL );
663 
664     if( iRet == -1 )
665     {
666         prvFatalError( "sigaction", errno );
667     }
668 }
669 /*-----------------------------------------------------------*/
670 
ulPortGetRunTime(void)671 uint32_t ulPortGetRunTime( void )
672 {
673     struct tms xTimes;
674 
675     times( &xTimes );
676 
677     return ( uint32_t ) xTimes.tms_utime;
678 }
679 /*-----------------------------------------------------------*/
680