From e19a872c2b378458e72cab6c5ac30b12efad13f0 Mon Sep 17 00:00:00 2001 From: Geoffrey Challen Date: Sat, 5 Mar 2016 08:46:00 -0500 Subject: [PATCH] Static thread names. --- kern/include/thread.h | 12 +++++++++++- kern/test/threadlisttest.c | 6 +----- kern/thread/thread.c | 12 +++++------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/kern/include/thread.h b/kern/include/thread.h index 48ca76d..42d56b4 100644 --- a/kern/include/thread.h +++ b/kern/include/thread.h @@ -48,6 +48,7 @@ struct cpu; /* Size of kernel stacks; must be power of 2 */ #define STACK_SIZE 4096 +#define MAX_NAME_LENGTH 64 /* Mask for extracting the stack base address of a kernel stack pointer */ #define STACK_MASK (~(vaddr_t)(STACK_SIZE-1)) @@ -70,7 +71,16 @@ struct thread { * These go up front so they're easy to get to even if the * debugger is messed up. */ - char *t_name; /* Name of this thread */ + + /* + * Name of this thread. Used to be dynamically allocated using kmalloc, but + * this can cause small changes in the amount of available memory due to the + * fact that it was cleaned up in exorcise. This produces more predictable + * behavior at the cost of a small amount of memory overhead and the + * inability to give threads huge names. + */ + + char t_name[MAX_NAME_LENGTH]; const char *t_wchan_name; /* Name of wait channel, if sleeping */ threadstate_t t_state; /* State this thread is in */ diff --git a/kern/test/threadlisttest.c b/kern/test/threadlisttest.c index 993ee01..dd96bd5 100644 --- a/kern/test/threadlisttest.c +++ b/kern/test/threadlisttest.c @@ -66,10 +66,7 @@ fakethread_create(const char *name) } /* ignore most of the fields, zero everything for tidiness */ bzero(t, sizeof(*t)); - t->t_name = kstrdup(name); - if (t->t_name == NULL) { - panic("threadlisttest: Out of memory\n"); - } + strcpy(t->t_name, name); t->t_stack = FAKE_MAGIC; threadlistnode_init(&t->t_listnode, t); return t; @@ -84,7 +81,6 @@ fakethread_destroy(struct thread *t) { KASSERT(t->t_stack == FAKE_MAGIC); threadlistnode_cleanup(&t->t_listnode); - kfree(t->t_name); kfree(t); } diff --git a/kern/thread/thread.c b/kern/thread/thread.c index 13d8ee0..d21388f 100644 --- a/kern/thread/thread.c +++ b/kern/thread/thread.c @@ -119,17 +119,16 @@ thread_create(const char *name) struct thread *thread; DEBUGASSERT(name != NULL); + if (strlen(name) > MAX_NAME_LENGTH) { + return NULL; + } thread = kmalloc(sizeof(*thread)); if (thread == NULL) { return NULL; } - thread->t_name = kstrdup(name); - if (thread->t_name == NULL) { - kfree(thread); - return NULL; - } + strcpy(thread->t_name, name); thread->t_wchan_name = "NEW"; thread->t_state = S_READY; @@ -280,7 +279,6 @@ thread_destroy(struct thread *thread) /* sheer paranoia */ thread->t_wchan_name = "DESTROYED"; - kfree(thread->t_name); kfree(thread); } @@ -795,7 +793,7 @@ thread_exit(void) thread_checkstack(cur); /* Interrupts off on this processor */ - splhigh(); + splhigh(); thread_switch(S_ZOMBIE, NULL, NULL); panic("braaaaaaaiiiiiiiiiiinssssss\n"); }