1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-30 15:52:12 +01:00

Osx Simulator Not running quickly but at least it's staying up

This commit is contained in:
James Cotton 2012-03-13 11:31:50 -05:00
parent 898a595d81
commit a2bc7b8668
3 changed files with 63 additions and 22 deletions

View File

@ -135,6 +135,7 @@ typedef struct THREAD_SUSPENSIONS
portBASE_TYPE xThreadState; portBASE_TYPE xThreadState;
volatile enum thread_status status; volatile enum thread_status status;
unsigned portBASE_TYPE uxCriticalNesting; unsigned portBASE_TYPE uxCriticalNesting;
char * name;
} xThreadState; } xThreadState;
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
@ -171,7 +172,7 @@ static volatile unsigned portBASE_TYPE uxCriticalNesting;
* when then goes into it's signal handler. It will changes it's running * when then goes into it's signal handler. It will changes it's running
* status to stopped which this thread will wait for. * status to stopped which this thread will wait for.
*/ */
static void pauseOtherThread(xTaskHandle hTask); static int pauseOtherThread(xTaskHandle hTask);
/** /**
* Prepares a task to pause (stores critical nesting, releases running semaphore) * Prepares a task to pause (stores critical nesting, releases running semaphore)
@ -215,6 +216,7 @@ static pthread_cond_t * prvGetConditionHandle( xTaskHandle hTask );
static pthread_mutex_t * prvGetMutexHandle( xTaskHandle hTask ); static pthread_mutex_t * prvGetMutexHandle( xTaskHandle hTask );
static xTaskHandle prvGetTaskHandle( pthread_t hThread ); static xTaskHandle prvGetTaskHandle( pthread_t hThread );
static void prvSetThreadStatus( pthread_t hThread, enum thread_status status ); static void prvSetThreadStatus( pthread_t hThread, enum thread_status status );
static enum thread_status prvGetThreadStatus( pthread_t hThread );
static portLONG prvGetFreeThreadState( void ); static portLONG prvGetFreeThreadState( void );
static void prvSetTaskCriticalNesting( pthread_t xThreadId, unsigned portBASE_TYPE uxNesting ); static void prvSetTaskCriticalNesting( pthread_t xThreadId, unsigned portBASE_TYPE uxNesting );
static unsigned portBASE_TYPE prvGetTaskCriticalNesting( pthread_t xThreadId ); static unsigned portBASE_TYPE prvGetTaskCriticalNesting( pthread_t xThreadId );
@ -444,10 +446,10 @@ portLONG lIndex;
while( pdTRUE != xSchedulerEnd ) { while( pdTRUE != xSchedulerEnd ) {
usleep(portTICK_RATE_MICROSECONDS); usleep(portTICK_RATE_MICROSECONDS);
vPortSystemTickHandler(SIG_TICK); vPortSystemTickHandler(SIG_TICK);
i++;
if (i % 1000 == 0) if (i % 1000 == 0)
fprintf(stderr,"."); fprintf(stderr,".");
i++; //if (i % 200 == 0)
//if (i % 2000 == 0)
// printTasks(); // printTasks();
} }
@ -520,6 +522,7 @@ void vPortYield( void )
if(pthread_mutex_trylock( &xSwappingThreadMutex )== EBUSY) { if(pthread_mutex_trylock( &xSwappingThreadMutex )== EBUSY) {
// The tick handler will just pause this thread // The tick handler will just pause this thread
fprintf(stdout, "Could not get the swapping thread to yield\r\n");
usleep(10); usleep(10);
storeSelf(); storeSelf();
pauseSelf(); pauseSelf();
@ -530,8 +533,8 @@ void vPortYield( void )
// Time to sleep // Time to sleep
//fprintf(stdout, "Yielding from %s\r\n", threadToName(pthread_self())); //fprintf(stdout, "Yielding from %s\r\n", threadToName(pthread_self()));
storeSelf(); storeSelf();
pthread_mutex_unlock( &xSwappingThreadMutex );
resumeThread(xTaskGetCurrentTaskHandle()); resumeThread(xTaskGetCurrentTaskHandle());
pthread_mutex_unlock( &xSwappingThreadMutex );
pauseSelf(); pauseSelf();
claimRunningSemaphore(3); claimRunningSemaphore(3);
} else } else
@ -621,7 +624,7 @@ void vPortSystemTickHandler( int sig )
} }
*/ */
if(pthread_mutex_trylock( &xSwappingThreadMutex )== EBUSY) { if(pthread_mutex_trylock( &xSwappingThreadMutex )== EBUSY) {
fprintf(stdout,"Can't get swapping lock for tick handler\r\n"); //fprintf(stdout,"Can't get swapping lock for tick handler\r\n");
return; return;
} }
@ -632,11 +635,13 @@ void vPortSystemTickHandler( int sig )
//printTasks(); //printTasks();
pauseOtherThread(xTaskGetCurrentTaskHandle()); // If that thread won't pause something is going on
if (pauseOtherThread(xTaskGetCurrentTaskHandle()) != -1) {
claimRunningSemaphore(2); claimRunningSemaphore(2);
vTaskSwitchContext(); vTaskSwitchContext();
releaseRunningSemaphore(); releaseRunningSemaphore();
resumeThread(xTaskGetCurrentTaskHandle()); resumeThread(xTaskGetCurrentTaskHandle());
}
// fprintf(stdout, "System tick done\r\n"); // fprintf(stdout, "System tick done\r\n");
} }
#endif #endif
@ -738,6 +743,13 @@ void * pParams = pxParams->pvParams;
usleep(1000); usleep(1000);
xTaskHandle hTask = prvGetTaskHandle( pthread_self() );
portLONG lIndex;
for ( lIndex = 0; lIndex < MAX_NUMBER_OF_TASKS && pxThreads[ lIndex ].hTask != hTask; lIndex++ );
assert(pxThreads[ lIndex ].hTask == hTask);
pxThreads[lIndex].name = (char *) ((tskTCB *) hTask)->pcTaskName;
pauseSelf(); pauseSelf();
sigemptyset( &xSignals ); sigemptyset( &xSignals );
@ -757,10 +769,16 @@ extern volatile unsigned portBASE_TYPE uxSchedulerSuspended;
void prvSuspendSignalHandler(int sig) void prvSuspendSignalHandler(int sig)
{ {
if (prvGetThreadStatus(pthread_self()) != RUNNING) {
//fprintf(stderr, "Caught erroneous suspend signal (%d): %s\r\n", sig, threadToName(pthread_self()));
return;
}
else {
//fprintf(stderr, "Caught suspend signal (%d): %s\r\n", sig, threadToName(pthread_self())); //fprintf(stderr, "Caught suspend signal (%d): %s\r\n", sig, threadToName(pthread_self()));
storeSelf(); storeSelf();
pauseSelf(); pauseSelf();
claimRunningSemaphore(1); claimRunningSemaphore(1);
}
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
@ -852,6 +870,23 @@ static void prvSetThreadStatus( pthread_t hThread, enum thread_status status )
} }
} }
/*-----------------------------------------------------------*/
static enum thread_status prvGetThreadStatus( pthread_t hThread )
{
portLONG lIndex;
/* If not initialized yet */
if( pxThreads == NULL ) return DESTROYED;
for ( lIndex = 0; lIndex < MAX_NUMBER_OF_TASKS; lIndex++ )
{
if ( pxThreads[ lIndex ].hThread == hThread )
{
return pxThreads[ lIndex ].status; }
}
return DESTROYED;
}
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
pthread_cond_t * prvGetConditionHandle( xTaskHandle hTask ) pthread_cond_t * prvGetConditionHandle( xTaskHandle hTask )
{ {
@ -1073,7 +1108,7 @@ void pauseSelf()
assert( xResult != EINVAL ); assert( xResult != EINVAL );
if (xResult == ETIMEDOUT && pthread_self() == prvGetThreadHandle(xTaskGetCurrentTaskHandle())) { if (xResult == ETIMEDOUT && pthread_self() == prvGetThreadHandle(xTaskGetCurrentTaskHandle())) {
fprintf(stdout,"Timed out should be running\r\n"); fprintf(stdout,"Timed out should be running %s\r\n", threadToName( pthread_self() ));
break; break;
} }
} }
@ -1100,7 +1135,7 @@ void pauseSelf()
* when then goes into it's signal handler. It will changes it's running * when then goes into it's signal handler. It will changes it's running
* status to stopped which this thread will wait for. * status to stopped which this thread will wait for.
*/ */
static void pauseOtherThread(xTaskHandle hTask) static int pauseOtherThread(xTaskHandle hTask)
{ {
const int MAX_TIME = 10000; // us const int MAX_TIME = 10000; // us
const int MAX_ATTEMPTS = 5; const int MAX_ATTEMPTS = 5;
@ -1109,10 +1144,16 @@ static void pauseOtherThread(xTaskHandle hTask)
for ( lIndex = 0; lIndex < MAX_NUMBER_OF_TASKS && pxThreads[ lIndex ].hTask != hTask; lIndex++ ); for ( lIndex = 0; lIndex < MAX_NUMBER_OF_TASKS && pxThreads[ lIndex ].hTask != hTask; lIndex++ );
assert(pxThreads[ lIndex ].hTask == hTask); assert(pxThreads[ lIndex ].hTask == hTask);
if(pxThreads[ lIndex ].status != RUNNING) {
//fprintf(stdout, "Attempted to stop thread that is not running %s by %s\r\n", threadToName(pxThreads[lIndex].hThread), threadToName(pthread_self()));
return -1;
}
for (int i = 0; i < MAX_ATTEMPTS; i++) { for (int i = 0; i < MAX_ATTEMPTS; i++) {
// Trigger signal handler which will call pauseSelf // Trigger signal handler which will call pauseSelf
pthread_t thread_to_supend = prvGetThreadHandle( hTask ); pthread_t thread_to_supend = prvGetThreadHandle( hTask );
//fprintf(stdout, "Requesting pause of thread %s from %s.\r\n", threadToName(thread_to_supend), threadToName( pthread_self() ) ); fprintf(stdout, "Requesting pause of thread %s from %s.\r\n", threadToName(thread_to_supend), threadToName( pthread_self() ) );
assert( pthread_kill( thread_to_supend, SIG_SUSPEND ) == 0); assert( pthread_kill( thread_to_supend, SIG_SUSPEND ) == 0);
@ -1120,13 +1161,15 @@ static void pauseOtherThread(xTaskHandle hTask)
while( (clock() - start_time) < MAX_TIME ) { while( (clock() - start_time) < MAX_TIME ) {
if(pxThreads[ lIndex ].status == STOPPED || pxThreads[ lIndex ].status == STORED) { if(pxThreads[ lIndex ].status == STOPPED || pxThreads[ lIndex ].status == STORED) {
//fprintf(stdout, "Pause detected of %s by %s\r\n", threadToName(pxThreads[lIndex].hThread), threadToName(pthread_self())); //fprintf(stdout, "Pause detected of %s by %s\r\n", threadToName(pxThreads[lIndex].hThread), threadToName(pthread_self()));
return; return 0;
} }
} }
//fprintf(stdout, "Sending pause signal from %s to %s: Try %d\r\n", threadToName(pthread_self()), threadToName(prvGetThreadHandle( hTask )), i); //fprintf(stdout, "Sending pause signal from %s to %s: Try %d\r\n", threadToName(pthread_self()), threadToName(prvGetThreadHandle( hTask )), i);
} }
assert(0); assert(0);
return -2;
} }
/** /**
@ -1137,7 +1180,7 @@ static void pauseOtherThread(xTaskHandle hTask)
static void resumeThread(xTaskHandle hTask) static void resumeThread(xTaskHandle hTask)
{ {
const unsigned int MAX_TIME = 10000; // us const unsigned int MAX_TIME = 10000; // us
const int MAX_ATTEMPTS = 25; const int MAX_ATTEMPTS = 250;
portLONG lIndex; portLONG lIndex;
for ( lIndex = 0; lIndex < MAX_NUMBER_OF_TASKS && pxThreads[ lIndex ].hTask != hTask; lIndex++ ); for ( lIndex = 0; lIndex < MAX_NUMBER_OF_TASKS && pxThreads[ lIndex ].hTask != hTask; lIndex++ );
@ -1161,6 +1204,7 @@ static void resumeThread(xTaskHandle hTask)
//fprintf(stdout, "Sending resume signal from %s to %s (%d): Try %d\r\n", threadToName(pthread_self()), threadToName(hTask), lIndex, i); //fprintf(stdout, "Sending resume signal from %s to %s (%d): Try %d\r\n", threadToName(pthread_self()), threadToName(hTask), lIndex, i);
} }
fprintf(stdout, "Railed to resume from %s to %s (%d)\r\n", threadToName(pthread_self()), threadToName(hTask), lIndex);
// Thread resumption timed out // Thread resumption timed out
assert(0); assert(0);
} }
@ -1186,8 +1230,8 @@ static void claimRunningSemaphore(int source)
*/ */
static void releaseRunningSemaphore() static void releaseRunningSemaphore()
{ {
//fprintf(stderr,"Released the semaphore %s\r\n", threadToName(pthread_self()));
assert( 0 == pthread_mutex_unlock( &xRunningThread ) ); assert( 0 == pthread_mutex_unlock( &xRunningThread ) );
//fprintf(stderr,"Released the semaphore %s\r\n", threadToName(pthread_self()));
} }

View File

@ -39,7 +39,7 @@ static uint8_t LED_GPIO[PIOS_LED_NUM];
static inline void PIOS_SetLED(uint32_t LED,uint8_t stat) { static inline void PIOS_SetLED(uint32_t LED,uint8_t stat) {
// printf("PIOS: LED %i status %i\n",LED,stat); //printf("PIOS: LED %i status %i\n",LED,stat);
LED_GPIO[LED]=stat; LED_GPIO[LED]=stat;
} }

View File

@ -108,16 +108,13 @@ void * PIOS_TCP_RxThread(void *tcp_dev_n)
(void) (tcp_dev->rx_in_cb)(tcp_dev->rx_in_context, &tcp_dev->rx_buffer[0], received, NULL, &rx_need_yield); (void) (tcp_dev->rx_in_cb)(tcp_dev->rx_in_context, &tcp_dev->rx_buffer[0], received, NULL, &rx_need_yield);
} }
#if 0 //fprintf(stderr, "Received %d\n", received);
if (rx_need_yield) {
fprintf(stderr, "Not sure about this ... \n");
}
#if defined(PIOS_INCLUDE_FREERTOS) #if defined(PIOS_INCLUDE_FREERTOS)
// Not sure about this
if (rx_need_yield) { if (rx_need_yield) {
vPortYieldFromISR(); vPortYieldFromISR();
} }
#endif /* PIOS_INCLUDE_FREERTOS */ #endif /* PIOS_INCLUDE_FREERTOS */
#endif
} while(received > 0); } while(received > 0);
if (-1 == shutdown(tcp_dev->socket_connection, SHUT_RDWR)) if (-1 == shutdown(tcp_dev->socket_connection, SHUT_RDWR))