From d143dde4381aa8a80b079f5f4fc8151ac9dd0abb Mon Sep 17 00:00:00 2001 From: Daniel Eischen Date: Wed, 30 Apr 2003 15:05:17 +0000 Subject: [PATCH] Move the mailbox to the beginning of the thread and align the thread so that the context (SSE FPU state) is also aligned. --- lib/libkse/arch/i386/include/pthread_md.h | 3 +++ lib/libkse/thread/thr_create.c | 3 +++ lib/libkse/thread/thr_init.c | 3 +++ lib/libkse/thread/thr_kern.c | 12 +++++++++--- lib/libkse/thread/thr_private.h | 10 ++++++---- lib/libpthread/arch/i386/include/pthread_md.h | 3 +++ lib/libpthread/thread/thr_create.c | 3 +++ lib/libpthread/thread/thr_init.c | 3 +++ lib/libpthread/thread/thr_kern.c | 12 +++++++++--- lib/libpthread/thread/thr_private.h | 10 ++++++---- 10 files changed, 48 insertions(+), 14 deletions(-) diff --git a/lib/libkse/arch/i386/include/pthread_md.h b/lib/libkse/arch/i386/include/pthread_md.h index 2611101a8cc5..cb5a3443a345 100644 --- a/lib/libkse/arch/i386/include/pthread_md.h +++ b/lib/libkse/arch/i386/include/pthread_md.h @@ -48,4 +48,7 @@ extern int _thr_getcontext(ucontext_t *); #define THR_GETCONTEXT(ucp) _thr_getcontext(ucp) #define THR_SETCONTEXT(ucp) _thr_setcontext(ucp) + +#define THR_ALIGNBYTES 15 +#define THR_ALIGN(td) (((unsigned)(td) + THR_ALIGNBYTES) & ~THR_ALIGNBYTES) #endif diff --git a/lib/libkse/thread/thr_create.c b/lib/libkse/thread/thr_create.c index 4c65d3c18e1a..0c9edbdccb42 100644 --- a/lib/libkse/thread/thr_create.c +++ b/lib/libkse/thread/thr_create.c @@ -97,6 +97,7 @@ _pthread_create(pthread_t * thread, const pthread_attr_t * attr, struct pthread *curthread, *new_thread; struct kse *kse = NULL; struct kse_group *kseg = NULL; + void *p; kse_critical_t crit; int i; int ret = 0; @@ -123,7 +124,9 @@ _pthread_create(pthread_t * thread, const pthread_attr_t * attr, ret = EAGAIN; } else { /* Initialize the thread structure: */ + p = new_thread->alloc_addr; memset(new_thread, 0, sizeof(struct pthread)); + new_thread->alloc_addr = p; /* Check if default thread attributes are required: */ if (attr == NULL || *attr == NULL) diff --git a/lib/libkse/thread/thr_init.c b/lib/libkse/thread/thr_init.c index d43adf56bb12..f06df6c749b5 100644 --- a/lib/libkse/thread/thr_init.c +++ b/lib/libkse/thread/thr_init.c @@ -311,10 +311,13 @@ _libpthread_init(struct pthread *curthread) static void init_main_thread(struct pthread *thread) { + void *p; int i; /* Zero the initial thread structure. */ + p = thread->alloc_addr; memset(thread, 0, sizeof(struct pthread)); + thread->alloc_addr = p; /* Setup the thread attributes. */ thread->attr = _pthread_attr_default; diff --git a/lib/libkse/thread/thr_kern.c b/lib/libkse/thread/thr_kern.c index a153a4d7e737..54d9dd778b09 100644 --- a/lib/libkse/thread/thr_kern.c +++ b/lib/libkse/thread/thr_kern.c @@ -2019,6 +2019,7 @@ struct pthread * _thr_alloc(struct pthread *curthread) { kse_critical_t crit; + void *p; struct pthread *thread = NULL; if (curthread != NULL) { @@ -2035,8 +2036,13 @@ _thr_alloc(struct pthread *curthread) _kse_critical_leave(crit); } } - if (thread == NULL) - thread = (struct pthread *)malloc(sizeof(struct pthread)); + if (thread == NULL) { + p = malloc(sizeof(struct pthread) + THR_ALIGNBYTES); + if (p != NULL) { + thread = (struct pthread *)THR_ALIGN(p); + thread->alloc_addr = p; + } + } return (thread); } @@ -2052,7 +2058,7 @@ _thr_free(struct pthread *curthread, struct pthread *thread) _lockuser_destroy(&thread->lockusers[i]); } _lock_destroy(&thread->lock); - free(thread); + free(thread->alloc_addr); } else { crit = _kse_critical_enter(); diff --git a/lib/libkse/thread/thr_private.h b/lib/libkse/thread/thr_private.h index 4d4324241fe4..22d2445d013f 100644 --- a/lib/libkse/thread/thr_private.h +++ b/lib/libkse/thread/thr_private.h @@ -592,6 +592,12 @@ struct pthread_specific_elem { * Thread structure. */ struct pthread { + /* + * Thread mailbox is first so it cal be aligned properly. + */ + struct kse_thr_mailbox tmbx; + void *alloc_addr; /* real address (unaligned) */ + /* * Magic value to help recognize a valid thread structure * from an invalid one: @@ -626,10 +632,6 @@ struct pthread { void *arg; struct pthread_attr attr; - /* - * Thread mailbox. - */ - struct kse_thr_mailbox tmbx; int active; /* thread running */ int blocked; /* thread blocked in kernel */ int need_switchout; diff --git a/lib/libpthread/arch/i386/include/pthread_md.h b/lib/libpthread/arch/i386/include/pthread_md.h index 2611101a8cc5..cb5a3443a345 100644 --- a/lib/libpthread/arch/i386/include/pthread_md.h +++ b/lib/libpthread/arch/i386/include/pthread_md.h @@ -48,4 +48,7 @@ extern int _thr_getcontext(ucontext_t *); #define THR_GETCONTEXT(ucp) _thr_getcontext(ucp) #define THR_SETCONTEXT(ucp) _thr_setcontext(ucp) + +#define THR_ALIGNBYTES 15 +#define THR_ALIGN(td) (((unsigned)(td) + THR_ALIGNBYTES) & ~THR_ALIGNBYTES) #endif diff --git a/lib/libpthread/thread/thr_create.c b/lib/libpthread/thread/thr_create.c index 4c65d3c18e1a..0c9edbdccb42 100644 --- a/lib/libpthread/thread/thr_create.c +++ b/lib/libpthread/thread/thr_create.c @@ -97,6 +97,7 @@ _pthread_create(pthread_t * thread, const pthread_attr_t * attr, struct pthread *curthread, *new_thread; struct kse *kse = NULL; struct kse_group *kseg = NULL; + void *p; kse_critical_t crit; int i; int ret = 0; @@ -123,7 +124,9 @@ _pthread_create(pthread_t * thread, const pthread_attr_t * attr, ret = EAGAIN; } else { /* Initialize the thread structure: */ + p = new_thread->alloc_addr; memset(new_thread, 0, sizeof(struct pthread)); + new_thread->alloc_addr = p; /* Check if default thread attributes are required: */ if (attr == NULL || *attr == NULL) diff --git a/lib/libpthread/thread/thr_init.c b/lib/libpthread/thread/thr_init.c index d43adf56bb12..f06df6c749b5 100644 --- a/lib/libpthread/thread/thr_init.c +++ b/lib/libpthread/thread/thr_init.c @@ -311,10 +311,13 @@ _libpthread_init(struct pthread *curthread) static void init_main_thread(struct pthread *thread) { + void *p; int i; /* Zero the initial thread structure. */ + p = thread->alloc_addr; memset(thread, 0, sizeof(struct pthread)); + thread->alloc_addr = p; /* Setup the thread attributes. */ thread->attr = _pthread_attr_default; diff --git a/lib/libpthread/thread/thr_kern.c b/lib/libpthread/thread/thr_kern.c index a153a4d7e737..54d9dd778b09 100644 --- a/lib/libpthread/thread/thr_kern.c +++ b/lib/libpthread/thread/thr_kern.c @@ -2019,6 +2019,7 @@ struct pthread * _thr_alloc(struct pthread *curthread) { kse_critical_t crit; + void *p; struct pthread *thread = NULL; if (curthread != NULL) { @@ -2035,8 +2036,13 @@ _thr_alloc(struct pthread *curthread) _kse_critical_leave(crit); } } - if (thread == NULL) - thread = (struct pthread *)malloc(sizeof(struct pthread)); + if (thread == NULL) { + p = malloc(sizeof(struct pthread) + THR_ALIGNBYTES); + if (p != NULL) { + thread = (struct pthread *)THR_ALIGN(p); + thread->alloc_addr = p; + } + } return (thread); } @@ -2052,7 +2058,7 @@ _thr_free(struct pthread *curthread, struct pthread *thread) _lockuser_destroy(&thread->lockusers[i]); } _lock_destroy(&thread->lock); - free(thread); + free(thread->alloc_addr); } else { crit = _kse_critical_enter(); diff --git a/lib/libpthread/thread/thr_private.h b/lib/libpthread/thread/thr_private.h index 4d4324241fe4..22d2445d013f 100644 --- a/lib/libpthread/thread/thr_private.h +++ b/lib/libpthread/thread/thr_private.h @@ -592,6 +592,12 @@ struct pthread_specific_elem { * Thread structure. */ struct pthread { + /* + * Thread mailbox is first so it cal be aligned properly. + */ + struct kse_thr_mailbox tmbx; + void *alloc_addr; /* real address (unaligned) */ + /* * Magic value to help recognize a valid thread structure * from an invalid one: @@ -626,10 +632,6 @@ struct pthread { void *arg; struct pthread_attr attr; - /* - * Thread mailbox. - */ - struct kse_thr_mailbox tmbx; int active; /* thread running */ int blocked; /* thread blocked in kernel */ int need_switchout;