[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[committed mach 2/2] kern: reduce the size of `struct thread'
From: |
Justus Winter |
Subject: |
[committed mach 2/2] kern: reduce the size of `struct thread' |
Date: |
Fri, 20 Feb 2015 18:48:08 +0100 |
Reduce the size of `struct thread' by twelve bytes making it fit into
exactly five cache lines (on 32-bit platforms).
* kern/thread.h (struct thread): Group the state and all flags in a
bitfield.
(TH_EV_WAKE_ACTIVE, TH_EV_STATE): Provide macros that generate keys
for synchronization primitives like `thread_wakeup'.
* kern/thread.c (thread_halt, thread_dowait, thread_suspend): Use the
new keys instead of addresses of fields for the synchronisation.
* kern/ipc_sched.c (thread_handoff): Likewise.
* kern/sched_prim.c (thread_invoke, thread_dispatch): Likewise.
---
kern/ipc_sched.c | 2 +-
kern/sched_prim.c | 10 +++++-----
kern/thread.c | 12 ++++++------
kern/thread.h | 27 +++++++++++++++++++++++----
4 files changed, 35 insertions(+), 16 deletions(-)
diff --git a/kern/ipc_sched.c b/kern/ipc_sched.c
index cc1672d..be82971 100644
--- a/kern/ipc_sched.c
+++ b/kern/ipc_sched.c
@@ -268,7 +268,7 @@ thread_handoff(
*/
old->wake_active = FALSE;
thread_unlock(old);
- thread_wakeup((event_t)&old->wake_active);
+ thread_wakeup(TH_EV_WAKE_ACTIVE(old));
goto after_old_thread;
}
} else
diff --git a/kern/sched_prim.c b/kern/sched_prim.c
index 376217a..d7792ae 100644
--- a/kern/sched_prim.c
+++ b/kern/sched_prim.c
@@ -615,7 +615,7 @@ boolean_t thread_invoke(
thread_lock(new_thread);
new_thread->state &= ~TH_UNINT;
thread_unlock(new_thread);
- thread_wakeup(&new_thread->state);
+ thread_wakeup(TH_EV_STATE(new_thread));
if (continuation != (void (*)()) 0) {
(void) spl0();
@@ -637,7 +637,7 @@ boolean_t thread_invoke(
new_thread->state &= ~(TH_SWAPPED | TH_UNINT);
thread_unlock(new_thread);
- thread_wakeup(&new_thread->state);
+ thread_wakeup(TH_EV_STATE(new_thread));
#if NCPUS > 1
new_thread->last_processor = current_processor();
@@ -676,7 +676,7 @@ boolean_t thread_invoke(
if (old_thread->wake_active) {
old_thread->wake_active = FALSE;
thread_unlock(old_thread);
-
thread_wakeup((event_t)&old_thread->wake_active);
+ thread_wakeup(TH_EV_WAKE_ACTIVE(old_thread));
goto after_old_thread;
}
@@ -767,7 +767,7 @@ boolean_t thread_invoke(
new_thread->state &= ~(TH_SWAPPED | TH_UNINT);
thread_unlock(new_thread);
- thread_wakeup(&new_thread->state);
+ thread_wakeup(TH_EV_STATE(new_thread));
/*
* Thread is now interruptible.
@@ -932,7 +932,7 @@ void thread_dispatch(
if (thread->wake_active) {
thread->wake_active = FALSE;
thread_unlock(thread);
- thread_wakeup((event_t)&thread->wake_active);
+ thread_wakeup(TH_EV_WAKE_ACTIVE(thread));
return;
}
break;
diff --git a/kern/thread.c b/kern/thread.c
index c627a06..5ffa844 100644
--- a/kern/thread.c
+++ b/kern/thread.c
@@ -968,7 +968,7 @@ kern_return_t thread_halt(
* operation can never cause a deadlock.)
*/
if (cur_thread->ast & AST_HALT) {
-
thread_wakeup_with_result((event_t)&cur_thread->wake_active,
+ thread_wakeup_with_result(TH_EV_WAKE_ACTIVE(cur_thread),
THREAD_INTERRUPTED);
thread_unlock(thread);
thread_unlock(cur_thread);
@@ -1006,7 +1006,7 @@ kern_return_t thread_halt(
*/
while ((thread->ast & AST_HALT) && (!(thread->state & TH_HALTED))) {
thread->wake_active = TRUE;
- thread_sleep((event_t) &thread->wake_active,
+ thread_sleep(TH_EV_WAKE_ACTIVE(thread),
simple_lock_addr(thread->lock), TRUE);
if (thread->state & TH_HALTED) {
@@ -1045,7 +1045,7 @@ kern_return_t thread_halt(
s = splsched();
thread_lock(thread);
thread_ast_clear(thread, AST_HALT);
- thread_wakeup_with_result((event_t)&thread->wake_active,
+ thread_wakeup_with_result(TH_EV_WAKE_ACTIVE(thread),
THREAD_INTERRUPTED);
thread_unlock(thread);
(void) splx(s);
@@ -1284,7 +1284,7 @@ thread_dowait(
* Check for failure if interrupted.
*/
thread->wake_active = TRUE;
- thread_sleep((event_t) &thread->wake_active,
+ thread_sleep(TH_EV_WAKE_ACTIVE(thread),
simple_lock_addr(thread->lock), TRUE);
thread_lock(thread);
if ((current_thread()->wait_result != THREAD_AWAKENED) &&
@@ -1308,7 +1308,7 @@ thread_dowait(
(void) splx(s);
if (need_wakeup)
- thread_wakeup((event_t) &thread->wake_active);
+ thread_wakeup(TH_EV_WAKE_ACTIVE(thread));
return ret;
}
@@ -1346,7 +1346,7 @@ kern_return_t thread_suspend(
thread_lock(thread);
/* Wait for thread to get interruptible */
while (thread->state & TH_UNINT) {
- assert_wait(&thread->state, TRUE);
+ assert_wait(TH_EV_STATE(thread), TRUE);
thread_unlock(thread);
thread_block(NULL);
thread_lock(thread);
diff --git a/kern/thread.h b/kern/thread.h
index d088c27..0e85d8c 100644
--- a/kern/thread.h
+++ b/kern/thread.h
@@ -70,6 +70,22 @@ struct thread {
task_t task; /* Task to which I belong */
queue_chain_t thread_list; /* list of threads in task */
+ /* Flags */
+ /* The flags are grouped here, but documented at the original
+ position. */
+ union {
+ struct {
+ unsigned state:16;
+ unsigned wake_active:1;
+ unsigned vm_privilege:1;
+ unsigned active:1;
+ };
+ event_t event_key;
+/* These keys can be used with thread_wakeup and friends. */
+#define TH_EV_WAKE_ACTIVE(t) ((event_t) (&(t)->event_key + 0))
+#define TH_EV_STATE(t) ((event_t) (&(t)->event_key + 1))
+ };
+
/* Thread bookkeeping */
queue_chain_t pset_threads; /* list of all threads in proc set*/
@@ -92,9 +108,10 @@ struct thread {
kern_return_t wait_result; /* outcome of wait -
may be examined by this thread
WITHOUT locking */
- boolean_t wake_active; /* someone is waiting for this
+ /* Defined above */
+ /* boolean_t wake_active; someone is waiting for this
thread to become suspended */
- int state; /* Thread state: */
+ /* int state; Thread state: */
/*
* Thread states [bits or'ed]
*/
@@ -129,7 +146,8 @@ struct thread {
/* VM global variables */
vm_offset_t recover; /* page fault recovery (copyin/out) */
- boolean_t vm_privilege; /* Can use reserved memory? */
+ /* Defined above */
+ /* boolean_t vm_privilege; Can use reserved memory? */
/* User-visible scheduling state */
int user_stop_count; /* outstanding stops */
@@ -194,7 +212,8 @@ struct thread {
timer_elt_data_t depress_timer; /* timer for priority depression */
/* Ast/Halt data structures */
- boolean_t active; /* how alive is the thread */
+ /* Defined above */
+ /* boolean_t active; how alive is the thread */
int ast; /* ast's needed. See ast.h */
/* Processor data structures */
--
2.1.4