diff --git a/sthreads/Makefile b/sthreads/Makefile index ce67212eca71a59e5243024a5affaa0e48769ee5..9b25ef984cfb826f31f14007e4cd92bf4944ac91 100644 --- a/sthreads/Makefile +++ b/sthreads/Makefile @@ -9,13 +9,8 @@ CFLAGS = -Wall -g -std=c99 -pedantic ASFLAGS = -g -all: test test_arg test_ret +all: test -test_arg: sthread.o queue.o glue.o test_arg.o - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ - -test_ret: sthread.o queue.o glue.o test_ret.o - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ # The simple test program test: sthread.o queue.o glue.o test.o @@ -24,7 +19,7 @@ test: sthread.o queue.o glue.o test.o # pseudo-target to clean up clean: - $(RM) -f *.o core* *~ test test_arg test_ret + $(RM) -f *.o core* *~ test .PHONY: all clean diff --git a/sthreads/glue.s b/sthreads/glue.s index 485480c7b8db9dfb83b2d4a022327aa5d62bf542..da4cd0094e07cf1cebb5eccac38b4e5289da3b55 100644 --- a/sthreads/glue.s +++ b/sthreads/glue.s @@ -28,24 +28,7 @@ scheduler_context: .quad 0 __sthread_switch: # Save the process state onto its stack - pushq %rax - pushq %rbx - pushq %rcx - pushq %rdx - pushq %rsi - pushq %rdi - pushq %rbp - - pushq %r8 - pushq %r9 - pushq %r10 - pushq %r11 - pushq %r12 - pushq %r13 - pushq %r14 - pushq %r15 - - pushf + # TODO # Call the high-level scheduler with the current context as an argument movq %rsp, %rdi @@ -56,27 +39,7 @@ __sthread_switch: # Restore the context to resume the thread. __sthread_restore: - # returned context is new stack - movq %rax, %rsp - - popf - - popq %r15 - popq %r14 - popq %r13 - popq %r12 - popq %r11 - popq %r10 - popq %r9 - popq %r8 - - popq %rbp - popq %rdi - popq %rsi - popq %rdx - popq %rcx - popq %rbx - popq %rax + # TODO ret @@ -99,29 +62,11 @@ __sthread_restore: .globl __sthread_initialize_context __sthread_initialize_context: - # %rdi - stackp, %rsi - f, %rdx - arg - # return value in %rax is the stack pointer, - # leave 144 bytes for thread context details and return address. - movq %rdi, %rax - subq $144, %rax - - leaq __sthread_finish(%rip), %rdi - movq %rdi, 136(%rax) # kill thread when done with f - movq %rsi, 128(%rax) # address of f to come back to - movq $0, 120(%rax) # rax = 0 - movq $0, 112(%rax) # rcx = 0 - movq $0, 96(%rax) # rdx = 0 - movq $0, 88(%rax) # rsi = 0 - movq %rdx, 80(%rax) # rdi = arg - movq $0, 72(%rax) # rbp = 0 - movq $0, 64(%rax) # r9 = 0 - movq $0, 48(%rax) # r10 = 0 - movq $0, 40(%rax) # r11 = 0 - movq $0, 32(%rax) # r12 = 0 - movq $0, 24(%rax) # r13 = 0 - movq $0, 16(%rax) # r14 = 0 - movq $0, 8(%rax) # r15 = 0 - movq $0, (%rax) # rflags = 0 + # TODO + + # TODO - Make sure you completely document every part of your + # thread context; what it is, and why you set each value + # to what you choose. ret diff --git a/sthreads/sthread.c b/sthreads/sthread.c index c20720ac7576c2f6a994f0a157c4754371f5f0a4..653257a779f20f71ce2fe0e5a26208772ebb7b2e 100644 --- a/sthreads/sthread.c +++ b/sthreads/sthread.c @@ -129,43 +129,8 @@ static void enqueue_thread(Thread *threadp) { */ ThreadContext *__sthread_scheduler(ThreadContext *context) { - if (context != NULL) { - /* Save the context argument into the current thread. */ - current->context = context; - /* Either queue up or deallocate the current thread, based on its state. */ - switch(current->state) { - case ThreadRunning: - current->state = ThreadReady; //change to ready and enqueue - enqueue_thread(current); - break; - case ThreadBlocked: - enqueue_thread(current); // enqueue to blocked queue - break; - case ThreadFinished: - __sthread_delete(current); // delete thread - break; - default: - fprintf(stderr, "Thread state has been corrupted: %d\n", - current->state); - exit(1); - } - } - - Thread *next = queue_take(&ready_queue); // get next ready thread - // if next exists set current to next and set it to running - if (next != NULL) { - current = next; - current->state = ThreadRunning; - } else { - // otherwise if no blocked threads done, if blocked threads deadlock - if (queue_empty(&blocked_queue)) { - printf("All threads completed successfully\n"); - exit(0); - } else { - fprintf(stderr, "System deadlock\n"); - exit(1); - } - } + /* TODO: Replace these lines with your implementation */ + /* TODO */ assert(0); /* TODO */ /* Return the next thread to resume executing. */ return current->context; @@ -191,32 +156,9 @@ void sthread_start(void) * structure, and it adds the thread to the Ready queue. */ Thread * sthread_create(void (*f)(void *arg), void *arg) { - // allocate stack space for thread - void *memory = (void *) malloc(DEFAULT_STACKSIZE); - if (memory == NULL) { - fprintf(stderr, "No memory to allocate thread stack\n"); - exit(1); - } - - // allocate thread struct - Thread *threadp = (Thread *) malloc(sizeof(Thread)); - if (threadp == NULL) { - fprintf(stderr, "No memory to allocate thread struct\n"); - free(memory); // no leaks - exit(1); - } - - // set state and memory - threadp->state = ThreadReady; - threadp->memory = memory; - - // stack pointer at start of region allocated for stack - void *stackp = (void *) ((char *) memory + DEFAULT_STACKSIZE); - threadp->context = __sthread_initialize_context(stackp, f, arg); - // enqueue to ready queue - enqueue_thread(threadp); - - return threadp; + /* TODO: Replace this function's body with your implementation */ + /* TODO */ assert(0); /* TODO */ + return NULL; } @@ -241,10 +183,8 @@ void __sthread_finish(void) { * context, as well as the memory for the Thread struct. */ void __sthread_delete(Thread *threadp) { - // free memory for context - free(threadp->memory); - // free thread struct - free(threadp); + /* TODO: Replace this function's body with your implementation */ + /* TODO */ assert(0); /* TODO */ } diff --git a/sthreads/test_arg.c b/sthreads/test_arg.c deleted file mode 100644 index f065cba3ae250c0c66329882d99813017ce1d52d..0000000000000000000000000000000000000000 --- a/sthreads/test_arg.c +++ /dev/null @@ -1,14 +0,0 @@ -#include <stdio.h> -#include <stdint.h> -#include "sthread.h" - -/* thread prints int arg */ -static void test(void *arg) { - printf("%zd\n", (intptr_t) arg); -} - -int main(int argc, char **argv) { - sthread_create(test, (void *) 3); - sthread_start(); - return 0; -} \ No newline at end of file diff --git a/sthreads/test_ret.c b/sthreads/test_ret.c deleted file mode 100644 index 2994798efe86726216ad76f7d549fc169b97ebb9..0000000000000000000000000000000000000000 --- a/sthreads/test_ret.c +++ /dev/null @@ -1,22 +0,0 @@ -#include <stdio.h> -#include <stdint.h> -#include "sthread.h" - -/* print HELLO! arg times yield thread each time */ -static void loop(void *arg) { - int i; - for (i = 0; i < (intptr_t) arg; i++) { - printf("HELLO!\n"); - sthread_yield(); - } -} - -int main(int argc, char **argv) { - /* threads with different lifetimes */ - sthread_create(loop, (void *) 1); - sthread_create(loop, (void *) 2); - sthread_create(loop, (void *) 3); - sthread_create(loop, (void *) 4); - sthread_start(); - return 0; -} \ No newline at end of file diff --git a/syscalls/Makefile b/syscalls/Makefile index 56ac1743ae2b0cbdc900a8fe1006857f8c87c7f3..8bc0922de8496faf821d2a6bebad62dc0d9bc036 100644 --- a/syscalls/Makefile +++ b/syscalls/Makefile @@ -4,9 +4,6 @@ CFLAGS = -Wall -Werror -g -O0 all: greeting -greeting.o: greeting.c -output.o: output.s - greeting: greeting.o output.o $(CC) $(CFLAGS) greeting.o output.o -o greeting $(LDFLAGS) diff --git a/syscalls/greeting b/syscalls/greeting deleted file mode 100755 index 0b82be7de95002d4e1aa52cef7b4583e66490548..0000000000000000000000000000000000000000 Binary files a/syscalls/greeting and /dev/null differ diff --git a/syscalls/greeting.c b/syscalls/greeting.c index d8ac7f389b30d3cd26d72d99426601adcd5662a7..b5c2fc7e58658368aefd9d76b348882350cdc0df 100644 --- a/syscalls/greeting.c +++ b/syscalls/greeting.c @@ -1,6 +1,7 @@ #include <stdint.h> -extern int64_t output(void *data, uint64_t size); +int64_t output(void *data, uint64_t size); + /* INCOMING TRANSMISSION... */ int main() { diff --git a/syscalls/output.s b/syscalls/output.s deleted file mode 100644 index d85f18c3cdb8105e06de7522645abb92c0cd1f9a..0000000000000000000000000000000000000000 --- a/syscalls/output.s +++ /dev/null @@ -1,11 +0,0 @@ -# function outputs alien message (writes msg to stdout) -# arg1 (msg address) at %rdi, arg2 (msg size) at %rsi -# returns size of outputted msg stored in %rax -.globl output -output: - movq $1, %rax # syscall write is 1 - movq %rsi, %rdx # write arg3 (count) is sizeof(msg) - movq %rdi, %rsi # write arg2 (*buf) is msg - movq $1, %rdi # write arg1 (fd) is 1 - syscall # make syscall - \ No newline at end of file