This is the mail archive of the
ecos-discuss@sources.redhat.com
mailing list for the eCos project.
Re: portability problem in pthread_create()
- From: Bart Veer <bartv at redhat dot com>
- To: lars dot viklund at axis dot com
- Cc: ecos-discuss at sources dot redhat dot com
- Date: Tue, 5 Mar 2002 16:30:45 GMT
- Subject: Re: [ECOS] portability problem in pthread_create()
- References: <3C6BEE8B5E1BAC42905A93F13004E8ABB3F48D@mailse01.axis.se>
- Reply-to: bartv at redhat dot com
>>>>> "Lars" == Lars Viklund <lars.viklund@axis.com> writes:
Lars> pthread_create() puts a pthread_info struct on the stack. If
Lars> (sizeof(pthread_info) % sizeof(CYG_WORD)) != 0 this will
Lars> make the stack unaligned which in turn will cause the assert
Lars> in Cyg_HardwareThread::attach_stack() to fail.
Lars> Suggested fix:
Lars> Index: packages/compat/posix/current/src/pthread.cxx
Lars> ===================================================================
Lars> RCS file: /n/cvsroot/os/ecos/packages/compat/posix/current/src/pthread.cxx,v
Lars> retrieving revision 1.1.1.1
Lars> retrieving revision 1.2
Lars> diff -u -r1.1.1.1 -r1.2
Lars> --- packages/compat/posix/current/src/pthread.cxx 12 Dec 2001 11:55:44 -0000 1.1.1.1
Lars> +++ packages/compat/posix/current/src/pthread.cxx 4 Mar 2002 17:36:59 -0000 1.2
Lars> @@ -536,8 +536,9 @@
Lars> nthread = (pthread_info *)stackbase;
Lars> - stackbase += sizeof(pthread_info);
Lars> - stacksize -= sizeof(pthread_info);
Lars> + // Round size of pthread_info upward to sizeof(CYG_WORD) to keep stack aligned
Lars> + stackbase += (sizeof(pthread_info)+sizeof(CYG_WORD)-1) & ~(sizeof(CYG_WORD)-1);
Lars> + stacksize -= (sizeof(pthread_info)+sizeof(CYG_WORD)-1) & ~(sizeof(CYG_WORD)-1);
Lars> thread_table[thread_next] = nthread;
Lars> Alternatively, fix the stack alignment assert, e.g. by
Lars> introducing a separate platform defined macro for stack
Lars> alignment instead of assuming that CYG_WORD alignment is
Lars> required.
I am not convinced this is a real problem. The last field in the
pthread_info structure is a pointer and the compiler is not allowed to
rearrange fields within a structure, so the probability that
(0 != (sizeof(pthread_info) % sizeof(CYG_WORD))) is low. This
assumption could fail on strange architectures or when building eCos
with the wrong compiler flags, but I suspect an awful lot of other
code would fail as well
Also the pthread_info structure is placed at the bottom (lowest memory
location) of the stack, so for a typical descending stack it does not
really matter whether or not the stacksize is word-aligned. Instead
what really matters is that stackbase+stacksize remains word-aligned.
I suspect the stackbase assertion in
Cyg_HardwareThread::attach_stack() is there to cover potential porting
problems to hardware with ascending stacks.
Have you seen this assertion trigger under normal circumstances?
Bart
--
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss