Skip to content

Commit

Permalink
Use interval timer in posix port for valgrind
Browse files Browse the repository at this point in the history
  • Loading branch information
chinglee-iot committed Jan 25, 2024
1 parent 8e664fc commit 7f0f827
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions portable/ThirdParty/GCC/Posix/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ static sigset_t xSchedulerOriginalSignalMask;
static pthread_t hMainThread = ( pthread_t ) NULL;
static volatile BaseType_t uxCriticalNesting;
static BaseType_t xSchedulerEnd = pdFALSE;
static pthread_t hTimerTickThread;
static bool xTimerTickThreadShouldRun;
static uint64_t prvStartTimeNs;
static List_t xThreadList;
/*-----------------------------------------------------------*/
Expand Down Expand Up @@ -294,9 +292,23 @@ BaseType_t xPortStartScheduler( void )

void vPortEndScheduler( void )
{
/* Stop the timer tick thread. */
xTimerTickThreadShouldRun = false;
pthread_join( hTimerTickThread, NULL );
struct itimerval itimer;
struct sigaction sigtick;
Thread_t * xCurrentThread;

/* Stop the timer and ignore any pending SIGALRMs that would end
* up running on the main thread when it is resumed. */
itimer.it_value.tv_sec = 0;
itimer.it_value.tv_usec = 0;

itimer.it_interval.tv_sec = 0;
itimer.it_interval.tv_usec = 0;
( void ) setitimer( ITIMER_REAL, &itimer, NULL );

sigtick.sa_flags = 0;
sigtick.sa_handler = SIG_IGN;
sigemptyset( &sigtick.sa_mask );
sigaction( SIGALRM, &sigtick, NULL );

/* Signal the scheduler to exit its loop. */
xSchedulerEnd = pdTRUE;
Expand Down Expand Up @@ -394,35 +406,38 @@ static uint64_t prvGetTimeNs( void )
* to adjust timing according to full demo requirements */
/* static uint64_t prvTickCount; */

static void * prvTimerTickHandler( void * arg )
{
( void ) arg;

prvPortSetCurrentThreadName("Scheduler timer");

while( xTimerTickThreadShouldRun )
{
/*
* signal to the active task to cause tick handling or
* preemption (if enabled)
*/
Thread_t * thread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
pthread_kill( thread->pthread, SIGALRM );
usleep( portTICK_RATE_MICROSECONDS );
}

return NULL;
}
/*-----------------------------------------------------------*/

/*
* Setup the systick timer to generate the tick interrupts at the required
* frequency.
*/
void prvSetupTimerInterrupt( void )
{
xTimerTickThreadShouldRun = true;
pthread_create( &hTimerTickThread, NULL, prvTimerTickHandler, NULL );
struct itimerval itimer;
int iRet;

/* Initialise the structure with the current timer information. */
iRet = getitimer( ITIMER_REAL, &itimer );

if( iRet == -1 )
{
prvFatalError( "getitimer", errno );
}

/* Set the interval between timer events. */
itimer.it_interval.tv_sec = 0;
itimer.it_interval.tv_usec = portTICK_RATE_MICROSECONDS;

/* Set the current count-down. */
itimer.it_value.tv_sec = 0;
itimer.it_value.tv_usec = portTICK_RATE_MICROSECONDS;

/* Set-up the timer interrupt. */
iRet = setitimer( ITIMER_REAL, &itimer, NULL );

if( iRet == -1 )
{
prvFatalError( "setitimer", errno );
}

prvStartTimeNs = prvGetTimeNs();
}
Expand Down

0 comments on commit 7f0f827

Please sign in to comment.