From cedfd802efaf0150cace14ce960f17aaf7d0c486 Mon Sep 17 00:00:00 2001 From: Joel Sherrill Date: Thu, 30 Jul 2009 00:14:26 +0000 Subject: 2009-07-29 Joel Sherrill * score/src/threadinitialize.c: Rework so there is only one error exit path. This required setting every variable that contains memory allocated from the workspace to NULL early and using that assumption in the one failed exit path. --- cpukit/score/src/threadinitialize.c | 117 ++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 66 deletions(-) (limited to 'cpukit/score/src/threadinitialize.c') diff --git a/cpukit/score/src/threadinitialize.c b/cpukit/score/src/threadinitialize.c index 2af727af7c..f4b9660526 100644 --- a/cpukit/score/src/threadinitialize.c +++ b/cpukit/score/src/threadinitialize.c @@ -57,29 +57,38 @@ bool _Thread_Initialize( { size_t actual_stack_size = 0; void *stack = NULL; -#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) - void *fp_area; -#endif + #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) + void *fp_area; + #endif void *extensions_area; bool extension_status; + int i; -#if __RTEMS_ADA__ /* * Initialize the Ada self pointer */ - - the_thread->rtems_ada_self = NULL; -#endif + #if __RTEMS_ADA__ + the_thread->rtems_ada_self = NULL; + #endif /* - * Allocate and Initialize the stack for this thread. + * Zero out all the allocated memory fields */ + for ( i=0 ; i <= THREAD_API_LAST ; i++ ) + the_thread->API_Extensions[i] = NULL; + extensions_area = NULL; + the_thread->libc_reent = NULL; - if ( !stack_area ) { + #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) + fp_area = NULL; + #endif + /* + * Allocate and Initialize the stack for this thread. + */ + if ( !stack_area ) { actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size ); - if ( !actual_stack_size || actual_stack_size < stack_size ) return false; /* stack allocation failed */ @@ -100,62 +109,37 @@ bool _Thread_Initialize( /* * Allocate the floating point area for this thread */ - -#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) - if ( is_fp ) { - - fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE ); - if ( !fp_area ) { - _Thread_Stack_Free( the_thread ); - return false; + #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) + if ( is_fp ) { + fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE ); + if ( !fp_area ) + goto failed; + fp_area = _Context_Fp_start( fp_area, 0 ); } - fp_area = _Context_Fp_start( fp_area, 0 ); - - } else - fp_area = NULL; - - the_thread->fp_context = fp_area; - the_thread->Start.fp_context = fp_area; -#endif + the_thread->fp_context = fp_area; + the_thread->Start.fp_context = fp_area; + #endif /* * Initialize the thread timer */ _Watchdog_Initialize( &the_thread->Timer, NULL, 0, NULL ); -#ifdef __RTEMS_STRICT_ORDER_MUTEX__ - /*Initialize the head of chain of mutex */ - _Chain_Initialize_empty(&the_thread->lock_mutex); -#endif - - /* - * Clear the libc reent hook. - */ - - the_thread->libc_reent = NULL; + #ifdef __RTEMS_STRICT_ORDER_MUTEX__ + /* Initialize the head of chain of held mutexes */ + _Chain_Initialize_empty(&the_thread->lock_mutex); + #endif /* * Allocate the extensions area for this thread */ - if ( _Thread_Maximum_extensions ) { extensions_area = _Workspace_Allocate( (_Thread_Maximum_extensions + 1) * sizeof( void * ) ); - - if ( !extensions_area ) { -#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) - if ( fp_area ) - (void) _Workspace_Free( fp_area ); -#endif - - _Thread_Stack_Free( the_thread ); - - return false; - } - } else - extensions_area = NULL; - + if ( !extensions_area ) + goto failed; + } the_thread->extensions = (void **) extensions_area; /* @@ -165,10 +149,8 @@ bool _Thread_Initialize( * so they cannot rely on the thread create user extension * call. */ - if ( the_thread->extensions ) { - uint32_t i; - for ( i = 0; i < (_Thread_Maximum_extensions + 1); i++ ) + for ( i = 0; i <= _Thread_Maximum_extensions ; i++ ) the_thread->extensions[i] = NULL; } @@ -206,7 +188,6 @@ bool _Thread_Initialize( /* * Initialize the CPU usage statistics */ - #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS _Timestamp_Set_to_zero( &the_thread->cpu_time_used ); #else @@ -216,7 +197,6 @@ bool _Thread_Initialize( /* * Open the object */ - _Objects_Open( information, &the_thread->Object, name ); /* @@ -227,22 +207,27 @@ bool _Thread_Initialize( * run safely. */ extension_status = _User_extensions_Thread_create( the_thread ); + if ( extension_status ) + return true; - if ( !extension_status ) { +failed: + if ( the_thread->libc_reent ) + _Workspace_Free( the_thread->libc_reent ); - if ( extensions_area ) - (void) _Workspace_Free( extensions_area ); + for ( i=0 ; i <= THREAD_API_LAST ; i++ ) + if ( the_thread->API_Extensions[i] ) + _Workspace_Free( the_thread->API_Extensions[i] ); + + if ( extensions_area ) + (void) _Workspace_Free( extensions_area ); -#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) + #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) if ( fp_area ) (void) _Workspace_Free( fp_area ); -#endif - - _Thread_Stack_Free( the_thread ); + #endif - return false; - } + _Thread_Stack_Free( the_thread ); + return false; - return true; } -- cgit v1.2.3